Reputation: 63
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".
Any body please help me to do this.
Upvotes: 4
Views: 3242
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
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
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
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