4

Suppose I have two buttons, one is an action button which is in the action bar(@+id/button1). And another is common button in the layout(@+id/button2).

How can I set the button1 disabled when i click the button2?

findViewById(button1) seems not work. it will return null.

this is my menu xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/button1" android:title="submit" android:showAsAction="always" /> </menu>

and this is my mainacticity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.button1 ) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    button1 = (Button) findViewById(R.id.button1);/*which return null*/
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setEnabled(false);/*what i failed to do*/
        }
    });
}
Yriuns
  • 755
  • 1
  • 8
  • 22

3 Answers3

3

1.Create a variable MenuItem in your Activity class

    MenuItem menuItem;

2. Find your variable in onCreateOptionsMenu

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_main, menu);
            menuItem = menu.findItem(R.id.item_circuit);
            return super.onCreateOptionsMenu(menu);
        }
  1. Disable the item in your buttonClick method

     button2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        menuItem.setEnabled(false);
                    }
                });
    
Marta Tenés
  • 2,102
  • 1
  • 13
  • 22
  • Maybe I got the story wrong. I want to disabled button1(action button) when i click button2(common button) – Yriuns Aug 25 '15 at 07:15
1

Try this
public void button1click(View v) { Button button2=(Button)getActionBar().getCustomView().findViewById(R.id.button2); button2.setClickable(false); button2.setEnabled(false); }

priwiljay
  • 2,603
  • 1
  • 18
  • 21
0

You have to inflate the layout/views before using findViewById.

Like that:

private Toolbar toolbar;

// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

cartLayout = (RelativeLayout) toolbar.findViewById(R.id.rl_cart);

Your toolbar layout would be like this:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right">

    <RelativeLayout
        android:id="@+id/rl_cart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:padding="5dp">


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:src="@drawable/ic_cart" />

    </RelativeLayout>
</RelativeLayout>

You can find more answer here:

How can I implement custom Action Bar with custom buttons in Android?

How to set two custom actionbar button in left and right in android?

Community
  • 1
  • 1
Ashish Tiwari
  • 2,168
  • 4
  • 30
  • 54