Thursday, May 17, 2012

Example 2:

Example 2:

Continuation of Example 1,

Add one more button to main.xml layout and name it as Activity.
Then add another layout called activity1.xml, For that right click on the layout folder --> new --> Android Xml File





Then add a TextView to activity1.xml

1. Then open AndroidManifest.xml and goes to 'Application' tab at the bottom. And in Application Nodes section press 'Add'.
2. Then select Activity from the popup screen
3. Then you can see anothe section Attributes for Activity, there you haveto click 'Name*',
4. There a popup should be appeared,like below image, there for name field you have to give 'MyActivity'.
5. Then check 'Inherited Abstract methods' and press Finish.




Then copy the code below to MyActivity.java

package com.jeeth.examples.myfirstandroidproject;

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

public class MyActivity extends Activity {
TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   /**
    * mention for which layout does this activity belongs.
    */
   setContentView(R.layout.activity1);
   
   /**
    * Getting data send from MyFirstAndroidProjectActivity.java
    */
   Bundle b=this.getIntent().getExtras();
   
   /**
    * getting data from bundle and assigning to textview.
    */
   textview=(TextView)findViewById(R.id.textView1);
   textview.setText(b.getString("data"));
   
   // TODO Auto-generated method stub
}

}


And then Modify  the code of MyFirstAndroidProjectActivity.java like below,

package com.jeeth.examples.myfirstandroidproject;

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

public class MyFirstAndroidProjectActivity extends Activity {
private EditText edittext;
private Button button;
private Button buttonActivity;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/**
* Initialize EditText and Button
*/
edittext = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);

/**
* action to perform on button click 
*/
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
Toast.makeText(MyFirstAndroidProjectActivity.this,
edittext.getText(), Toast.LENGTH_LONG).show();
}
});
/**
* action to perform on Activity button click
*/
buttonActivity=(Button)findViewById(R.id.button2);
buttonActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* Calling another activity on button click
* For that we have to use Intent
* And for transfering any data from this activity to other activity
* we have to use Bundle
*/
Intent intent=new Intent(MyFirstAndroidProjectActivity.this,MyActivity.class);
Bundle b=new Bundle();
/**
* Appending Edittext values to Bundle.
* and then appending Bundle to Intent
* And then starting other activity
*/
b.putString("data",edittext.getText().toString());
intent.putExtras(b);
startActivity(intent);
}
});
}
}



Hmmmm....... now you can run the application.