How to set an icon at the end of Toolbar - android

I want to set an icon at the the end of my Toolbar,which start another activity.
My Toolbar portion
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:background="#2B4AE0"
app:theme="#style/ToolBarStyle">
<TextView
android:id="#+id/headerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.Theme"
android:textColor="#android:color/white" />
<RelativeLayout
android:id="#+id/notification"
android:layout_width="50dp"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center" />
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/bell_icon" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
I tried
android:layout_alignParentEnd="true"
and setting margin left but it doesn't work correctly.

Try adding this to your ImageView:
android:layout_gravity="end"

If you want something like this (icon 2)
You do not need add icon to the layout manually, you should to implement menu.xml
Create menu.xml like this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_activity"
android:icon="#drawable/ic_custom_icon"
android:title="#string/new_activity" />
</menu>
Add it in your activity/fragment to the actionbar/toolbar by
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
Handle menu items events
Toolbar with menus works fine, it sets icons to the right of toolbar automatically

If there's anyone that still confused (including me before), somehow android studio doesn't provide autocomplete for the android:layout_gravity under the toolbar tag. So to make it works, just copy and paste android:layout_gravity="end" to the ImageView/layout.

Setting the orderInCategory will set the order of the menu item
<item
...
android:orderInCategory="150"
app:showAsAction="always"
..
/>
if you want to put margin between your menu items you can add an empty menu item in between

<item
android:title="kjljk"
app:showAsAction="always"
android:id="#+id/menuitem_search"
android:icon="#drawable/pdficon"
>
</item>
Then in OnCreateView:
setHasOptionsMenu(true);
Closing the OncreateView below write this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.pdf, menu);
...
}

Related

How to get Textview reference using butterknif in android

Hi in my layout there is I have included sub layout using ButterKnife in Android. How can I get TextView reference my code is below. Same thing, I want to get with help of ButterKnife, can someone help me please...
gmail_view.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/gmail_item"
android:title="gmail_count"
app:actionLayout="#layout/badge_layout"
app:showAsAction="always">
</item>
</menu>
badge_layout:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center"
android:clickable="true"
style="#android:style/Widget.ActionButton">
<ImageView
android:id="#+id/gmail_icon"
android:src="#drawable/ic_gmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="0dp"
android:contentDescription="bell" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gmail_textview"
android:layout_width="wrap_content"
android:minWidth="17sp"
android:textSize="12sp"
android:textColor="#ffffffff"
android:layout_height="wrap_content"
android:gravity="center"
android:text="10"
android:layout_alignTop="#id/gmail_icon"
android:layout_alignRight="#id/gmail_icon"
android:layout_marginRight="0dp"
android:layout_marginTop="3dp"
android:paddingBottom="1dp"
android:paddingRight="4dp"
android:paddingLeft="4dp"
android:background="#drawable/bage_circle"/>
</RelativeLayout>
Activity:-
MenuItem menuItem = menu.findItem(R.id.gmail_item);
RelativeLayout badgeLayout = (RelativeLayout) menuItem.getActionView();
gmail_text = (TextView) badgeLayout.findViewById(R.id.gmail_textview);
#Bind(R.id.btnid_given_inlayout)
Button btn;
#Bind(R,id.LAYOUTID)
RelativeLayout layout;
in oncreate u need bo bind the views...
so after setting content
write the below line
ButterKnife.bind(this);
in code...u can refer like...
btn.setOnclickListener.
layout.setVisibility();
if it doesnt answer ur question ,u can post ur xml
Try the following code -
gmail_text = (TextView) ButterKnife.findById(badgeLayout, R.id.gmail_textview);
action_save:
<?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/action_save1"
android:orderInCategory="100"
android:title="Save"
app:showAsAction="always" />
in public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
inflater.inflate(R.menu.action_save, menu);
menu.findItem(R.id.action_save1);
UPDATE:
ButterKnife won't supporting binding MenuItem, as in the ButterKnife issue #41.
No, you can't bind the menu layout view because ButterKnife only binds the layout in activity onCreate or fragment onCreateView method. ButterKnife will complains about not founding the view because the Menu is inflated after onCreate() which is in onCreateOptionsMenu like the following code:
#Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_menu, menu);
return true;
}
Here the diagram that shows the Activity lifecyle diagram from android-lifecycle:
So you can't bind the view with ButterKnife.
CMIIW

Tollbar with a notification count

Hello I'm using a Tollbar and inside I added some drawable items in the menu options. What I want is to put a number inside those drawable. Like the image below
View post on imgur.com
I know there are other post related to this but not with a Toolbar and I need to is a Toolbar.
Thank you
The simpliest way is to use TextView with star background:
<TextView
android:id="#+id/notification"
android:layout_width="#dimen/notification_width"
android:layout_height="#dimen/notification_height"
android:background="#drawable/bg_star"
android:gravity="center"
android:textColor="#android:color/white"
tools:text="5" />
You can use obvious TextView's setText() method to set or hide notification text.
To add this layout to your Toolbar use Android menu's app:actionLayout parameter:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/action_notifications"
app:actionLayout="#layout/view_notification"
app:showAsAction="always" />
</menu>
And in your Activity inflate menu like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.notification_menu, menu);
return true;
}
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/action_bar_bkgnd"
app:theme="#style/ToolBarTheme" >
<TextView
android:background="#drawable/bg_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:id="#+id/notification"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);

