Monday, 2 September 2013

Intent

This exercise is about Intents which is used to create new activities.
Open Eclipse IDE.
Create new project with details:
Application Name: AndroidIntent
Project Name: AndroidIntent
Package Name: com.example.androidintent
Click NEXT.
 Click NEXT.
 Click NEXT.
 Click NEXT.
 Now make changes in the layout file.
Add an EditText(Text Box used to get data input) and a Button.
This EditText and Button will be used to get input from user and send the data to the next activity.

<EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et"
        android:layout_centerHorizontal="true"
        android:text="Click Me" />

 Now save the input from user into a String and send the data to the next activity using put() of Intent.

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

public class MainActivity extends Activity
{
EditText et;
Button b;
String s;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.et);
b=(Button)findViewById(R.id.button1);
intent=new Intent(this,Abc.class);*
b.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
s=et.getText().toString();
intent.putExtra("s",""+s);
startActivity(intent);
}
});
}

}

*while creating the new activity remember the name provided. otherwise the program will generate error.
 Now we need to create another activity which would start when we will click the button.
In the Package Explorer right click your project.
Click New>Other.
 Select Android Activity and click NEXT.
 Click NEXT.
 Give the name to the Activity provided in the previous java class and click FINISH.(Abc in this case)
 Now Create a TextView which would display the data received from the previous activity.

<TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

 Use the function get() to get the data and then set it to the textview.

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class Abc extends Activity
{
TextView tv;
Bundle bundle;
String s;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abc);
tv=(TextView)findViewById(R.id.tv);
bundle=getIntent().getExtras();
s=bundle.get("s").toString();
tv.setText(""+s);
}

}


 Now your app is ready to be tested.


No comments:

Post a Comment