Reputation: 1299
I am getting problem in adding icon with text in the toolbar.I have tried the following code:
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_new_group, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.createGroup:
createNewGroup();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_new_group.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/createGroup"
android:icon="@drawable/single_tick"
android:orderInCategory="100"
android:title="Create"
app:showAsAction="always|withText" />
</menu>
Only single tick is displayed in the toolbar .Title is not displayed. Please help .
Updated code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/createGroup"
android:icon="@drawable/single_tick"
android:orderInCategory="100"
android:showAsAction="withText|always"
android:title="Create" />
</menu>
Still not working for me.Please view the following screenshot:
Icon is not displayed here.I want icon and text on the toolbar.
activity_new_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBarNewGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/toolbar_color"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/frame"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_gravity="center"
android:layout_marginTop="@dimen/margin10">
<com.almabay.almachat.circularImageView.CircularImageView
android:id="@+id/groupPic"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_gravity="bottom|center_horizontal" />
<com.almabay.almachat.circularImageView.CircularImageView
android:id="@+id/iv_camera_group"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="top|right"
android:layout_marginTop="@dimen/margin30"
android:background="@drawable/color"
android:src="@drawable/camera1" />
</FrameLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/margin10"
android:layout_marginTop="@dimen/margin20"
android:background="@drawable/edittext_bottom_border_orange"
android:hint="Type group subject here ..."
android:paddingBottom="@dimen/padding10" />
<ListView
android:id="@+id/lv_friends"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
Upvotes: 2
Views: 10916
Reputation: 141
I have resolved this problem by following code:
1- Create menu in menu folder
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item
android:id="@+id/action_sign"
android:title="签到"
app:showAsAction="always|withText"
app:actionLayout="@layout/menu_sign" />
2- app:actionLayout
is the key word, Then create layout named menu_sign and put textView in it. the icon will displayed by drawableLeft
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:drawableLeft="@mipmap/ic_user_sign"
android:gravity="center"
android:orientation="vertical"
android:text="@string/text"
android:drawablePadding="@dimen/margin_5"
android:paddingLeft="@dimen/margin_10"
android:paddingRight="@dimen/margin_10"
android:textColor="@android:color/white" />
3- Add listener for your menu
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_sign, menu);
signItem = menu.findItem(R.id.action_sign);
signItem.getActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onOptionsItemSelected(signItem);
}
});
4- Then you will get the menu and listener
Upvotes: 14
Reputation: 5953
You can wrap TextView
with Toolbar as follows to fully Customize it. With TextView you can use drawableLeft
or drawlableRight
to specify the icon.
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ToolBarStyle">
<TextView
android:id="@+id/spinner_nav"
android:drawableLeft="Your_Draable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
</android.support.v7.widget.Toolbar>
Upvotes: 4