Change the name of the action bar when a button is pressed

I have two Activity activity1 and activity2, Activity1 has two buttons, button1 and button2. When you click on button1, it should refer to activity2, if the title in Action-bar has "am button1", and when you press button2 it again refers to activity2, and it should have a title in Action-bar has "am button2".

  • When button1 clicks on Activity1, it must pass data through put-extra of intent and modify the action. The action-bar header has "am button1".
  • Activity2 should receive data from Activity1 and make changes to the action bar.

Any body please help me do this.

+4
source share
4

1

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

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

}
+5

1: 1

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

1: 1

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

2:

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

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

In the first action

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 mSelectedButton variable to set the title

0
source

In the manifest file, when declaring your second action, specify the required name as a label:

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

Source: https://habr.com/ru/post/1530167/


All Articles