Lakshmi
Lakshmi

Reputation: 63

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.

Upvotes: 4

Views: 3242

Answers (4)

Hardik Trivedi
Hardik Trivedi

Reputation: 6072

Here is your solution

Activity 1 : ON click of any button 1

Intent intent=new Intent(this,ActivityTwo.class);
intent.putExtra("title", "M button 1");
startActivity(intent);

Activity 1 : ON click of any button 1

Intent intent=new Intent(this,ActivityTwo.class);
intent.putExtra("title", "M button 2");
startActivity(intent);

Now on Activity 2 :

String title=getIntent().getStringExtra("title");
getActionBar().setTitle(title);

or

String title=getIntent().getStringExtra("title");
getSupportActionBar().setTitle(title);

Upvotes: 2

kevz
kevz

Reputation: 2737

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);
}

}

Upvotes: 5

Lokesh
Lokesh

Reputation: 5368

In manifest file while declaring your second activity, set your required name as label:

<activity
        android:name="your second activity"
        android:label="@string/your 2nd activity name" >
    </activity>

Upvotes: 0

jyomin
jyomin

Reputation: 1967

In the first activity

Intent mIntent;   
     @Override
            public void onClick(View v) {

                    mIntent = new Intent(FirstActivity.this,SecondActivity.class);
                    switch (v.getId()) {
                    case R.id.first_btn:
                        mIntent.putExtra("buttonClicked", "Am Button One");
                        break;
                    case R.id.second_btn:
                        mIntent.putExtra("buttonClicked", "Am Button Second ");
                        break;}
                        startActivity(mIntent);}

        //and in the second activity  write:
            private String mSelectedButton;
        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_web_view);
                mSelectedButton = getIntent().getIntExtra("buttonClicked", "");
            }

then use the variable mSelectedButton to set the title

Upvotes: 0

Related Questions