I want to make a button with a letter inside a circle, without using a picture.
My button is defined like this:
<Button
android:id="#+id/zoomInButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="#drawable/circle"
android:text="+"
android:textColor="#color/white"
android:textSize="62sp" />
<Button
android:id="#+id/zoomOutButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="#drawable/circle"
android:text="-"
android:textColor="#color/white"
android:textSize="62sp" />
And with circle being:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<stroke
android:width="3dp"
android:color="#color/white" />
<solid android:color="#android:color/transparent" />
</shape>
However, the circle is not centred and the text is cropped:
How to center the background on the text?
Try to reduce textSize of those Buttons' because there is not enough room for 62sp thats why the texts are cropped.
Related
I created a circular shape called circle.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#FFF" />
<size
android:width="5dp"
android:height="5dp"/>
<padding
android:left="5dip"
android:right="5dip"
android:top="5dip"
android:bottom="5dip" />
</shape>
In the preview it looks circular and not oval. Then I implemented it in my text view:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/addToCartBtn"
android:layout_width="match_parent"
android:text="#string/cart_button_text"
android:textColor="#FFF"
android:background="#color/colorPrimary"
android:layout_height="65dp"/>
<TextView
android:id="#+id/cartItemsCountTV"
android:elevation="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_alignStart="#id/addToCartBtn"
android:text="1"
android:textSize="16sp"
android:textStyle="bold"
android:background="#drawable/circle"/>
</RelativeLayout>
Then the result is still an oval shape:
The shape only becomes circular when the text is two digits such as "10". Why is it oval even though it should be circular?
Use same layout_width and layout_height to keep it as circle instead of wrap_content.
<TextView
....
android:layout_width="40dp"
android:layout_height="40dp"
..../>
Using a View's size to wrap_content will force it to expand only far
enough to contain the values (or child controls) it contains.
I need to do this effect on a layout like the following :
I tried with gradient on a shape, shadows, https://developer.android.com/reference/android/graphics/drawable/NinePatchDrawable , https://developer.android.com/reference/android/graphics/BlurMaskFilter
or even some libraries: https://github.com/SanojPunchihewa/GlowButton
But it always depends on a bitmap or on a button
What I have for the moment is my drawable and layout :
Shape drawable with strokes :
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="22.5dp"/>
<padding android:left="3dp" android:right="3dp" android:top="3dp" android:bottom="3dp"/>
<stroke android:color="#color/colorAccent" android:width="1dp"/>
<solid android:color="#android:color/transparent"/>
</shape>
Layout :
<LinearLayout
android:id="#+id/text_selections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="#drawable/btn_rounded_accent_light_stroke"
android:gravity="center"
android:layout_gravity="center"
android:orientation="horizontal"
android:tag="perso_button_rounded_accent_light">
<TextView
android:id="#+id/text_selections_line_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:tag="perso_text_view_accent"
android:text="Ma Commande"
android:layout_marginRight="5dp"
android:textColor="#color/colorAccent"
android:textSize="17sp" />
<Button
android:layout_width="25dp"
android:layout_height="25dp"
android:text="5"
android:textColor="#color/colorBlueLight"
android:background="#drawable/bg_rounded_button"
android:shadowRadius="90">
</Button>
</LinearLayout>
The library is rendering like following which is pixelating
Do you have an idea on how to do this effect ? (I want to do it programmatically, not using an image)
Thanks in advance
I trying to make a circular image button like WhatsApp.
I tried the below code, but I am only able to create a circular imageButton. The image did not show in the imageButton.
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:paddingBottom="140dp"
android:src = "#drawable/camera"
android:scaleType="fitXY"
android:layout_height="70dp"
android:background="#drawable/round"
android:layout_gravity="center_horizontal" />
round.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="100dp" />
<stroke
android:width="5dp"
android:color="#090" />
</shape>
You added android:paddingBottom=140dp. Issue with that. Check this
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/round"
android:tint="#android:color/black"
android:scaleType="fitXY"
android:padding="5dp"
android:src="#drawable/camera" />
You just missed solid tag in your custom shape file.
<solid android:color="#090" />
Edit:
Never try to give margin or padding which has a larger value. Just like you are using android:paddingBottom="140dp". This is not a recommended way.
Try this
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/round"
android:scaleType="centerInside"
android:src="#mipmap/ic_launcher" />
Use a shape and fill with your desire color.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="100dp" />
<solid android:color="#090" /> // solid for filling empty space
</shape>
Use code in ImageButton
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:paddingBottom="140dp"
android:src = "#drawable/camera"
android:scaleType="fitXY"
android:layout_height="70dp"
android:background="#drawable/round"
android:layout_gravity="center_horizontal" />
Add Solid Tag
<solid android:color="#090" />
and replace android:paddingBottom="140dp" with android:padding="whateversuitsyou"
For drawing a solid circle, try the code below:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#090"/>
<size
android:width="100dp"
android:height="100dp"/>
</shape>
Shape type oval is used for drawing circles.
WhatsApp used a floating action button, and you can implement it in two steps.
Add a dependency to your build.gradle file:
dependencies {
compile 'com.github.clans:fab:1.6.4'
}
Add com.github.clans.fab.FloatingActionButton to your layout XML file.
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:src="#drawable/ic_message"
fab:fab_colorNormal="#color/app_primary"
fab:fab_colorPressed="#color/app_primary_pressed"
fab:fab_colorRipple="#color/app_ripple"/>
Download image of camera by clicking here.
And you are done.
Set width and height as below.
android:layout_width="match_parent"
android:layout_height="wrap_content"
This works for me. Try this...
I have a linear layout which contains three image buttons. Each image buttons has a different color. At the initial time, all image buttons are unchecked. I want to set checked/selected for an image button if the image button is selected (a blue V will overlap to the image button background ), and Another image button will be unchecked. How can I do it in android?
This is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="7dp"
android:layout_centerInParent="true">
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/color1"
android:background="#color/colorAccent"/>
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/color2"
android:layout_marginLeft="5dp"
android:background="#color/colorPrimaryDark" />
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/color3"
android:layout_marginLeft="5dp"
android:background="#color/colorPrimary"/>
</LinearLayout>
Updated: The blue V means checked status. It is similar the result
I would suggest to use checkboxes for this task.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="7dp"
android:layout_centerInParent="true">
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/color1"
android:button="#drawable/color_selector"
android:background="#color/accent"/>
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/color2"
android:button="#drawable/color_selector"
android:layout_marginLeft="5dp"
android:background="#color/primary"
android:checked="false" />
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="50dp"
android:layout_height="50dp"
android:button="#drawable/color_selector"
android:id="#+id/color3"
android:layout_marginLeft="5dp"
android:background="#color/primary_dark"/>
</LinearLayout>
and color_selector.xml should be placed into the drawable folder with the required selector (just an example)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/ic_done_white_18dp">
<shape android:shape="rectangle" >
<stroke android:color="#color/white" android:width="2dp"/>
<corners
android:bottomLeftRadius="#dimen/spacing_xxsmall"
android:bottomRightRadius="#dimen/spacing_xxsmall"
android:topLeftRadius="#dimen/spacing_xxsmall"
android:topRightRadius="#dimen/spacing_xxsmall" />
</shape>
</item>
<item android:state_checked="false">
<shape android:shape="rectangle">
<stroke android:color="#color/white" android:width="1dp"/>
<corners
android:bottomLeftRadius="#dimen/spacing_xxsmall"
android:bottomRightRadius="#dimen/spacing_xxsmall"
android:topLeftRadius="#dimen/spacing_xxsmall"
android:topRightRadius="#dimen/spacing_xxsmall" />
</shape></item>
</selector>
Hope this will help.
Hi have shape and am trying to include this in my layout but it doesn't show?
This is my shape xml "damage.xml"
<shape xmlns:android="http://schemas.android.com/apk/res/android"
shape="rectangle">
<solid color="#ff0000" />
<stroke width="3dp" color="#ff0000" />
And this is my I thought I needed to include the Shape in the layout.
<?xml version="1.0" encoding="utf-8"?>
<View android:id="#+id/damage" android:background="#layout/damage"
android:layout_width="5dip" android:layout_height="wrap_content">
</View>
<ImageView android:id="#+id/icon" android:layout_width="60px"
android:layout_height="60px" android:layout_marginRight="6dip"
android:src="#drawable/placeholder" />
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="6dip">
<TextView android:layout_width="fill_parent" android:id="#+id/title"
android:layout_height="wrap_content" android:text="Item Name"
android:textStyle="bold" android:singleLine="true" android:textSize="16sp" />
<ImageView android:id="#+id/rating" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/rating_five" />
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:id="#+id/range"
android:layout_height="wrap_content" android:text="Range"
android:textStyle="bold" android:singleLine="true" android:textSize="16sp"
android:paddingRight="5dip" />
<TextView android:layout_width="fill_parent" android:id="#+id/condition"
android:layout_height="wrap_content" android:text="Condition"
android:textStyle="bold" android:singleLine="true" android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
However nothing is drawn? Where am I going wrong. Secondly is it correct to set the width and heoght of the shape in the View or should I be doing it in the damage.xml? For example
<shape xmlns:android="http://schemas.android.com/apk/res/android"
shape="rectangle">
<solid color="#ff0000" />
<size android:height="wrap_content" />
<size android:width="5dip" />
<stroke width="3dp" color="#ff0000" />
Here the compiler complains about wrap_content.
Any help would be greatly appreciated.
Try changing color="#ff0000" to android:color="#ff0000". That should cause the rectangle to be visible. Once you do that, you will find that a red rectangle shows up, but it fills the parent view vertically. That is because you used android:layout_height="wrap_content", even though the rectangle does not have any fixed height. I would fix that by using some other value for android:layout_height in your layout.
Also, it appears that you have placed damage.xml in the layout directory. Since a shape is a drawable, you should place it in the drawable directory, and reference it as #layout/damage in your layout.
Update:
Using the following code in damage.xml causes the rectangle to show up fine for me.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#f0f000" />
<stroke android:width="3dp" android:color="#ff0000" />
</shape>