Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Action bar title on button click

I have two Activity activity1 and activity2, activity1 has two buttons, button1 and button2. When click on button1 it should link to activity2 should have title in Action-bar has "am button1" and when click on button2 it again link to activity2 and it should have title in Action-bar has "am button2".

  1. When button1 click on Activity1 it should pass data through put-extra of intent and change activity action-bar title has "am button1".
  2. Activity2 should receive data from activity1 and make change the action-bar in that.

Any body please help me to do this.

like image 689
Lakshmi Avatar asked Mar 20 '23 08:03

Lakshmi


1 Answers

Activity 1 class

public class ActivityOne extends Activity{

Button btnOne, btnTwo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    btnOne = (Button) findViewById(R.id.btnOne);
    btnOne.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
            intent.putExtra("title", "am Button1");
            startActivity(intent);

        }
    });

    btnTwo = (Button) findViewById(R.id.btnTwo);
    btnOne.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
            intent.putExtra("title", "am Button2");
            startActivity(intent);

        }
    });
}
}

ActivityTwo class

public class ActivityTwo extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String title = intent.getStringExtra("title");

    getActionBar().setTitle(title);
}

}
like image 164
kevz Avatar answered Apr 01 '23 13:04

kevz