android menu item text and auto caps disable

Currently getting menu item text color back and caps letter for lollipop devices
<?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/menu_list"
android:title="#string/list"
android:actionMenuTextColor ="#color/paytm_blue"
app:showAsAction="always"
/>
</menu>
To achieve : item Text color in blue and caps disabled
Thank you in advance
Add the following to one of your values xml files -
<bool name="abc_config_actionMenuItemAllCaps">false</bool>
You can set custom layout for menu item and can set all required things in this custom file.
First create one xml layout file named customemenu.xml
customemenu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW ALL "
android:id="#+id/tvall"
android:textColor="#color/white"
android:textAllCaps="false"
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginLeft="5dp"
/>
</LinearLayout>
</LinearLayout>
Override onCreateOptionsMenu() like this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_search_res, menu);
MenuItem item = menu.findItem(R.id.menu_list);
item.setActionView(R.layout.customemenu);
super.onCreateOptionsMenu(menu, inflater);
}

What parameters to use with android inflater

I have created a listview using an online tutorial where I am trying to create a multicolumned listview. All tutorials have told me I need an inflater but none show what to put or should be in the corresponding xml file. Could anyone please shed some light on this.
I'm setting the listview in the oncreate method. I have the following method with an inflater:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
But there is nothing in the menu_main.xml file.
Because it's multicolumned I have the listview in the main xml file like follows:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/currentChores">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/choreList"
android:textSize="20dp"
android:typeface="sans"
android:layout_centerHorizontal="true"
android:layout_below="#+id/currentChores"
android:paddingBottom="10dp"
android:paddingTop="50dp">
</ListView>
</LinearLayout>
And then I made a seperate xml file for the columns called chore_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/chore_name"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:text="Chore"
android:typeface="sans"
android:textColor="#000000"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/child_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Child"
android:typeface="sans"
android:textColor="#000000"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/point_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Points"
android:typeface="sans"
android:textColor="#000000"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
And I'm trying to create this list view using the following line:
mySimpleAdapter = new SimpleAdapter(Chores.this, chores, R.layout.chore_list, new String[]{"chore","child_name","points"},new int[]{R.id.chore_name,R.id.child_name,R.id.point_value });
list.setAdapter(mySimpleAdapter);
When I run this I can scroll further down the page which is telling me there is something there but I can't see anything
Your piece of code creates Menu not a ListView
Menus info: http://developer.android.com/guide/topics/ui/menus.html
ListView info: http://developer.android.com/guide/topics/ui/layout/listview.html
You have to use inflater like this
private LayoutInflater inflater;
inflater = LayoutInflater.from(context);
The method onCreateOptionsMenu is used to inflate menu,not ListView.If you want to custom views of each row for your ListView,you should create a Adapter class which extends BaseAdapter and implement its getView method,and in this method inflate your custom xml layout for you view of each row.
There is a article about this:https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html.
EDIT:After looking at your newest question,I think your problem is because your data source has no data,that means chores is empty.When you create a SimpleAdapter,the second parameter is your data source,and its key must corresponding to the string array behind this parameter.
onCreateOptionsMenu method :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return super.onCreateOptionsMenu(menu);
}
And your item in corresponding xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
Menu
Defines a Menu, which is a container for menu items. A <menu> element
must be the root node for the file and can hold one or more <item> and
<group> elements.
Item
Creates a MenuItem, which represents a single item in a menu. This
element may contain a nested <menu> element in order to create a
submenu.
Group
An optional, invisible container for <item> elements. It allows you to
categorize menu items so they share properties such as active state
and visibility. For more information, see the section about Creating
Menu Groups.
Please visit http://developer.android.com/guide/topics/ui/menus.html

How to add material design library switch in the action bar

How to insert com.gc.materialdesign.views.Switch in the Actionbar using MenuInflater??
Below is the code for inflating the android default switch.
This is my onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.myswitch);
switchButton = (Switch)item.getActionView();
}
This is my menu_main.xml
<menu
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/myswitch"
android:title="off/on"
app:showAsAction="always"
app:actionLayout="#layout/switchlayout"
app:actionViewClass="android.widget.Switch"
/>
</menu>
This is the switch layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Switch
android:layout_centerVertical="true"
android:id="#+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="off/on toasts" />
</RelativeLayout>
Any help is appreciated!
Replace your Switch code with below code
<com.gc.materialdesign.views.Switch
android:id="#+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#1E88E5" />
and Replace item code with
<item
android:id="#+id/menu_switch"
android:title="off/on"
app:showAsAction="always"
app:actionLayout="#layout/switchlayout"/>
Try replacing <Switch in your switchlayout.xml with <com.gc.materialdesign.views.Switch. Also, you don't need to specify app:actionViewClass since you're already providing a layout.

Categories

Resources