Im trying to have an icon and text on my custom button. I want both to be centered. For this Im using phillipcalvin-iconbutton. This works quite well however the drawable is set does not have any padding so it looks like this:
I have tried to create another drawable with the same same resource that has a padding like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="3dp"
android:left="3dp"
android:right="3dp"
android:top="1dp">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_done"
>
<padding
android:left="50dp"
android:top="50dp"
android:right="50dp"
android:bottom="50dp"
/>
</item>
</selector>
</item>
That does not make a difference.
I don't want to use some sort of Layout were I set a wrap a TextView and a ImageView because I loose the button animation.
Any Idea how I can solve this?
Just incase you want to see my custom button:
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#color/darkRipple">
<item android:id="#android:id/mask"
android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp"
tools:targetApi="lollipop">
<shape android:shape="rectangle">
<solid android:color="#color/darkRipple" />
<corners android:radius="7dp" />
</shape>
</item>
<item android:id="#android:id/background"
android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp">
<shape android:shape="rectangle">
<corners android:radius="7dp" />
<solid android:color="#color/dark" />
<size android:width="110dp" android:height="40dp" />
</shape>
</item>
</ripple>
and here is how I use it:
<com.phillipcalvin.iconbutton.IconButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#drawable/btn_dark"
android:drawableLeft="#drawable/btn_ic_done"
app:iconPadding="10dp"
android:text="DONE"
android:textColor="#color/White"/>
I think one way to get around the problem could be to import the image used as a drawableLeft (android:drawableLeft="#drawable/btn_ic_done" in this case) to android studio as a new Image Asset. That'll allow you to set padding to the image at the time you import it(or you could set padding to the image using some external image editor).
Steps to achieve this:
Right click res folder
res -> new -> image asset
[Customise the image according to your needs][1]
[1]: https://i.stack.imgur.com/6Weu6.png
From official documrntation:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/your.project.package"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<!-- ... -->
<com.phillipcalvin.iconbutton.IconButton
android:id="#+id/search"
android:drawableLeft="#drawable/action_search"
android:text="#string/search"
app:iconPadding="10dp" />
Related
I need to create a custom image view for choosing color by design. When user click the circle, inner white circle with a tick should appear, like in example below:
Otherwise, it should be just a circle shape with some color (unchecked state), like below:
The main problem, that I do not know how to create a selector with the inner circle inside and with keeping the ability to setup color programmatically.
Simple rounded shape I can setup, for example, like this:
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/circle_crop"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:src="#android:color/holo_red_dark" />
How to modify it to setup white circle with tick inside on a checked state (when user click on it)?
So easy.
1. Create layer-list Drawable Called "icon_check"
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item //for the round white background
android:bottom="10dp" //margin, "<size>" tag doesn't work
android:end="10dp"
android:start="10dp"
android:top="10dp">
<shape
android:shape="oval">
<solid
android:color="#color/white" />
</shape>
</item>
<item
android:drawable="#drawable/check" //check mark from built-in asset
android:bottom="14dp" //margin, extra 4dp is prefered
android:end="14dp"
android:start="14dp"
android:top="14dp" />
</layer-list>
2. Create selector Drawable Called "foreground_check"
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/icon_check" //layer-list created above
android:state_selected="true" />
<item
android:drawable="#android:color/transparent" //key point, 【"#null" doesn't work!!】
android:state_selected="false" />
</selector>
3. Setup CardView
<androidx.cardview.widget.CardView //【MaterialCardView doesn't work!!】
android:id="#+id/cardView"
android:layout_width="40dp"
android:layout_height="40dp"
android:foreground="#drawable/foreground_check" //apply layer-list drawable
app:cardBackgroundColor="#color/black"
app:cardCornerRadius="20dp" /> //half of side length
4. Setup Ui Controller
cardView.setOnClickListener {
it.isSelected = !it.isSelected
}
Result:
You need to create a drawable selector for your image view, for example:
drawable_round_view_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/drawable_round_view_selected" android:state_selected="true" />
<item android:drawable="#drawable/drawable_round_view_unselected" android:state_selected="false" />
<item android:drawable="#drawable/drawable_round_view_unselected" />
</selector>
drawable_round_view_selected
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<size
android:width="40dp"
android:height="40dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<stroke
android:width="1dp"
android:color="#3383FF" />
<solid android:color="#3383FF"/>
</shape>
</item>
<item>
<shape android:shape="oval">
<size
android:width="40dp"
android:height="40dp" />
<solid android:color="#ffffff" />
</shape>
</item>
<item android:drawable="#drawable/ic_baseline_check_24" />
</layer-list>
and drawable_round_view_unselected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="40dp"
android:height="40dp" />
<!-- unselected button background -->
<solid
android:color="#color/black" />
<stroke
android:color="#color/gray_martini"
android:width="1dp"/>
</shape>
After that on your layout set the image view like:
<ImageView
android:id="#+id/roundImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:src="#drawable/drawable_round_view_selector"/>
Finally on your activity or fragment change view depending on selected state:
binding.roundImageView.setOnClickListener {
it.isSelected = !it.isSelected
}
PS: change colors and sizes depending on your requirements.
I am currently learning android and wanted to make a button that has elevation effect in it.
To be more precise I want elevation effect like the image below.
Can anyone help me on how to make this type of button in android studio?
Add a XML file in drawable folder and name it a.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Gray shadow. -->
<item android:left="3dp" android:top="5dp">
<shape>
<solid android:color="#android:color/darker_gray" />
<corners android:radius="15dp" />
</shape>
</item>
<!-- White foreground. -->
<item android:bottom="5dp" android:right="3dp">
<shape>
<solid android:color="#android:color/white" />
<corners android:radius="15dp" />
</shape>
</item>
</layer-list>
Add in your button:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:ackground="#drawable/a.xml"
android:text="Button" />
Output:
You just need to add below line to Button tag in xml. It will add elevation as you wanted.
android:stateListAnimator="#null"
I need a border at the bottom, left and right of TextView, but for the left and right border only custom size of actual textview height, not the whole.
Looks like this:
Could anyone explain how to implement it?
At current moment I draw border at bottom, left and right using this code
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/white" />
</shape>
</item>
<item
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/grey_20" />
<solid android:color="#null" />
</shape>
</item>
You can directly give the padding to the layout in which you put the image and give background to the layout you will get border around the corner.
And you also got custom border around the image.
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="10dp"
android:paddingTop="20dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" />
I am giving background to a image you can give image source.
Find a solution, this works for me
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#color/line_grey" />
</shape>
</item>
<item
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<shape>
<solid android:color="#color/white" />
</shape>
</item>
<item android:bottom="8.0dp">
<shape>
<solid android:color="#color/white" />
</shape>
</item>
I need a toggle button to have an image and a background color. Currently, my toggle button only has an image and no background color. Here is the code for it
<ToggleButton
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:onClick="onToggleClicked"
android:textSize="0sp"
android:checked="false"
android:background="#drawable/soundfocuslone1"/>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<bitmap android:src="#drawable/soundonlone1"
android:gravity="center"/>
</item>
<item android:state_pressed="true">
<bitmap android:src="#drawable/soundfocuslone1"
android:gravity="center" />
</item>
<item android:state_checked="false">
<bitmap android:src="#drawable/soundofflone1"/>
</item>
</selector>
Is it possible to have two backgrounds on the toggle button? I want a background color as well as an image on it. If not, is there a simple way to have an image button behave like a toggle button? Having an image button acting as a toggle button would probably be easier as the image is not scaled on the toggle button. Thanks, and I would appreciate all replies.
if you dont want the image to be placed in the center, use this for giving background.
1. put this in your toggle button - android:background="#drawable/bw_toggle_on_selector"
2. create an XML in drawable folder with the name "bw_toggle_on_selector" and paste the below code in it-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#7995A8"
android:startColor="#E8E8E8"
android:endColor="#000000"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>
3 .use android:drawableLeft for setting background image.
and if you want to place the image in center, then use this code (it covers everything incl. background image).
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true">
<layer-list>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_stop"
/>
</item>
<item>
<shape>
<gradient
android:angle="270"
android:endColor="#a0e0b071"
android:startColor="#a0a67637" />
<stroke
android:width="1dp"
android:color="#5c3708" />
<corners
android:radius="5dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
</layer-list>
</item>
<item
android:state_enabled="true">
<layer-list>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_back"
/>
</item>
<item>
<shape
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#a0a67637"
android:startColor="#a0e0b071" />
<stroke
android:width="1dp"
android:color="#5c3708" />
<corners
android:radius="5dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
** Some Tips **
*Changing Button text onClick
1. create a XML file in Drawable folder with the name "text_color.xml"
paste the below code in it-
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="#000000" />
<item
android:state_pressed="false"
android:color="#01DFD7" />
</selector>
2. put this in your button to define the XML
android:textColor="#drawable/text_color"
*Faster way of Resizing my images?
use this tool or Android asset studio (google it)
*How do i make a Custom Background for my Buttons?
try this online tool :)
Actually ToggleButton has "android:button" attribute. So u can use "android:button" attribute for selector, and "android:background" for giving the background color to the ToggleButton.But using toggle button we can get the gravity issue so you can use check box instead of toggle button.
<CheckBox
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:onClick="onToggleClicked"
android:textSize="0sp"
android:checked="false"
android:padding="5dp"
android:button="#drawable/soundfocuslone1"
android:background="ur_background_color_code or drawable"/>
First of all you write inside your xml file:
<ToggleButton
android:id="#+id/tgTextReader"
android:layout_width="60dp"
android:layout_height="36dp"
android:background="#drawable/bw_toggle_on_selector"
android:focusable="false"
android:focusableInTouchMode="false"
android:textOff=""
android:textOn="" />
then create folder inside res/
drawable folder
write xml file name is bw_toggle_on_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/a"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/b"
android:state_checked="false"/>
</selector>
this file keep in drawable foloder.
and also create a new folder inside res /
drawable-nodpi
where two image keep there. such as image a, image b.
hops work successfully. Thanks
I want to develop custom spinner like line around spinner with triangle at right bottom corner.
like following image
For above fig I wrote my custom spinner like a
spinner.xml
<Spinner android:background="#drawable/spinner_background"/>
spinner_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<item android:state_pressed="true"
android:drawable="#drawable/spinner_ab_pressed_new_theme_bs">
<shape>
<solid
android:color="#color/White" />
<corners android:radius="3dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<stroke
android:width="2dp"
android:color="#color/skyblue" />
</shape>
</item>
<!-- spinner_ab_default_new_theme_bs -> this image for corner triangle -->
<item
android:drawable="#drawable/spinner_ab_default_new_theme_bs" >
<shape>
<solid
android:color="#color/White">
</solid>
<corners android:radius="3dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<stroke
android:width="2dp"
android:color="#color/gray"/>
</shape>
</item>
And I got output like following image
I tried lot but not achieve my goal please anybody have solution to develop spinner.
like above first one image.
Spinner
<Spinner
android:id="#+id/To_Units"
style="#style/spinner_style" />
style.xml
<style name="spinner_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#drawable/gradient_spinner</item>
<item name="android:layout_margin">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">20dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:popupBackground">#DFFFFFFF</item>
</style>
gradient_spinner.xml (in drawable folder)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item><layer-list>
<item><shape>
<gradient android:angle="90" android:endColor="#B3BBCC" android:startColor="#E8EBEF" android:type="linear" />
<stroke android:width="1dp" android:color="#000000" />
<corners android:radius="4dp" />
<padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
</shape></item>
<item ><bitmap android:gravity="bottom|right" android:src="#drawable/spinner_arrow" />
</item>
</layer-list></item>
</selector>
#drawable/spinner_arrow is your bottom right corner image
This is a simple one.
your_layout.xml
<android.support.v7.widget.AppCompatSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/spinner_background"
/>
In the drawable folder, spinner_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item><layer-list>
<item>
<shape>
<solid
android:color="#color/colorWhite">
</solid>
<corners android:radius="3dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<stroke
android:width="2dp"
android:color="#color/colorDarkGrey"/>
</shape>
</item>
<item >
<bitmap android:gravity="bottom|right"
android:src="#drawable/ic_arrow_drop_down_black_24dp" />
</item>
</layer-list></item>
</selector>
Preview:
You can achieve the following by using a single line in your spinner declaration in XML:
Just add this: style="#android:style/Widget.Holo.Light.Spinner"
This is a default generated style in android. It doesn't contain borders around it though. For that you'd better search something on google.
Hope this helps.
UPDATE:
AFter a lot of digging I got something which works well for introducing border around spinner.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="8dp"
android:top="8dp">
<shape>
<solid android:color="#android:color/white" />
<corners android:radius="4dp" />
<stroke
android:width="2dp"
android:color="#9E9E9E" />
<padding
android:bottom="16dp"
android:left="8dp"
android:right="16dp"
android:top="16dp" />
</shape>
</item>
</layer-list>
Place this in the drawable folder and use it as a background for spinner. Like this:
<RelativeLayout
android:id="#+id/speaker_relative_layout"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:background="#drawable/spinner_style"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Spinner
android:id="#+id/select_speaker_spinner"
style="#style/Widget.AppCompat.DropDownItem.Spinner"
android:layout_width="match_parent"
android:layout_height="70dp"
android:entries="#array/select_speaker_spinner_array"
android:spinnerMode="dialog" />
</RelativeLayout>
If you don't prefer to create multiple XML files, try:
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:strokeWidth="1dp"
app:strokeColor="#color/black"
app:cardCornerRadius="4dp">
<androidx.appcompat.widget.AppCompatSpinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"/>
</com.google.android.material.card.MaterialCardView>
To change the border color:
app:strokeColor="YOUR_COLOR_HERE"
To change the border's width:
app:strokeWidth="YOUR_DP_WIDTH_HERE"
There are two ways to achieve this.
1- As already proposed u can set the background of your spinner as custom 9 patch Image with all the adjustments made into it .
android:background="#drawable/btn_dropdown"
android:clickable="true"
android:dropDownVerticalOffset="-10dip"
android:dropDownHorizontalOffset="0dip"
android:gravity="center"
If you want your Spinner to show With various different background colors i would recommend making the drop down image transparent, & loading that spinner in a relative layout with your color set in.
btn _dropdown is as:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/spinner_default" />
<item
android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/spinner_default" />
<item
android:state_pressed="true"
android:drawable="#drawable/spinner_pressed" />
<item
android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/spinner_pressed" />
<item
android:state_enabled="true"
android:drawable="#drawable/spinner_default" />
<item
android:state_focused="true"
android:drawable="#drawable/spinner_pressed" />
<item
android:drawable="#drawable/spinner_default" />
</selector>
where the various states of pngwould define your various States of spinner seleti
To change only "background" (add corners, change color, ....) you can put it into FrameLayout with wanted background drawable, else you need to make nine patch background for to not lose spinner arrow. Spinner background is transparent.
You could design a simple nine-patch png image and use it as the background of spinner. Using GIMP you can put both border and right triangle in image.
It's super easy you can just add this to your Adapter -> getDropDownView
getDropDownView:
convertView.setBackground(getContext().getResources().getDrawable(R.drawable.bg_spinner_dropdown));
bg_spinner_dropdown:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/white" />
<corners
android:bottomLeftRadius=enter code here"25dp"
android:bottomRightRadius="25dp"
android:topLeftRadius="25dp"
android:topRightRadius="25dp" />
</shape>