Showing posts with label between. Show all posts
Showing posts with label between. Show all posts

Thursday, February 5, 2015

Android beginner tutorial Part 58 Send and receive data between Activities

In this tutorial well learn how to transfer data between two Activities.

We are going to have two activities - MainActivity and SecondActivity. Ive already covered how to create secondary Activities in the tutorial about Intents. I explained how to do that in detail in that tutorial, so if you followed my tutorials you should be able to do that. A reminder: add an Activity in the Manifest XML file and add a SecondActivity.java class and an activity_second.xml layout.

The main activity is going to have a button that opens a new Activity. However, it launches it with an intention to receive a result. This is done using a startActivityForResult() method. By doing it that way we tell our main activity to expect a result from the Activity were launching. In the second activity well have an EditText input and a button. When the button is pressed, the data in the text field is sent back to the main activity and the second activity is closed.

When the main activity receives the message, it displays it in a toast.

Lets see how we are going to do this. Firstly, go to activity_main.xml layout and add a button:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="First Activity"
/>

<Button android:id="@+id/goButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Launch Second Activity"
/>

</LinearLayout>

In MainActivity.java declare an ID for the request:

private static final int IDM_TEST = 101;

Use it when you call startActivityForResult() in the click event handler of the button:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final Button btn = (Button)findViewById(R.id.goButton);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), SecondActivity.class);
startActivityForResult(intent, IDM_TEST);
}
});
}

The request is sent, but what about receiving the results?

Create a function onActivityResult(). Call its superclass and then check if resultCode parameter equals RESULT_OK. That means weve received a result as expected.

Inside the if...statement declare an "extras" variable, which is a Bundle type object and gets its values from data.getExtras().

Then we check if requestCode parameters value equals IDM_TEST (the id we passed when requesting the result), and if so, toast the results using the extras object:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
// handle results
if(requestCode == IDM_TEST){
Toast toast = Toast.makeText(MainActivity.this, extras.getString("UserText"), Toast.LENGTH_SHORT);
toast.show();
}
}
}

Full MainActivity.java code:

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity{

private static final int IDM_TEST = 101;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final Button btn = (Button)findViewById(R.id.goButton);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), SecondActivity.class);
startActivityForResult(intent, IDM_TEST);
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
// handle results
if(requestCode == IDM_TEST){
Toast toast = Toast.makeText(MainActivity.this, extras.getString("UserText"), Toast.LENGTH_SHORT);
toast.show();
}
}
}

}

The activity_second.xml layout has an EditText object and a Button:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="Second Activity"
/>

<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Write something here..."
android:id="@+id/editText"
/>

<Button android:id="@+id/backButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK"
/>

</LinearLayout>

In SecondActivity.java class, we only need to handle the button. When it is clicked, we create an intent and use a method called setResult(). To pass the values to the parent activity we can use the putExtra() method of the intent. It has 2 parameters - name and value.

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SecondActivity extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

final Button btn = (Button)findViewById(R.id.backButton);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
EditText text = (EditText)findViewById(R.id.editText);

Intent intent = new Intent();
intent.putExtra("UserText", text.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
});
}

}

Thats all! Now you can go to the second activity from your main one, enter some text, return to the main activity using the button and see the main activity display the data received from the second one.

Thanks for reading!
Read more »

Monday, February 2, 2015

Data sharing between two Android applications

In this tutorial Im going to illustrate how we can share data between two Android applications using Shared Preference.
To implement this I used two Android applications. One is "Datawriter" and the other one is "Datareader".
"Datawriter" is to update shared data. Its package name is com.writer.data class name is DataWriterActivity . Here is the code for DataWriterActivity class.

package com.writer.data;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

public class DataWriterActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dataWriter();
}

public void dataWriter(){
String strShareValue = "Hello! this is shared data";
SharedPreferences prefs = getSharedPreferences("demopref",Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("demostring", strShareValue);
editor.commit();
}
}

dataWriter method will write the string Hello! this is shared data to a shared memory.

Next application is to read shared data. The application name is  Datareader and its package name is com.datareader class name is DataReaderActivity. Here is the code for DataReaderActivity class.
package com.datareader;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class DataReaderActivity extends Activity {
String dataShared;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dataRead();
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(dataShared);

}

public void dataRead(){
Context con;
try {
con = createPackageContext("com.writer.data", 0);
SharedPreferences pref = con.getSharedPreferences("demopref", Context.MODE_PRIVATE);
dataShared = pref.getString("demostring", "No Value");
}
catch (NameNotFoundException e) {
Log.e("Not data shared", e.toString());
}
}
}

"com.writer.data" in the highlighted line is the package name of the first application which we used to share data.
Following is the out put of second application :

Read more »

Thursday, January 29, 2015

Understanding the difference between Object Drawing Mode vs Merge Drawing Mode in Flash PART 1 Flash CS5 Beginners Tutorial


When you use the drawing tools in Flash, you have the option to draw in two modes: object drawing mode vs merge drawing mode. This article will explain a few of the differences between the two.

Step 1

