Android radius button - java

I have a button like this
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:text="ButtonText"
style="#style/ButtonText"/>
and this style
<style name="ButtonText">
<item name="android:layout_margin">3dp</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">2</item>
</style>
Now I want to add radius to button, I have tried <item name="android:radius">6dp</item> but it is not working. What is a solution for this?

In your drawable folder, create a round_button.xml with
<?xml version="1.0" encoding="utf-8"?>
<!-- http://www.dibbus.com/2011/02/gradient-buttons-for-android/ -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners
android:radius="2dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
and then in the xml containing the button, set the background as:
android:background="#drawable/round_button"

I believe to add radius to a button you have to create a custom drawable (see this link, specifically the shape drawable section)
then, with the drawable defined, set the background xml tag of the button equal to your drawable file:
android:background="#drawable/your_file"

Related

SearchView with border and text

I want to make my SearchView like this:
How can I implement in Java ?
What you see in the image is the Material TextInput with OutLined style. You can read more about that and the Filled style in the official docs here.
Also, don't forget you'll need material library for this. For that, do
implementation 'com.google.android.material:material:1.2.0-alpha06'
Now, what you can do with it is to make it like above by adding startDrawable and CornerRadius:
Create the XML widget as below and put a startIcon.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/editTextLayout"
style="#style/TextInputLayoutAppearanceOutLined"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Search View"
app:hintEnabled="true"
app:startIconDrawable=" *** Your ICON *** ">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textSize="12sp" />
</com.google.android.material.textfield.TextInputLayout>
Then, this is the style TextInputLayoutAppearanceOutLined, put it in your style.xml file:
<style name="TextInputLayoutAppearanceOutLined" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="hintTextAppearance">#style/HintText</item>
<item name="helperTextTextAppearance">#style/HintText</item>
<item name="android:textColor">#color/dark_grey</item>
<item name="android:textColorHint">#color/colorAccent</item>
<item name="hintTextColor">#color/colorAccent</item>
<item name="boxStrokeColor">#color/colorAccent</item>
<item name="startIconTint">#color/colorAccent</item>
<item name="boxBackgroundColor">#color/white</item>
<item name="boxCornerRadiusBottomEnd">#dimen/_26sdp</item>
<item name="boxCornerRadiusBottomStart">#dimen/_26sdp</item>
<item name="boxCornerRadiusTopEnd">#dimen/_26sdp</item>
<item name="boxCornerRadiusTopStart">#dimen/_26sdp</item>
<item name="boxStrokeWidthFocused">1dp</item>
<item name="hintEnabled">true</item>
<item name="hintAnimationEnabled">true</item>
<item name="colorControlNormal">#color/colorAccent</item>
<item name="colorControlActivated">#color/colorPrimary</item>
Obviously, you can set this in XML as well, but style gives you more control over your app to change the element everywhere by one edit.
Now, you'll have what you want your SearchView to look like, but further to make it act like a SearchView, you need to set filtering for your ListAdapter to make it work.
For that, you can look here.
make search view in layout file first
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_round"/>
then the make drawable and name it bg_round and put the code below to that drawable
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fff"/> // this the the inner background color
<stroke android:width="5dp" //this is for the stroke of the border
android:color="#android:color/black"/> //this is the stroke color
<corners android:radius="4cdp" /> //this is the corner radius
</shape>

Android seekbar customize

I am trying to customize and android seekbar (using API 17) so that the whole progress bar line is blue.
I have created the following XML in res/drawable:
draw_seekbar_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<clip>
<shape
android:shape="line">
<stroke
android:color="#color/progressFxBar"
android:width="2dp"
/>
</shape>
</clip>
</item>
</selector>
and
<SeekBar
android:id="#+id/t_seekbar"
android:layout_width="220dp"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:max="100"
android:progress="50"
android:progressDrawable="#drawable/draw_seekbar_settings"
The problem is only half the progress bar is blue, the other half doesn't have any colour whatsoever.
I would like to get this image
Instead I am getting this
change the draw_seekbar_settings.xml as follows:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Define the background properties like color etc -->
<item android:id="#android:id/background">
<shape
android:shape="line">
<stroke
android:color="#color/progressFxBar"
android:width="2dp"
/>
</shape>
</item>
<!-- Define the progress properties like start color, end color etc -->
<!-- if you want to change the progress edit following -->
<item android:id="#android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#007A00"
android:centerColor="#007A00"
android:centerY="1.0"
android:endColor="#06101d"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>

How to set dropdown arrow from drawable in spinner?

