Android: Is there a way to change the height of switch button? - android

The switch height looks really small on a tablet. Is there a way, we can increase the height?
If we increase it by giving a value to layout_height, it just add some blank space at the bottom, but the switch size doesnt increase.

you can try this:
android:thumbTextPadding="25dp"
android:switchMinWidth="56dp"
android:track="#drawable/custom_grey_image"

The #dipali answer is righthere is a small example how to change your swicth width,
i hope it will help some one..
1)Use ur color for thumb (/res/drawable/color_thumb.xml)
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="40dp" />
<gradient android:height="40dp" android:startColor="#FF569BDA" android:endColor="#FF569BDA"/>
2)gray color for track (/res/drawable/gray_track.xml)
shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"
>
<size android:height="40dp" />
<gradient android:height="40dp" android:startColor="#dadadada" android:endColor="#dadadada"/>
3)Selector for thumb (/res/drawable/thumb.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/gray_track" />
<item android:state_pressed="true" android:drawable="#drawable/color_thumb" />
<item android:state_checked="true" android:drawable="#drawable/color_thumb" />
<item android:drawable="#drawable/gray_track" />
4) Selector for track (/res/drawable/track.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/color_thumb" />
<item android:drawable="#drawable/gray_track" />
and finally in switch
use
android:switchMinWidth="56dp"
android:thumb="#drawable/thumb"
android:track="#drawable/track"

You can try to use android:padding="5dp" or android:paddingTop="5dp" and android:paddingBottom="5dp"

Related

Switch stops displaying after adding Thumb/Track drawables

I added switch, I wanted to change its color, so i added following in drawables.
Thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#color/black" />
<item android:state_pressed="true" android:drawable="#color/tabAccessoryButtonSelected" />
<item android:state_checked="true" android:drawable="#color/tabAccessoryButtonSelected" />
</selector>
Track.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/tabAccessoryButtonSelected" />
</selector>
Layout.xml
<Switch
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/switch1"
android:layout_centerHorizontal="true"
android:paddingTop="50dp"
android:text="#string/notification_settings"
android:textColor="#color/white"
android:thumb="#drawable/thumb"
android:track="#drawable/track"
/>
As soon as i added this, switch has stopped displaying in the layout, the text is still there, but the Switch tool is not there.
If i remove Thumb.xml and Track.xml it starts displaying switch.
Kindly guide me what i am doing wrong here.
Thanks
I have modify it now try my solution below.
Layout.xml
<Switch
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/switch1"
android:layout_centerHorizontal="true"
android:paddingTop="50dp"
android:text="#string/notification_settings"
android:textColor="#color/white"
android:thumb="#drawable/thumb"
android:track="#drawable/track"
/>
track.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/color_thumb" />
<item android:drawable="#drawable/gray_track" />
</selector>
thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/gray_track" />
<item android:state_pressed="true" android:drawable="#drawable/color_thumb" />
<item android:state_checked="true" android:drawable="#drawable/color_thumb" />
<item android:drawable="#drawable/gray_track" />
</selector>
color_thumb.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="40dp" />
<gradient android:height="40dp" android:startColor="#color/tabAccessoryButtonSelected"" android:endColor="#color/tabAccessoryButtonSelected""/>
</shape>
gray_track.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="40dp" />
<gradient android:height="40dp" android:startColor="#color/tabAccessoryButtonSelected" android:endColor="#color/tabAccessoryButtonSelected"/>
</shape>
I know this is an old question but I had the same problem today and the issue seemed to be that the thumb drawable needs a fixed size. The track size seems to adjust itself to the size of the thumb so a thumb with no indicated size won't display and because of this the track won't display either.
If you add an actual xml drawable as a thumb and not just a color you can add a size element to it:
switch_thumb.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/colorSwitchThumb" />
<corners android:radius="#dimen/switch_corner_radius"/>
<size android:height="10dp" android:width="10dp"/>
</shape>

Android - Setting Pressed State and Border on a TextView - Only one Background

I have 3 TextViews stacked vertically in a LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:textSize="#dimen/text_size_large"
android:text="Item1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:textSize="#dimen/text_size_large"
android:text="Item2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:textSize="#dimen/text_size_large"
android:text="Item3"/>
</LinearLayout>
I could apply a drawable to the background attribute to render a Border
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="-2dp" android:right="-2dp" android:left="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke android:width="1dp" android:color="#color/pale_grey" />
<padding android:left="24dp" android:right="24dp"
android:top="12dp" android:bottom="12dp" />
</shape>
</item>
</layer-list>
I could also apply a selector, via a drawable, to the background property so that the TextView changed color depending on states such as Pressed. However there is only one background attribute.
I do not believe i can apply the <shape> xml and the <selector> xml within the same drawable. Therefore do I need to wrap a single layout element around each TextView just so can apply another background?
It looks like google solves this by encompassing border, padding and background colour into one drawable patch-9 image. I was hoping I didn't need to do that
Thanks
In the selector set the state pressed and unpressed with your XML's that render the border
Example:
<selector xlmns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/border_unpressed" android:state_pressed="false"/>
<item android:drawable="#drawable/border_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/border_unpressed" />
</selector>
Note:
Make sure to set the TextView attribute android:clickable="true"
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="4dp">
</corners>
<stroke android:width="1dp" android:color="#ffffff"/>
<gradient
android:startColor="#187cb6"
android:endColor="#187cb6"
android:angle="270"/>
</shape>
</item>
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" />
</selector>
</item>
</layer-list>

Android add arrow image to spinner

I have created a background drawable that I use for a spinner. I would like to now but an arrow image inside this background. I have tried things like android:drawableRight="arrowimage", but this does not seem to show anything. Does anyone know how I can include an image in my custom background for a spinner?
Create an XML in a drawable folder with any name for example spinner_bg.xml and add this following lines
<?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="#ffffff"
android:startColor="#ffffff"
android:type="linear" />
<stroke android:width="1dp"
android:color="#504a4b" />
<corners android:radius="5dp" />
<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_ab_default_holo_dark_am" />
// you can use any other image here, instead of default_holo_dark_am
</item>
</layer-list>
</item>
</selector>
Add the following lines to your styles.xml which is inside values folder
<style name="spinner_style" >
<item name="android:background">#drawable/spinner_bg</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
</style>
Now add this style to your spinner as
<Spinner android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/spinner_style"
android:popupBackground="#cccccc" />
Aniruddha's solution worked fine for me, when I used JellyBean (4.2.2) or Kitkat (4.4). But unfortunately on Marshmallow (6.0) I have an exception, possible cause explained here: Using drawable resources
I ended up just making a trick and place spinner to LinearLayout with ImageView like this:
<LinearLayout
android:id="#+id/spinner_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_arrow_drawable"/>
</LinearLayout>
This works for me. You may be missing the default state. Without posting your code it's hard to tell.
<!-- theme -->
<item name="android:dropDownSpinnerStyle">#style/mySpinnerStyle</item>
<!-- style -->
<style name="mySpinnerStyle" parent="android:Widget.Holo.Spinner">
<item name="android:background">#drawable/mySpinner_background</item>
</style>
<!-- mySpinner_background -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/mySpinner_disabled" />
<item android:state_pressed="true"
android:drawable="#drawable/mySpinner_pressed" />
<item android:state_pressed="false" android:state_focused="true"
android:drawable="#drawable/mySpinner_focused" />
<item android:drawable="#drawable/mySpinner_default" />
</selector>
This has worked for me:
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:spinnerMode="dropdown" />
Set your Image ICON On Spinner :
<item>
<layer-list>
<item>
<color android:color="#android:color/white" />
</item>
<item>
<bitmap android:gravity="center_vertical|right" android:src="#drawable/down_arrow" />
</item>
</layer-list>
</item>

How do I apply the default "blue button"-style used on android first start?

Namely, how do I apply this button style? I'm refering to the blue button in the lower right corner.
create a drawable folder in res folder then create XML file and call it button_background
within the XML file you will define the shape of the button, like what is the color, and corner, stroke and padding everything you can do here. The following code is similar to the button:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#56b7cc" />
<stroke
android:width="1dp"
android:color="#70daf1" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#68c8dd"
android:endColor="#70daf1"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#70daf1" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
then in your layout give the button the following:
<Button
android:id="#+id/Button1"
android:layout_width="50dip"
android:layout_height="40dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="107dp"
android:background="#drawable/button_background"
android:textColor="#ffffff"
android:text="ok" />
I made fixed width and height so it look as the picture, the most important attribute is
android:background="#drawable/button_background"
you can modify it as much as you want by changing the colors, padding read more about it here. hope this is what you are looking for
Its android ShowcaseView functionality. You can refer it here
If you want only button style, you need create own button style and based on, for example:
on nine-patch image (using nine-patch image as Background of Button android, Button with 9-patch image background doesn't stretch correctly),
or based on shape (How to create custom button in Android using XML Styles)
style.xml
<style name="Button.Blue" parent="android:Widget.Holo.Light.Button">
<item name="android:background">#drawable/xbg_blue_button</item>
<item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textStyle">bold</item>
</style>
xbg_blue_button.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/btn_default_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/btn_default_disabled_focused_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/btn_default_pressed_holo_light" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/btn_default_normal_holo_light" />
<item android:state_focused="true" android:drawable="#drawable/btn_default_focused_holo_light" />
<item android:drawable="#drawable/btn_default_normal_holo_light" />
drawables:
https://dl.dropboxusercontent.com/u/54394631/buttons.rar

Selector.xml is not working when clicked on the image button

I use the following selector to change image of image button, when I click on it. But this does not work most of the times. Only for few buttons it works. Don't know what the cause here is
mapselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Pressed -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/mapiconover"/>
</selector>
And in the button,
Am doing like this,
<ImageButton
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/btnMap" android:layout_centerInParent="true" android:src="#drawable/mapselector"
android:background="#drawable/map" android:scaleType="fitXY"
android:layout_alignParentBottom="true">
</ImageButton>
but still image is not changing when I click.
Any guess for this?
you have create like this in the ImageButton xml
create xml file using the button image like this
<?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/backbutton" />
<item
android:drawable="#drawable/closebutton" />
</selector>
add that xml file as background for IMageButton
<ImageButton
android:layout_height="50px"
android:layout_width="50px"
android:id="#+id/settings"
android:background="#drawable/settings_button" //setting_button in
the xml file
android:text="Settings"/>
Not sure if this will work but you could try changing your selector to this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Pressed -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/mapiconover"/>
<item android:drawable="#drawable/map" />
</selector>
And your image button to this:
<ImageButton
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/btnMap"
android:layout_centerInParent="true"
android:background="#drawable/mapselector"
android:scaleType="fitXY"
android:layout_alignParentBottom="true">
</ImageButton>
I had the same problem. For me it was because of
android:state_xxxx
conflict. It solved by clarifying the conditions between enabled and pressed. I used the following code to get different states between press and normal state:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="false" >
<shape
android:shape="rectangle">
<gradient android:startColor="#1094ff"
android:endColor="#7eb6e3"
android:angle="270" />
<corners android:radius="#dimen/fab_margin" />
<stroke android:width="2px"
android:color="#505050" />
</shape>
</item>
<item android:state_pressed="true" android:state_enabled="true">
<shape
android:shape="rectangle">
<gradient android:startColor="#0075d4"
android:endColor="#0070cb"
android:angle="270" />
<corners android:radius="#dimen/fab_margin" />
<stroke android:width="2px"
android:color="#ff0000" />
</shape>
</item>
<item android:state_enabled="false" >
<shape
android:shape="rectangle">
<gradient android:startColor="#b9b9b9"
android:endColor="#848484"
android:angle="270" />
<corners android:radius="#dimen/fab_margin" />
<stroke android:width="2px"
android:color="#505050" />
</shape>
</item>
</selector>
This example uses different gradient and stroke for different states. You can also use your drawable instead. Just create a file like "mybutton.xml" in your drawable folder. then set the background property of your button like this:
android:background="#drawable/mybutton"
I hope it be helpful.

Categories

Resources