Reputation: 171
I have developed a action-bar in Android Studio, however, the action-bar displays the activity name rather than the app name along with the icons.
For example I want it to display the app-name plus the refresh, setting and User Icon. Instead it display the activity name along with refresh, setting and User Icon.
Menuactivity.java
public class MenuActivity extends ActionBarActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_user) {
Intent intent= new Intent(this,DashboardActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
MenuMain shows the application name however once run the code it displays the activity name rather than the app name, how can I rectify this?.
Upvotes: 0
Views: 5656
Reputation: 19351
1. The easiest way is:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.strind.app_name);
2. You can also use a custom Toolbar
layout
Change your existing code us
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
To like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_main;
Follow please these my tutorial to see how to implement it.
Here's my solution - create a TextDrawable logo using custom toolbar
- Create a custom layout with name
action_bar.xml
Put into it this code
<ImageView android:id="@+id/image_view" android:layout_width="32dp" android:layout_height="32dp" tools:src="@mipmap/ic_launcher"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:text="@string/app_name"> //THIS GUY android:textColor="#ffffff" android:textSize="24sp" android:textAlignment="gravity" android:gravity="center_vertical"/>
Add to your
onCreate
method inMainActivity
class this code://SET A DRAWABLE TO IMAGEVIEW Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getSupportActionBar().setCustomView(R.layout.actionbar_main); TextDrawable drawable = TextDrawable.builder() .buildRound("A", getResources().getColor(R.color.colorAccent)); ImageView imageView = (ImageView) findViewById(R.id.image_view); imageView.setImageDrawable(drawable);
After changes it should look like:
NOTICE: To show appName i used TextView
with android:text="@string/app_name"
value.
Hope it help
Upvotes: 2