Create a new Flash document.

Step 2

Select the brush tool. You can use any brush size, color and shape that youd like. But I recommend that you choose a brush size thats not too small so that what you draw will be thicker and more visible. Also, make sure that the color you choose is different from the background color of your document.

Step 3

Once the brush tool is selected, take a look at the toolbar and look for the icon that shows a circle inside a square.


You can click on this button, and it allows you to activate or deactivate the object drawing mode. If the icon appears to look inset, it means that the object drawing mode is ON. If not, then it means that the object drawing mode is OFF, and you are therefore on merge drawing mode instead.


Clicking on this button lets you switch between object mode and merge mode. Check the object drawing mode button in your toolbar to see whether its ON or OFF. If its ON, then click on it to turn it OFF. If not, then leave it as is.

Step 4

The best way to explain the differences between the two drawing modes is to create examples. So make sure that the object drawing mode is OFF. Then draw a line on the left side of the stage using the brush tool.


Now remember, since the object drawing mode is OFF, this means that this line was drawn in merge mode.

Step 5

Now lets switch to the object drawing mode. So this time, make sure that the object drawing mode is ON. And then draw another line on the right side of the stage using the brush tool.


Immediately, youll notice a difference. Youll see that this new line that you drew while you were in the object drawing mode is enclosed in a box. When its enclosed in this box, it means that the artwork is currently selected. And when its selected, this means that you can edit it (e.g. move it, change the color, modify the size). However, when you select something that was drawn in merge mode, it will NOT show a box or a border around it. In the next step, well see what happens instead.

Step 6

Now lets select the other line to see how artwork drawn in merge mode will look like when its selected.

In the toolbar, choose the selection tool (its the black arrow).


Then click on the line that you drew on the left side of the stage in order to select it.


Here, youll see that the selection looks different (click on the image above to enlarge). Instead of being enclosed in a box, youll see dots all over the artwork. So when you see this, this means that you currently have a merge type of drawing selected.

So here we see that object drawings and merge drawings look different from each other when they are selected. This easily lets us know whether something is an object drawing or a merge drawing.

But aside from this, what other differences do they have?
Another difference between a merge drawing and an object drawing is this:

Merge drawing - you have the ability to directly select just a portion of the drawing

Object drawing - if you try to select even just a small portion of the object drawing, the entire object drawing automatically gets selected

To better understand this, lets practice it using our examples on the stage.

Step 7

Make sure youre still using the selection tool. Lets first try to select a portion of the merge drawing on the left side of the stage. To select a portion, use the selection tool, then just click and drag a selection area that covers a small part of the artwork. Refer to the image below as a guide:


Once you release the mouse and youve selected the portion, youll see the dots appear only on top of the part that was selected. So this means that the parts that are not covered with dots are not included in the selection.

Step 8

Now lets edit the currently selected portion.

Go to the toolbar, and select a new color for the selected portion by picking one from the fill color box.


Just click on the color box and select a different color. Youll then see that the new color only applies to the selected portion.


So with merge drawings, you see that we can easily select just a portion of it. The selected portion can then be edited without affecting the rest of the drawing. You can change the selected portions color, modify its shape, move it to a different location, or you can even delete it if youd like.

In the next step, well see if we can do the same thing with the object drawing on the right.

Step 9

So now, while still using the selection tool, well try to select just a portion of the drawing on the right.

Go ahead and click and drag a selection area that just covers a portion of the shape, similar to what we did in step 7.


Once you release the mouse, youll see that even though you selected only a portion of the drawing, the entire shape still gets selected. That is what happens with object drawings. As long as you select even the tiniest portion of the drawing, the entire thing gets selected.

But what can you do if youve got an object drawing, but youd still like to be able to select only a portion of it? The next step will show you how.

Step 10

With object drawings, its still possible to select just a portion of the shape. Theres just an extra step that you have to do.

First, take note of the edit bar, which can be found above the stage. It should say Scene 1.


The edit bar tells you where you are. Right now, you are on scene 1.

Now go ahead and double-click on the object drawing on the right side of the stage.

After you double-click on it, look at the edit bar again. It should now say Scene 1 followed by Drawing Object.


What this means is that you are now inside the drawing object. You should see that you no longer have the box around your drawing. So in a way, its as if you opened the box to get direct access to the artwork inside it. And now that youre inside the box, youll be able to treat the artwork as a merge drawing. So youll now have the ability to select just portions of it instead of the entire shape.

Step 11

Once youre finished editing an object drawing, its important that you go back to the main scene. Otherwise, you might end up inadvertently adding unwanted elements to the object drawing that youre currently editing.

To go back to scene 1, just click on the Scene 1 link in the edit bar.


NOTE: Always be mindful of the edit bar. Its a common mistake for many to accidentally double-click on an object drawing, and then they fail to notice that they are no longer in the main scene, but continue to work as if they are.


In part 2 of this series, well create some object and merge drawing examples using the oval tool.

Understanding the difference between Object Drawing Mode vs Merge Drawing Mode in Flash - PART 2: NEXT >>
Read more »