I am trying to set drawable as an arrow in Spinner but bitmap is not supported.
I used svg file (ic_arrow.xml) and set it in spinner_arrow.xml file:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp"
/>
<solid android:color="#color/white" />
<stroke android:width="2px" android:color="#color/grey" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<bitmap android:gravity="center|right" android:src="#drawable/ic_arrow"/>
</item>
</layer-list>
So I used it for background in layout file
<Spinner
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/spinner_arrow"/>
But I have an error in displaying Spinner: "Binary XML file line #41 in myapp:layout/activity_registration_driver: Error inflating class Spinner".
I think the problem is in adding drawable for arrow in spinner but i don't know how to solve it
Please use below code for add arrow with spinner:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/btn_height_common"
android:layout_marginLeft="#dimen/_15sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginRight="#dimen/_15sdp"
android:background="#drawable/bg_editext">
<Spinner
android:id="#+id/sp_gender"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:entries="#array/gender"
android:fontFamily="#font/gotham_narrow_book"
android:overlapAnchor="false"
android:paddingLeft="#dimen/_10sdp"
android:paddingRight="#dimen/_10sdp"
android:textColor="#color/gray_text_color"
android:textSize="#dimen/normal_textsize" />
<ImageView
android:layout_width="#dimen/_15sdp"
android:layout_height="#dimen/_15sdp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/_15sdp"
android:padding="#dimen/_2sdp"
android:src="#mipmap/ic_down_gray" />
</RelativeLayout>
Output:
Basically one needs to create a custom background for a spinner. It should be something like this:spinner_background.xml
<item>
<layer-list>
<item>
<color
android:color="#android:color/white"/>
</item>
<item>
<bitmap
android:gravity="center_vertical|right"
android:src="#drawable/ic_arrow_drop_down_black_24dp"/>
</item>
</layer-list>
</item>
Then create a custom style for your spinner, where you specify the above selector as background:
<style name="Widget.App.Spinner" parent="#style/Widget.AppCompat.Spinner">
<item name="overlapAnchor">true</item>
<item name="android:background">#drawable/spinner_background</item>
</style>
And finally in your app theme you should override two attributes if you want it to be applied all across your app:
<item name="spinnerStyle">#style/Widget.App.Spinner</item>
<item name="android:spinnerStyle">#style/Widget.App.Spinner</item>

How can i add divider to my drop down list (spinner)?

before click
after click - i'd like to add such blue line
I would like to add divider to my drop down list.
I used solutions which I found on stackoverflow but they didn't work.
This is my xml code for in xml fragment
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:background="#drawable/spinner">
</Spinner>
This is spinner.xml. It is define boarder, shape and image of("click-button")
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape android:shape="rectangle">
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="1.5dp"
/>
<gradient android:startColor="#color/white" android:endColor="#color/white" android:angle="270" />
<stroke android:width="2px" android:color="#color/colorPrimary2" />
<corners android:radius="0dp" />
</shape>
</item>
<item android:gravity="center|right" android:drawable="#drawable/ic_spinner_drop_down"/>
</layer-list>
</item>
</selector>
This works only for spinnerMode="dropdown"... for dialog mode the divider has to be added during runtime via an adapter (the referenced sample is also using dropdown but after implementing it and changing the mode to dialog, the divider is still being displayed).
Just try adding this to your styles.xml file inside the values resource directory:
<style name="SpinnerStyle" parent="Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#0000ff</item>
<item name="android:dividerHeight">0.5dp</item>
</style>
<style name="SpinnerTheme" parent="AppTheme">
<item name="android:dropDownListViewStyle">#style/SpinnerStyle</item>
</style>
And then, you can either add an extra child node to the style tag that's already there in that file (that will apply the style to all spinners):
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ... -->
<!-- ... -->
<!-- ... -->
<!-- ... some existing lines -->
<!-- ... new line to add:-->
<item name="android:dropDownListViewStyle">#style/SpinnerStyle</item>
</style>
Or... you can just add that style to your specific Spinner tag in your fragment XML (that will apply the style to only this spinner):
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:background="#drawable/spinner"
android:theme="#style/SpinnerTheme">
</Spinner>

to show active image if I clicked button

I want to show active image when I clicked button.But ,if I am not clicked,to show normal image style.How can I do that ?Please help me kindly.
icon_home_config.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/home_button_hover" />
<item android:state_focused="true" android:drawable="#drawable/home_button_hover" />
<item android:drawable="#drawable/home_button"/>
</selector>
ImageView
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/btn_background" />
btn_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_default_disabled_focused" android:state_focused="true" android:state_enabled="false"/>
<item android:drawable="#drawable/btn_default_focused" android:state_focused="true" android:state_enabled="true"/>
<item android:drawable="#drawable/btn_default_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/btn_default_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_default_normal" />
</selector>
use this background.xml selector & put it in drawable folder & set image background to this selector.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/normal" android:state_focused="true"/>
<item android:drawable="#drawable/normal"/>
</selector>
OR
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#drawable/normal" />
<stroke
android:width="0dp"
android:color="#drawable/pressed" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
set image background
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/background" />

Categories

Resources