How to change the text on the action bar

前端 未结 18 777
春和景丽
春和景丽 2020-11-22 12:30

Currently it just displays the name of the application and I want it to display something custom and be different for each screen in my app.

For example: my home scr

相关标签:
18条回答
  • 2020-11-22 13:09

    Little bit older but had the same problem. I did it like this:

    strings.xml

    <string name="title_awesome_app">My Awesome App</string>

    and make sure you set this in your AndroidManifest.xml:

    <activity
                ...
                android:label="@string/title_awesome_app" >
                ...
    </activity>
    

    it's easy and you don't have to worry about null-references and other stuff.

    0 讨论(0)
  • 2020-11-22 13:09

    In onCreateView add this:

    (activity as AppCompatActivity).supportActionBar?.title ="My APP Title"
    
    0 讨论(0)
  • 2020-11-22 13:13

    Kotlin

    You can do it programmatically in Kotlin this way. You just ignore it if "supportActionBar" is null:

        supportActionBar?.setTitle(R.string.my_text)
        supportActionBar?.title = "My text"
    

    Or this way. It throws an exception if "supportActionBar" is null.

        supportActionBar!!.setTitle(R.string.my_text)
        supportActionBar!!.title = "My text"
    
    0 讨论(0)
  • 2020-11-22 13:14

    Update: Latest ActionBar (Title) pattern:

    FYI, ActionBar was introduced in API Level 11. ActionBar is a window feature at the top of the Activity that may display the activity title, navigation modes, and other interactive items like search.

    I exactly remember about customizing title bar and making it consistent through the application. So I can make a comparison with the earlier days and can list some of the advantages of using ActionBar:

    1. It offers your users a familiar interface across applications that the system gracefully adapts for different screen configurations.
    2. Developers don't need to write much code for displaying the Activity Title, icons and navigation modes because ActionBar is already ready with top level abstraction.

    For example:

    enter image description here

    enter image description here

    => Normal way,

    getActionBar().setTitle("Hello world App");   
    getSupportActionBar().setTitle("Hello world App");  // provide compatibility to all the versions
    

    => Customizing Action Bar,

    For example:

    @Override
    public void setActionBar(String heading) {
        // TODO Auto-generated method stub
    
        com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.title_bar_gray)));
        actionBar.setTitle(heading);
        actionBar.show();
    
    }
    

    Styling the Action Bar:

    The ActionBar provides you with basic and familiar looks, navigation modes and other quick actions to perform. But that doesn't mean it looks the same in every app. You can customize it as per your UI and design requirements. You just have to define and write styles and themes.

    Read more at: Styling the Action Bar

    And if you want to generate styles for ActionBar then this Style Generator tool can help you out.

    =================================================================================

    Old: Earlier days:

    => Normal way,

    you can Change the Title of each screen (i.e. Activity) by setting their Android:label

       <activity android:name=".Hello_World"
                      android:label="This is the Hello World Application">
       </activity>
    

    => Custom - Title - bar


    But if you want to Customize title-bar in your own way, i.e. Want to put Image icon and custom-text, then the following code works for me:

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
    

    titlebar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="400dp" 
      android:layout_height="fill_parent"
      android:orientation="horizontal">
    
    <ImageView android:id="@+id/ImageView01" 
                android:layout_width="57dp" 
                android:layout_height="wrap_content"
                android:background="@drawable/icon1"/>
    
    <TextView 
    
      android:id="@+id/myTitle" 
      android:text="This is my new title" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:textColor="@color/titletextcolor"
       />
    </LinearLayout>
    

    TitleBar.java

    public class TitleBar extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final boolean customTitleSupported = 
                    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
            setContentView(R.layout.main);
            if (customTitleSupported) {
                getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                    R.layout.titlebar);
            }
            final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
            if (myTitleText != null) {
                myTitleText.setText("NEW TITLE");
                // user can also set color using "Color" and then
                // "Color value constant"
                // myTitleText.setBackgroundColor(Color.GREEN);
            }
        }
    }
    

    strings.xml

    The strings.xml file is defined under the values folder.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, Set_Text_TitleBar!</string>
        <string name="app_name">Set_Text_TitleBar</string>
        <color name="titlebackgroundcolor">#3232CD</color>
        <color name="titletextcolor">#FFFF00</color>
    </resources>
    
    0 讨论(0)
  • 2020-11-22 13:14

    if u r using navigation bar to change fragment then u can add change it where u r changing Fragment like below example :

     public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.clientsidedrawer:
                        //    Toast.makeText(getApplicationContext(),"Client selected",Toast.LENGTH_SHORT).show();
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new clients_fragment()).commit();
                        break;
        
                    case R.id.affffdatasidedrawer:
                        getSupportActionBar().setTitle("Add Client");
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new addclient_fragment()).commit();
                        break;
        
                    case R.id.editid:
                        getSupportActionBar().setTitle("Edit Clients");
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Editclient()).commit();
                        break;
        
                    case R.id.taskid:
                        getSupportActionBar().setTitle("Task manager");
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new Taskmanager()).commit();
                        break;
    

    if u r using simple activity then just call :

    getSupportActionBar().setTitle("Contact Us");
    

    to change actionbar/toolbar color in activity use :

    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#06023b")));
    

    to set gradient to actionbar first create gradient : Example directry created > R.drawable.gradient_contactus

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        >
    
        <gradient
            android:angle="90"
            android:startColor="#2980b9"
            android:centerColor="#6dd5fa"
            android:endColor="#2980b9">
        </gradient>
    
    
    </shape>
    

    and then set it like this :

    getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_contactus));
    
    0 讨论(0)
  • 2020-11-22 13:15

    The Easiest way to change the action bar name is to go to the AndroidManifest.xml and type this code.

    <activity android:name=".MainActivity"
        android:label="Your Label> </activity>
    
    0 讨论(0)
提交回复
热议问题