Home > Mobile >  How can I change menu items of different toolbars in the same activity?
How can I change menu items of different toolbars in the same activity?

Time:04-29

I'm trying to change the locale of the text on the options menu items. I've been trying to do this in onPrepareOptionsMenu. I can get the correct locale text from resources and when I print out the result of setTitle it looks correct. However, on the application itself, old titles remain. I don't know what I'm missing any help is appreciated.

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem toggle = menu.findItem(R.id.toggleType);
        MenuItem options = menu.findItem(R.id.options);
        MenuItem exit = menu.findItem(R.id.exit);
        String new1 = context.getResources().getString(R.string.satellite);
        String new2 = context.getResources().getString(R.string.options);
        String new3 = context.getResources().getString(R.string.exit);
        toggle.setTitle(new1);
        options.setTitle(new2);
        exit.setTitle(new3);
        return super.onPrepareOptionsMenu(menu);
    }

In main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@ id/toggleType"
        android:title="@string/satellite"
        app:showAsAction="never" />
    <item
        android:id="@ id/options"
        android:title="@string/options"
        app:showAsAction="never" />
    <item
        android:id="@ id/exit"
        android:title="@string/exit"
        app:showAsAction="never" />
</menu>

EDIT:

I think I realise the problem but I still have no solution for it. The problem is that I'm using TabHost and I have three tabs in my layout, so in order to show the toolbar on all three of the tabs WHILE keeping the tabwidget at the bottom of the screen, I created three toolbars in each tab. So when I change the titles of those items only the last toolbars' menu items update. Is there a way I can change the other toolbar menu items' titles as well?

Here's the code. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/tabHost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">



    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="@color/colorAccent"
            android:layout_alignParentBottom="true"></TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="50dp">

            <!-- TAB 1 -->
            <LinearLayout
                android:id="@ id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:orientation="vertical">

                <androidx.appcompat.widget.Toolbar
                    android:id="@ id/toolbar1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?attr/colorPrimary"
                    android:minHeight="?attr/actionBarSize"
                    android:theme="?attr/actionBarTheme" />

                  <!-- SOME CONTENT HERE -->

            </LinearLayout>

            <!-- TAB 2 -->
            <LinearLayout
                android:id="@ id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#da8200"
                android:orientation="vertical">

                <androidx.appcompat.widget.Toolbar
                    android:id="@ id/toolbar2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?attr/colorPrimary"
                    android:minHeight="?attr/actionBarSize"
                    android:theme="?attr/actionBarTheme" />

                  <!-- SOME CONTENT HERE -->

            </LinearLayout>

            <!-- TAB 3 -->
            <LinearLayout
                android:id="@ id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <androidx.appcompat.widget.Toolbar
                    android:id="@ id/toolbar3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?attr/colorPrimary"
                    android:minHeight="?attr/actionBarSize"
                    android:theme="?attr/actionBarTheme" />
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:background="@drawable/gradient"
                    android:layout_height="fill_parent">

                  <!-- SOME CONTENT HERE -->

                </LinearLayout>
            </LinearLayout>
        </FrameLayout>
    </RelativeLayout>
</TabHost>

MainActivity.java -> onCreate():

        Toolbar toolbar1 = findViewById(R.id.toolbar1);
        Toolbar toolbar2 = findViewById(R.id.toolbar2);
        Toolbar toolbar3 = findViewById(R.id.toolbar3);
        setSupportActionBar(toolbar1);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("");
        }
        setSupportActionBar(toolbar2);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("");
        }
        setSupportActionBar(toolbar3);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("");
        }
        toolbar1.inflateMenu(R.menu.main_menu);
        toolbar2.inflateMenu(R.menu.main_menu);
        toolbar3.inflateMenu(R.menu.main_menu);

CodePudding user response:

Because I'm a dumbass I couldn't think of changing the margins. Anyway here's a working implementation with a shared toolbar across all tabs.

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="@color/colorAccent"
            android:layout_alignParentBottom="true"></TabWidget>

        <androidx.appcompat.widget.Toolbar
            android:id="@ id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="?attr/colorPrimary"
            android:gravity="top"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="50dp"
            android:layout_marginTop="50dp">
  • Related