Android ImageButton src overwriting background - android

I'm trying to create a rounded image button using the code below which works when you don't declare a src image. However when you do, it just places the image in but without the rounding effect on it.
style.xml
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="#+id/imageButton1"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:src="#drawable/ellyfish"
android:background="#drawable/roundcorner"
/>
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="50dp" />
</shape>

Make your src image round, or make it transparent so you can see the rounded background behind it.

Related

Background image in TextView showing up transparent

I have a circle png that I put as a background image in a TextView. However, the image shows up transparent even when I tried setting the alpha level to "1". Is there something wrong I did in my XML code?
<TextView
android:id="#+id/timeline_text_details_img"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:background="#drawable/circle"
android:gravity="center"
android:textColor="#fff"
android:textSize="14dp"
android:alpha="1"
android:textStyle="bold" />
The circle png I want needs to be thick white like how it originally is
but instead it looks like this when I run it
You can simply create a shape circle.xml and place it in drawable.
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="2dp"
android:color="#android:color/white"
/>
<solid
android:color="#000000"/>
</shape>
You can change the color scheme as you like.

How to create a circular button in android with image in the background?

I want to create a circular button in android. This the xml I am using:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#9F2200"/>
<stroke android:width="2dp" android:color="#fff" />
</shape>
Now when I put a button in the layout, I will have to set the above xml as the
android:background
attribute. But then how can I put an image within the circular button I have generated. How to accomplish this within the xml itself?
You can do this using FrameLayout
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="your selector" >
<Button
android:id="#+id/fbLogin"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="yourimage"
android:gravity="center"/>
</FrameLayout>
If I got it you can use an ImageButton, which lets you choose both a background with android:background and a drawable resource with android:src.
Use an ImageButton and set your image with the src tag: android:src="#drawable/your_image"

Make ImageView or its parent circular from left side

I have a very basic layout as follows. I'm just using a red background in the ImageView just to depict the situation.
<?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="wrap_content"
android:background="#drawable/custom_bg_grey_stroke_radius"
android:orientation="vertical" >
<ImageView
android:id="#+id/IV"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="#ff0000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:layout_toRightOf="#+id/IV" />
</RelativeLayout>
This is the custom background I'm using:
custom_bg_grey_stroke_radius.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- background color of the entire rectangle -->
<solid android:color="#FFFFFF" />
<!-- color of borders -->
<stroke
android:width="1dp"
android:color="#c1c1c1" />
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />
</shape>
I found various links which make the images circular but from all the 4 corners.
This link tells how to make image circular from top or bottom.
Android round a Layouts background image, only top or bottom corners
Can someone please figure out how to make Image circular from the left.
It would be good if I could specify the exact radius. I want it just barely circular around 5dp.
And, I apologize for the repetitiveness, but I'm just not able to work this out.

Android put left border line on a image in xml Drawable

I need to put a orange line in the left side of a imageview. I put in "the hard way" and it works:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dp" >
<LinearLayout
android:layout_width="5dp"
android:layout_height="fill_parent"
android:background="#FF7F24" >
</LinearLayout>
<ImageView
android:id="#+id/image1"
android:layout_width="50dp"
android:layout_height="60dp"
/>
</LinearLayout>
There is a way to do this effect using a drawable xml file in the image directly without using the linearlayouts?
Thanks for your help!
Use this below code as a drawable to imageview
<item>
<shape android:shape="rectangle" >
<solid android:color="#FF7F24" />
</shape>
</item>
<item android:left="5dp">
<bitmap android:shape="rectangle"
android:src="#drawable/image"/>
</item>
You can always create a shape and set it as the background of your LinearLayout instead of "#FF7F24".
But since you only want a solid color, I think the way you implemented it is just fine

Setting rounded corners and image to a button

I am trying to make button'corners round, as well as setting a background image. Though I was able to achieve button with rounded corners, but not to set the image as the background. When I try android:drawableTop:#drawable/filename, the button is again converted to its default shape and text goes below the image, but I want the image as the background, so how to apply both the things(rounded corner and background image) to the button?
I have created an xml file named button_shape in drawable; the code for it is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#ffffff"
/>
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
<stroke android:width="3dp" android:color="#000000" />
</shape>
and the main xml file code where I have put the button is:
<Button
android:id="#+id/button1"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginTop="179dp"
android:background="#drawable/button_shape"
android:text="Button"
android:textColor="#000000"
/>
Please help me with this.
use this in shape
solid android:color="#android:color/transparent" />
and imageview should be
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:scaleType="fitXY" android:background="#drawable/shape"
android:src="#drawable/yourimage" />
Use this background.xml put in Drowable and set on Button android:background="#drawable/ background" and ovel_shape is xml file in drawable where you can put code for rounded corners
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/rounded_corn"
android:drawable="#drawable/ovel_shape"/>
<item
android:id="#+id/background_img"
android:drawable="#drawable/image"/>
</layer-list>

Categories

Resources