tags:

views:

93

answers:

2

I am trying to add a badge to one of my Activity's button. Right now I am trying to do the xml. The Button with the badge, should look like this:

alt text

I have already done the bubble and the text inside using a RelativeLayout:

<RelativeLayout android:layout_width="wrap_content" 
            android:layout_height="wrap_content">

            <ImageView android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/badge"
                android:layout_centerInParent="true"/>

             <TextView android:layout_width="wrap_content" 
                       android:layout_height="wrap_content"
                       android:textStyle="bold"
                       android:text="2"
                       android:layout_centerInParent="true" />
         </RelativeLayout>

But I can't find a way to place it there and making it work on portrait and landscape with the same xml.

The Buttoms in the Activity are like this:

<Button android:id="@+id/new_releases_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_button_selector"
            android:text="@string/new_releases_title"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:gravity="center_vertical"
            android:paddingLeft="12dp"
            android:layout_marginTop="15dp"
            android:layout_below="@id/coming_soon_button"
            android:onClick="newReleaseClick"
            android:layout_centerHorizontal="true" />

        <Button android:id="@+id/top_sellers_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_button_selector"
            android:text="@string/top_sellers_title"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:gravity="center_vertical"
            android:paddingLeft="12dp"
            android:layout_marginTop="15dp"
            android:layout_below="@id/new_releases_button"
            android:onClick="topSellersClick"
            android:layout_centerHorizontal="true" />

and here are the two resources:

alt text alt text

How should I do the xml?

EDIT: Best approach so far, but it still doesn't work:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button android:id="@+id/discounts_button"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:background="@drawable/ic_button_selector"
                android:text="@string/discounts_title"
                android:textColor="#FFFFFF"
                android:textSize="20sp"
                android:onClick="discountsClick"
                android:layout_marginTop="40dp"
                android:layout_marginLeft="20dp"/>

        <RelativeLayout android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_gravity="top|left">

            <ImageView android:layout_width="wrap_content" 
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:src="@drawable/badge"
                    android:layout_centerInParent="true"/>

             <TextView android:layout_width="wrap_content" 
                           android:layout_height="wrap_content"
                           android:textStyle="bold"
                           android:text="20"
                           android:layout_centerInParent="true" />
         </RelativeLayout>
</FrameLayout>
+1  A: 

Use a FrameLayout (instead of RelativeLayout) and put button and image into it.

Position the image (cirle with number) and button via

android:layout_gravity="top|left"
android:layout_marginTop="Xdp"
android:layout_marginLeft="Xdp"

to your likes.

Mathias Lin
This looks like the correct way but I still can't make it work.It makes sense to add `marginTop` and `marginLeft` to the badge, but they should be negative. This position it outside the view. So instead, I added positive `marginTop` and `marginLeft` to the button, but those values are ignore. I yet couldn't understand why.I will edit the question with the best approach so far.
Macarse
yes, correct. no negative margin to the image (although i haven't tried if that would work as well), you can work with the top/left margins for both image and button as you like. but it does make sense this way: the upper left hand position of the FrameLayout is the upper left corner of the image, therefore you need to move the button a bit to the right and down.
Mathias Lin
Cool, adding `android:layout_gravity="top|left"` to the Button made it work, but it cuts the button since my width and height from my parent are "wrap_content". I don't want to force something like `android:layout_height="60dp"` which looks good. Any idea of a better way to solve this?
Macarse
not sure why it would cut the button's height just b/c of wrap_content. adding a paddingBottom maybe? btw: i noticed you set the layout_width of the frameLayout to fill_parent, I think wrap_content would also be sufficient here; but shouldn't solve the issue though.
Mathias Lin
A: 

I'd say that should be easy with FrameLayout:

<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
     android:layout_centerHorizontal="true" android:layout_marginTop="15dp">
      <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="15dip">
       <Button android:id="@+id/new_releases_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_button_selector"
        android:text="@string/new_releases_title"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:gravity="center_vertical"
        android:paddingLeft="12dp"
        android:layout_below="@id/coming_soon_button"
        android:onClick="newReleaseClick"/>
     </FrameLayout>
     <RelativeLayout android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
        <ImageView android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/badge"
            android:layout_centerInParent="true"/>
         <TextView android:layout_width="wrap_content" 
                   android:layout_height="wrap_content"
                   android:textStyle="bold"
                   android:text="2"
                   android:layout_centerInParent="true" />
     </RelativeLayout>
</FrameLayout>

Probably you will need to adjust margins.

EDIT:

Adding the android:layout_gravity="top|left" will look like this:

alt text

Konstantin Burov
This didn't work for me. The badge is seen on top of the button, and the `android:layout_marginTop` looks it's ignore somehow.
Macarse
Weird.. then try updated xml on the answer (you may want to adjust padding of internal FrameLayout). Also try using RelativeLayout as top-level container instead of FrameLayout.
Konstantin Burov
does adding android:layout_gravity="top|center" to both button and RelativeLayout make any difference? code looks ok so far at first sight.
Mathias Lin
I just added the output of that code with `android:layout_gravity="top|left"`
Macarse
a FrameLayout in a FrameLayout? That's strange, not necessary@Macarse, is the code in your initial question / edit part the very recent one?For the button cut off - how about adding the same amount of margin to right/bottom that you added to the left/top - just a rough guess?
Mathias Lin