p3r02d3r0
p3r02d3r0

Reputation: 37

Change layout color by clicking a submenu item

I'm a newbie with Java and Android, and currently I'm working on a simple app which has a menu button, with several submenu items. Basically, I'm learning stuff by creating this app.

What I want to do? I would like to change the main layout color by clicking those submenu items.

Anyway:

This is my menu icon which opens submenus:

<item
    android:id="@+id/action_pozadine"
    android:orderInCategory="100"
    android:icon="@drawable/ic_action_picture"
    app:showAsAction="always"
    android:title="pozadine"/>

Submenus are set in a separate menu XML:

<item
    android:title="Pozadina1"
    android:id="@+id/poz_1"
    />
<item
    android:title="Pozadina2"
    android:id="@+id/poz_2"
    />

The submenu items are inflated in a pop-up window when clicked on "action_pozadine":

case R.id.action_pozadine:

        View menuItemView = findViewById(R.id.action_pozadine);
        PopupMenu popupMenu = new PopupMenu(this, menuItemView);
        popupMenu.inflate(R.menu.submenu);
        popupMenu.show();
        return true;

How do I change the color of the main layout by clicking on one of those submenu items? Is it even possible?

I searched tons of tutorials, but all of them are with Button onclick listeners, and you can't put Buttons in menus, or submenus.

Any help is really appreciated.


So I managed to get it work. This is how onMenuItemClick code looks right now:

@Override
public boolean onMenuItemClick(MenuItem submenu) {
    switch (submenu.getItemId()) {
        case R.id.poz_1:
            Toast.makeText(getApplicationContext(), "Pozadina 1", Toast.LENGTH_SHORT).show();                
            return true;

        case R.id.poz_2:
            Toast.makeText(getBaseContext(), "Pozadina 2", Toast.LENGTH_SHORT).show();
            return true;


    }

Anyone knows how to change the layout background color, or call a drawable of a layout background by clicking on those submenu buttons?

Thanks.

Upvotes: 1

Views: 712

Answers (1)

Kostya Buts
Kostya Buts

Reputation: 1577

Get your layout:

LinearLayout layout = new LinearLayout(this);

Alternatively you can get it using id:

LinearLayout layout = (LinearLayout)findViewById(R.id.yourLayoutId);

Then simply:

layout.setBackgroundColor(Color.RED);

Upvotes: 0

Related Questions