2

I have a menu item in action bar. Which always looks small as in below image.

enter image description here

I want its size to be approx. double of this. I have put different sized icons in mdpi, hdpi, xhdpi folders. Thus, it is not a dpi related problem.

I have this issue in HTC One and emulator which is xhdpi. I tried enlarging icon in xhdpi folder and tested in emulator, but no change in its size in action bar. For micromax canvas2(hdpi) it works fine.

Is there any way to set menu item's size to fix height and width? Or any other workaround?

Community
  • 1
  • 1
Geek
  • 8,280
  • 17
  • 73
  • 137
  • can you put your xml code? – Sushil Oct 25 '13 at 08:46
  • @Sushil xml for menu item? OR whole layout? To make it more clear, this is the problem for all screen that has menu item in action bar. SO may be problem with menu item code only. – Geek Oct 25 '13 at 08:48
  • may be you can use a custom title bar and can control the size of menu items. In that case dont use menu itema at all. I have posted a custom titlebar code below. – Sushil Oct 25 '13 at 08:50
  • you could do something like this: set the menu item value or drawable, and then get the density dpi value and check if its xhdpi if so, set the width n the height – Rat-a-tat-a-tat Ratatouille Oct 25 '13 at 08:51
  • @DharaShah As mentioned in question, I did not find any way to set the size for menu item. – Geek Oct 25 '13 at 08:53
  • http://stackoverflow.com/questions/8825880/width-and-height-of-an-options-menu?answertab=votes#tab-top and [this](http://stackoverflow.com/questions/9278959/how-to-control-image-size-in-menu) – Rat-a-tat-a-tat Ratatouille Oct 25 '13 at 08:57
  • just randomz.. remove the mdpi folder and just test with the xhdpi folder, and find out why the emulator does nt pick the xhdpi folder , is it cz of the presence of other folders or wat. may b that cud lead to some useful info. – Rat-a-tat-a-tat Ratatouille Oct 25 '13 at 09:01
  • @DharaShah I'll try your suggestions and come back. – Geek Oct 26 '13 at 07:24
  • Just out of my curiosity: why dont you use an icon wich is styled like the Android guidelines? – JJ86 Oct 26 '13 at 10:33
  • @JaAd Will you please elaborate or post a link to guidelines you are talking about? – Geek Oct 26 '13 at 13:13
  • @Akash everything is on official Android site: 1) http://developer.android.com/guide/topics/ui/actionbar.html 2)http://developer.android.com/design/patterns/actionbar.html 3) http://developer.android.com/design/style/iconography.html – JJ86 Oct 26 '13 at 13:17
  • @JaAd I read all links. Will you tell me how my icon is not styled like android guidelines and causing me this issue? P.S. I didn't find any which is causing this issue. – Geek Oct 26 '13 at 16:42
  • @Akash if you want text next to the icon, use android:showAsAction="ifRoom|withText" to show the menu title along with the icon. Avoid to include on the "icon.png" text and add icons for every density: mdpi, hdpi, xhdpi, xxhdpi. – JJ86 Oct 28 '13 at 17:45
  • @JaAd That is not a text. Its a complete image. I already have icons for every density as mentioned in question. – Geek Oct 29 '13 at 05:48
  • @DharaShah I tried removing this icon from dpi folders except xxhdpi. Still it is displayed too small. – Geek Oct 29 '13 at 06:20
  • i dont know where i read this, the emulator does not have a support for xxhdpi.. i wonder if thats the case ?? Ignore this, if its completely wrong – Rat-a-tat-a-tat Ratatouille Oct 29 '13 at 07:26
  • @DharaShah But it is the case with xhdpi, too. :) – Geek Oct 29 '13 at 07:30
  • Create a nine patch images for all of your images and use them! – AndroidDev Oct 30 '13 at 13:38

4 Answers4

3

I have the same issue, and so far I have not found a solution. It is HTC specific and I observed this behavior on an HTC One X.

Their Sense framework forces a square menu icon, so it narrows your view. As a workaround, I didn't use the menu items api, I created a custom view for the action bar and aligned my imageview to the right (while preserving the title and home icon) using this answer: SherlockActionBar: How to adjust CustomView against actionBar

Community
  • 1
  • 1
1

May be you can use custom title bar:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/action_bar"
    android:orientation="horizontal" >

<LinearLayout
    android:id="@+id/slidingmenu"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:paddingLeft="20dip"
    android:layout_centerInParent="true" 
    android:layout_alignParentLeft="true">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/slidingmenu_selector" 
        android:duplicateParentState="true" />
 </LinearLayout>


<TextView
        android:id="@+id/myheading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="23dp" />

<LinearLayout
    android:id="@+id/homemenu"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:paddingRight="20dip"
    android:layout_centerInParent="true" 
    android:layout_alignParentRight="true">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/homemenu_selector" 
        android:duplicateParentState="true" />
 </LinearLayout>


</RelativeLayout>
Sushil
  • 8,250
  • 3
  • 39
  • 71
  • Can you please explain what is the first ImageView for and what is the second ImageView for? In addition, my action bar does not have fixed number of menu items. – Geek Oct 25 '13 at 08:55
  • I have a menu item on left side for opening a slider and a menu on right side for home button. – Sushil Oct 25 '13 at 08:56
  • Ok. Will the ImageView respond to click? – Geek Oct 25 '13 at 09:01
  • yes it does. It is normal imageview and you can have a clicklistener for it. – Sushil Oct 25 '13 at 09:03
  • Then it will not have effect for user click event. But more concerning part is that why am I not able to use action bar? What is the issue with me? If it is the action bar then others should have this problem, too? And I may not want to use this workaround in my all app because SDK has already implemented this and I will be redoing it. – Geek Oct 26 '13 at 05:23
1

Put the image in drawable-nodpi folder

Naresh
  • 3,174
  • 1
  • 17
  • 22
0

I solved this issue by using custom view for menuitem.

Layout for menu item :

    <ImageButton
        android:id="@+id/first_menu_item"
        android:layout_width="56dp"
        android:layout_height="33dp"
        android:layout_gravity="center"
        android:contentDescription=""
        android:background="@drawable/menu_item_background"
        android:gravity="center" />

</LinearLayout>

Set custom view for actionbar in activity :

// Set custom menu items for action bar
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                            | Gravity.CENTER_VERTICAL);   // Set Layout Parameters
lp.setMargins(5, 5, 10, 5); // Left, top, right, bottom margins
View view = LayoutInflater.from(this).inflate(
                    R.layout.actionbar_menu_item, null);
ImageButton menuItem = (ImageButton) view
                    .findViewById(R.id.first_menu_item);
menuItem.setImageDrawable(getResources().getDrawable(
                    R.drawable.edit_button));
menuItem.setOnClickListener(this);
actionBar.setCustomView(view, lp);
actionBar.setDisplayShowCustomEnabled(true);
Geek
  • 8,280
  • 17
  • 73
  • 137