-1

Is there anyway to change the strings at runtime in java. please check my updated question in mainActivity.java i want to change menu item title

can i set menu item title in MainActivity.java because i want to change title in some conditions how to settitle in java file...

strings.xml

<resources>
    <string name="app_name">check</string>
    <string name="title_home">Home</string>
    <string name="title_dashboard">Dashboard</string>
    <string name="title_notifications">Notifications</string>
</resources>

The navigation menu where I have used the strings

menu_nevigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/home"
        android:title="@string/title_home"
        android:icon="@drawable/ic_home" />
    <item
        android:id="@+id/dash"
        android:title="@string/title_dashboard"
        android:icon="@drawable/ic_dash"/>
    <item
        android:id="@+id/notification"
        android:title="@string/title_notifications"
        android:icon="@drawable/ic_notification"/>
</menu>

MainActivity.java

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        //Initialize And Assign Variable
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);

             menu = bottomNavigationView.getMenu();  //mynavigationview is the name of your navigationcomponent
    MenuItem menuitem = menu.findItem(R.id.home);  //menu is the menuhere
    menuitem.setTitle("Your title");

        //Set Home Selected
        bottomNavigationView.setSelectedItemId(R.id.home);


        //Perform ItemSelected
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.dash:
                    startActivity(new Intent(getApplicationContext()
                            ,dash.class));
                    overridePendingTransition(0,0);
                    return true;
                    case R.id.home:
                        return true;
                    case R.id.notifications:
                        startActivity(new Intent(getApplicationContext()
                                ,notifications.class));
                        overridePendingTransition(0,0);
                        return true;

                }
                return false;
            }
        });

    }


}

2 Answers2

2

From your explanation in the comments I am guessing that you want to change your R.string folder at run-time. Which is not possible. In fact the entire res folder cannot be modified at run-time.

If you want to change the menu titles at runtime

menu = mynavigationview.getMenu()  //mynavigationview is the name of your navigationcomponent
MenuItem menuitem = menu.getItem(*insert item index here*);  //menu is the menu here 
menuitem.setTitle("Your title");

This should work. If you want to keep the titles stored then you should use shared preferences to do so and set them from the preferences each time your application is launched.

Aditya Kurkure
  • 422
  • 5
  • 19
  • menu = bottomNavigationView.getMenu() //mynavigationview is the name of your navigationcomponent MenuItem home = menu.findItem(R.id.home); //menu is the menuhere home.setTitle("Your title"); NOT WORKING @AdityaKurkure – Priya Bhaduriya May 23 '20 at 13:05
  • please check i updated mainactivity.java in question @AdityaKurkure.. – Priya Bhaduriya May 23 '20 at 13:17
  • error: cannot find symbol variable menu @AdityaKurkure – Priya Bhaduriya May 23 '20 at 13:21
  • Menu menu = mynavigationview.getMenu() //mynavigationview is the name of your navigationcomponent MenuItem menuitem = menu.findtem(*insert item index here*); //menu is the menu here menuitem.setTitle("Your title"); please update answer. @AdityaKurkure – Priya Bhaduriya May 23 '20 at 13:36
0

First you need to get your navigationView with findViewById(R.id.nav_view_id), then:

Menu menu = navigationView.getMenu();
MenuItem item = menu.findItem(R.id.your_menuItem_id);
// or MenuItem item = menu.getItem(0) if you have one MenuItem

Then you create a method in which you will choose which title you currently need.

//for example
 protected String getMyTitle(){
    if(/*something*/){
        return getResources().getString(R.id.title_home);
    }else if(/*something*/){
        return getResources().getString(R.id.title_dashboard);
    }else{
        return getResources().getString(R.id.title_notifications);
    }
}

And as a result, we put the text in item.

item.setTitle(getMyTitle());

This link may also help you: link

Razrab.ytka
  • 101
  • 4
  • title_home, title_dashboard, title_notifications these 3 strings i am using in menu title i want to set title in mainactivity.java @Razrab.ytka please check updated question.. – Priya Bhaduriya May 23 '20 at 12:35
  • your answer is now helping but i got it already thank you so much for help me @Razrab.ytka – Priya Bhaduriya May 23 '20 at 13:44