android menu item text and auto caps disable - android

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);
}

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

Break line after Icon in Menu Item Android

Simple question that I don't find an answer for.
Is it possible to break line after the icon in a menu item making them not inline anymore? (The menu slides in from left by using NavigationView in Android support library)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/cards"
android:checked="false"
android:icon="#drawable/card_icon"
android:title="Cards" />
...
...
...
</group>
This is how I want it to look like.
But instead they are aligned inline.
Update after answer:
Using a modified android.R.layout.activity_list_item (changed to vertical aligned left):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="1dip"
android:paddingBottom="1dip"
android:paddingStart="8dip"
android:paddingEnd="8dip"
android:orientation="vertical">
<TextView android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_horizontal"
android:paddingStart="?android:attr/listPreferredItemPaddingStart" />
<ImageView android:id="#+id/icon"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_gravity="left" />
</LinearLayout>
And I added an actionLayout to the menu items:
<item
android:id="#+id/cards"
android:checked="false"
android:icon="#drawable/cards_icon"
android:actionLayout="#layout/vertical_menu_item"
android:title="Cards" />
But nothing happens. They are still inline. I also tried using app:showAsAction="always" and app:actionLayout="#layout/vertical_menu_item".
I suggest you to create a custom view (eg: vertical layout with ImageView and TextView), and in onCreateOptionsMenu() you can use setActionView() method of the MenuItem to use this custom view as Item view.
Or from menu xml:
android:actionLayout="#[package:]layout/layout_resource_name"

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.

Touch feedback on menu item with actionLayout

I'm using a menu item with actionLayout because I need to add a textView along with my icon for the item on the ActionBar. everything shows up correctly but I don't know (and couldn't find anywhere) how to add the animation that shows up when user taps on a menu item on Lollipop devices.
I would like to show number of comments on the side of comment icon in my toolbar.
Here is what I have:
<?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_like"
android:icon="#drawable/list_discuss_like"
android:title="Like"
app:showAsAction="always"/>
<item
android:id="#+id/menu_comment"
app:actionLayout="#layout/menu_item_comment"
android:title="Comment"
android:icon="#drawable/ic_comment_white_24dp"
app:showAsAction="always"/>
</menu>
and for my menu_item_comment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:hapticFeedbackEnabled="true"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="#drawable/ic_comment_white_24dp"/>
<TextView
android:id="#+id/ab_comment_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="(12)"
android:textColor="#color/grayTextHint"/>
</LinearLayout>
The animation and feedback works on the like item but not on the comment one.
Any help is appreciated.
Set the clickable element's background to the theme's action bar item background.
<LinearLayout
...
android:clickable="true"
android:background="?android:attr/actionBarItemBackground">
...
</LinearLayout>

How to set an icon at the end of Toolbar

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);
...
}

Categories

Resources