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>
Related
I'm trying to make a custom radio button...
I'm trying to guide myself in some tutorials here in the community but I'm not successful...
Below I'll leave an image of how I want my radio button to be checked/unchecked.
and then I'll leave my code as it is so far:
<RadioButton
android:id="#+id/btn_language_english_us"
android:layout_width="match_parent"
android:layout_height="34dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:button="#drawable/radio_bg"
android:fontFamily="#font/sansation"
android:text="#string/us_english"
android:textColor="#color/white"
android:textSize="16sp" />
radio_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/circle_indicator_blue" />
<item android:state_checked="false" android:drawable="#drawable/circle_indicator_white" />
</selector>
#drawable/circle_indicator_blue and #drawable/circle_indicator_white
are Vector assets
and here is the uncked/checked result
could someone help me where I'm doing wrong?
my intention is to leave as the first image mentioned above.
Instead using vector asset you may use selector item . Chane your circle_indicator_blue.xml code as below .
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="5dp" android:color="#android:color/white"/>
<size android:width="24dp" android:height="24dp" />
<solid android:color="#android:color/holo_blue_light" />
</shape>
</item>
</selector>
And circle_indicator_white.xml as below -
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval" android:tint="#android:color/white">
<size android:width="24dp" android:height="24dp"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="20dp"/>
</shape>
</item>
</selector>
Output-
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>
I have Button that is using a custom drawable to make rounded corners. But I would like to change the background colors for the different states, focused, pressed etc..
I have used the below in onCreate to change the overall background color. But I don't know how to target the state_pressed to change its color too.
Any help?
in onCreate
DrawableCompat.setTint(btnLogin.getBackground(), ContextCompat.getColor(getApplicationContext(), R.color.colorGreen));
Button
<Button
android:id="#+id/btnLogin"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#drawable/button_rounded"
android:text="Sign In"
android:textColor="#android:color/white"
android:textStyle="bold" />
button_rounded.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="10dip" />
<solid android:color="#5c89c1" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle">
<corners android:radius="10dip" />
<solid android:color="#204778" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dip" />
<solid android:color="#204778" />
</shape>
</item>
</selector>
Try this and it work for me:
Drawable drawable = getResources().getDrawable(R.drawable.button_rounded);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable.mutate(), getResources().getColor(R.color.colorGreen));
I'm new to XML language and have no clue on how to put and ImageButton inside a circular progress bar and also have no clue if that's even possible.
No. If you want to customize ProgressBar you need to set Style and android:progressDrawable
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="#drawable/custom_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
then you can create Layer-List Drawable or any other drawable.
<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>
<gradient
android:startColor="#000001"
android:centerColor="#0b131e"
android:centerY="1.0"
android:endColor="#0d1522"
android:angle="270"
/>
</shape>
</item>
<!-- Define the progress properties like start color, end color etc -->
<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>
check out this
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"