How to change the text on the action bar

前端 未结 18 1009
春和景丽
春和景丽 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: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

    
    
    
        
        
    
    
    
    

    and then set it like this :

    getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_contactus));
    

提交回复
热议问题