I am trying to make a round Android ImageButton. To do so, I write the following code according to the link.
In my main.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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:textColor="#333"
android:textSize="18sp"
android:text="Invite Activity." />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:src="#drawable/round"
android:background="#drawable/roundcorner"
android:padding="50dp" />
</LinearLayout>
In my res/drawable/roundcorner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#33DDFF" />
<corners android:radius="100dp" />
</shape>
But the code doesn't work. The output is like below
However, when I change ImageButton to ImageView in main.xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:src="#drawable/round"
android:background="#drawable/roundcorner"
android:padding="50dp" />
It works as below
I am confused since as I know ImageButton inherits from ImageView. Why does it work differently? Is there anyway to fix this? Thanks in advance!
Make your src as android:src="#drawable/roundcorner", or make it transparent so you can see the rounded background behind it.
Related
code:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:gravity="center"
android:text="H" />
#drawable/background is a 9.png image.
If change to a common png image, gravity works.
How can i fix this? I want to make text in center.
First you have to create a drawable (drwable_example.xml) which loads the 9.png file:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="fill_vertical|fill_horizontal"
android:src="#drawable/background" >
</bitmap>
</item>
</layer-list>
then you can load this as background:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:background="#drawable/drawable_example"
>
It worked for me, hope it helps.
Not fixed yet. But change to below code:
#drawable/background is a 9.png image.
<RelativeLayout
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_margin="3dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background"/>
<TextView
android:id="#+id/typeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="test"
android:textColor="#color/colorPrimary" />
</RelativeLayout>
How to draw button like below image in xml? Basically, it has two different buttons. One is for I. another one is for Pick Up. Thanks.
Try this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/bg_btn_coner"
android:gravity="center"
android:text="Pickup"
android:textColor="#android:color/white"
android:textSize="20sp" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/bg_btn_coner"
android:gravity="center"
android:text="i"
android:textColor="#android:color/white"
android:textSize="20sp" />
</RelativeLayout>
and bg_btn_coner.xml
<solid android:color="#737373" />
<stroke
android:width="3dp"
android:color="#android:color/white"></stroke>
<corners android:radius="50dp"></corners>
Result:
Try this. Example image is shown.
Note: you will have to fix the path to make the image perfect.
button_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#android:color/darker_gray" />
</shape>
button_rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="720dp"
android:height="576dp"
android:viewportWidth="720"
android:viewportHeight="576">
<path
android:fillColor="#android:color/darker_gray"
android:pathData="M700.461,426.359c0,51.164-7.764,92.641-17.336,92.641H76.369
c-9.575,0,17.191-101.692,17.191-152.855l-1.444-173.699c0-51.164-25.321-136.646-15.747-136.646h606.756
c9.572,0,17.336,41.476,17.336,92.64V426.359z" />
</vector>
You will have to fix the vector to make the shape perfect. but this a good example for u.
my_layout.xml:
<?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="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="42dp"
android:background="#drawable/button_rectangle"/>
<Button
android:layout_width="42dp"
android:layout_height="42dp"
android:background="#drawable/button_circle"/>
</RelativeLayout>
Hope this helps!
I am planning to achieve something like this:
---------------Text---------------
where "-----" is actually a line ,(view line perhaps? or a divider) .
more accurately, this is an example of what i am trying to achieve:
I was wondering how do I achieve that in my textview. here's my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/mydate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/darker_gray"
android:textSize="18sp"/>
</RelativeLayout>
You can do this by creating a single, full-width line, and then putting the TextView on top of it. If you give the TextView the same background color as the parent's background, and give the TextView some left and right padding, it will look like you example.
You can use this for the line (line.xml in your res/drawable directory) :
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line"
>
<stroke
android:color="#android:color/darker_gray"
android:width="1dp"
/>
</shape>
and then use this layout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="10dp"
android:src="#drawable/line"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/mydate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="#android:color/white"
android:textColor="#android:color/darker_gray"
/>
</FrameLayout>
I'm creating a custom list item in and android list view. it's very simple, it has a textview on the left and an image on the right. the image should align all the way right, and the text view should take up all the space on the left. problem is, i can get the image to show up, but it pushes left. if i set the textview width to fill_parent, the image disappears. here's my layout:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="50px" android:layout_gravity="center_vertical">
<!-- item -->
<TextView android:id="#+id/txtItem" android:gravity="center_vertical" android:textSize="20px" android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!-- disclosure image -->
<ImageView android:layout_gravity="right|center_vertical" android:src="#drawable/common_icon_arrow_disclosure"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
i've also tried a relative layout, but the same thing happens. what am i doing wrong?
EDIT: for clarification, this is what i'm trying to accomplish:
i want the text field to take up the whole left area, and the image to be on the right, unscaled, vertically centered.
You can also just add the drawable to the textview directly like this.
<TextView
android:id="#+id/txtItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawableRight="#drawable/common_icon_arrow_disclosure"
android:gravity="center_vertical"
android:textSize="20px" />
You can use bitmap drawable like this (this also adds rounded corners):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#android:color/white" />
<stroke
android:width="2dp"
android:color="#adadad"
/>
<corners android:radius="5dp" />
</shape>
</item>
<item android:right="5dp"
android:top="2dp"
android:bottom="2dp">
<bitmap
android:src="#drawable/image"
android:gravity="right"
/>
</item>
</layer-list>
and put this drawable in background property of your TextView:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/right_image"
android:text="some text"
/>
Left-align your text with the parent, and set the gravity of the image to right:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="48dp">
<TextView
android:id="#+id/team_member_name"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:singleLine="true"/>
<ImageView
android:id="#+id/team_member_icon"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:src="#drawable/circle_green"/>
</RelativeLayout>
Relative layout is what you want. In your case the fill_parent attribute was causing the text view to push the image off of the screen. If you use Relative layout and set the text view to use the layout_toLeftOf it should work.
Here is a quick example that I think will work (I haven't tested it out and it's from memory but the idea is there)...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50px"
android:layout_gravity="center_vertical">
<ImageView android:id="#+id/img"
android:layout_gravity="right|center_vertical"
android:src="#drawable/common_icon_arrow_disclosure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/txtItem"
android:gravity="center_vertical"
android:textSize="20px"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toLeftOf="#id/img" />
</RelativeLayout>
This worked for me:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dp"
android:layout_marginRight="10dp"
android:textSize="16sp"
android:textColor="#color/text_color"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"/>
<ImageView android:id="#+id/imageBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dp"
android:contentDescription="#string/delete"
android:src="#drawable/delete_minus_png"
android:layout_alignParentRight="true" />
</RelativeLayout>
Line android:layout_marginRight="10dp" did the trick.
Hope it helps!
Looks like there is some layout bug or so. If the TextView has fill_parent flag, the ImageView will be pushed away from the layout, even if the TextView contents are small enough.
When I created a button view, Android always create some extra space between this button and other views below it.
In this example below, there is a button above the second button. You can see the gap between these two buttons. How can I get rid of this gap? Thanks.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/up"
android:layout_width="fill_parent" android:layout_height="28dip"
android:textSize="9sp" android:gravity="center" android:text="Hide sth"
android:layout_marginBottom="0" android:paddingBottom="0" />
<Button android:id="#+id/down"
android:layout_width="fill_parent" android:layout_height="28dip"
android:textSize="9sp" android:gravity="center" android:text="Show sth" />
</LinearLayout>
Create your own button background nine-patch PNG that eliminates the space. You can find the existing background files in your SDK.
CommonsWare pretty much pointed you in the right direction but here's a sample layout example. Just replace the android:background="COLOR" to a reference of a 9 patch png
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn1"
android:text="btn1"
android:padding="0dip"
android:layout_margin="0dip"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:background="#FFF"
/>
<Button
android:id="#+id/btn2"
android:text="btn2"
android:padding="0dip"
android:layout_margin="0dip"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:background="#FF0000"
/>
</LinearLayout>
Hope that helps!
I just figured out the most simple way. We have to simply add background to Buttons and it will automatically reduce spaces.
Notice example
<Button
android:id="#+id/button1"
android:text="Button 1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000" />
<Button
android:id="#+id/button2"
android:text="Button 2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000" />
Obviously we would have to split Buttons with border line. There are few ways for that.
1. android:layout_marginTop="1dp" to second Button. The small piece of background will separate it.
2. Add style for Button :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#000000"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="1px" android:color="#FFFFFF" />
</shape>
and set it with :
android:background="#drawable/your_button_border"
3. To add View with background of a different color and place it between Buttons.
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#AAA" />
Just pick the most convenient for you way.