Change Button Shape on selected State? - java

I have an Android Button:
<Button
android:layout_width="wrap_content"
android:layout_height="44dp"
android:background="#drawable/action_button" />
action_button.xml is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:left="25dp"
android:right="25dp"/>
<gradient
android:angle="0"
android:endColor="#color/whiteColor"
android:startColor="#color/whiteColor"/>
<corners android:radius="#dimen/borderRadius"/>
<stroke
android:width="2px"
android:color="#color/actionColor"/>
</shape>
How can I change the shape, when the button is pressed?

Hi please use below selector and change according to your need
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- <item android:state_enabled="false"
android:drawable="#drawable/cyan_strip" />
<item android:state_pressed="true"
android:drawable="#drawable/cyan_strip" />
<item android:state_pressed="false" android:state_focused="true"
android:drawable="#drawable/cyan_strip" />
<item android:drawable="#drawable/black_strip" /> -->
<item android:state_enabled="false">
<shape>
<solid
android:color="#4ec9d9" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<padding
android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
</shape>
</item>
<item android:state_pressed="true">
<shape>
<solid
android:color="#4ec9d9" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<padding
android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
</shape>
</item>
<item android:state_pressed="false" android:state_focused="true">
<shape>
<solid
android:color="#4ec9d9" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<padding
android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
</shape>
</item>
<item android:state_selected="true">
<shape>
<solid
android:color="#4ec9d9" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<padding
android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
</shape>
</item>
<item >
<shape>
<solid
android:color="#000000" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<padding
android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
</shape>
</item>
</selector>

you need a selector
<?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/action_button_pressed" />
<item android:drawable="#drawable/action_button" />
</selector>
put it in a xml file, under drawable/, and use it as background for your Button . Android will take care of the rest

see :
Link
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#302226"
android:endColor="#6D7B8D"
android:angle="45"/>
<padding android:left="6dp"
android:top="6dp"
android:right="6dp"
android:bottom="6dp" />
<corners android:radius="30dp" />
</shape>
or see :
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/shape_oval" />
Shape_oval is an image which we can take from google serching or drawing ourself in paint or photoshop.

Related

UI error between what I design in drawables and what is displaying in imageview

I searched in internet to find a way to implement graphical designs then I find a way to implement them with drawables;
I start to coding the template but it doesn't display correctlly :(
My designed drawable
Main layout
what is the problem ? How can I fix it ?
I designed it but it dosen't work. I don't know what is the problem with that.
Should I use image of that template in the background of imageview for all templates like this ?
Notice : I set that drawable for background of layouts like linear,relative and constraint but doesn't shown correctly too.
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#9c9c9c" />
</shape>
</item>
<item
android:bottom="920dp"
android:left="-100dp"
android:right="50dp"
android:top="-700dp">
<rotate
android:fromDegrees="50"
android:pivotX="-50%"
android:pivotY="100%">
<shape android:shape="rectangle">
<solid android:color="#fff" />
<corners android:radius="30dp" />
<stroke
android:width="2dp"
android:color="#dfcefc" />
</shape>
</rotate>
</item>
<item
android:bottom="50dp"
android:left="-30dp"
android:right="315dp"
android:top="-100dp">
<rotate
android:fromDegrees="-40"
android:pivotX="10%"
android:pivotY="50%">
<shape android:shape="rectangle">
<solid android:color="#fff" />
<corners android:radius="20dp" />
<stroke
android:width="2dp"
android:color="#dfcefc" />
</shape>
</rotate>
</item>
<item
android:bottom="200dp"
android:left="100dp"
android:right="-400dp"
android:top="-40dp">
<rotate
android:fromDegrees="-40"
android:pivotX="60%"
android:pivotY="40%">
<shape android:shape="rectangle">
<solid android:color="#fff" />
<corners android:radius="30dp" />
<stroke
android:width="2dp"
android:color="#dfcefc" />
</shape>
</rotate>
</item>
<item
android:bottom="420dp"
android:left="120dp"
android:right="150dp"
android:top="100dp">
<rotate
android:fromDegrees="-40"
android:pivotX="365%"
android:pivotY="125%">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<gradient
android:angle="270"
android:endColor="#7436f6"
android:startColor="#aa57e5"
android:type="linear" />
</shape>
</rotate>
</item>
<item
android:bottom="-200dp"
android:left="-100dp"
android:right="240dp"
android:top="500dp">
<rotate
android:fromDegrees="-40"
android:pivotX="60%"
android:pivotY="5%">
<shape android:shape="rectangle">
<solid android:color="#fff" />
<corners android:radius="30dp" />
<stroke
android:width="2dp"
android:color="#dfcefc" />
</shape>
</rotate>
</item>
<item
android:bottom="100dp"
android:left="-100dp"
android:right="100dp"
android:top="200dp">
<rotate
android:fromDegrees="-40"
android:pivotX="130%"
android:pivotY="145%">
<shape android:shape="rectangle">
<solid android:color="#fff" />
<corners android:radius="30dp" />
<gradient
android:angle="270"
android:endColor="#7436f6"
android:startColor="#aa57e5"
android:type="linear" />
</shape>
</rotate>
</item>
I need it. Please help me :)
use <size android:width="50dp"
android:height="50dp"
/> to set height width
here is your square
<item
android:bottom="420dp"
android:left="120dp"
android:right="150dp"
android:top="100dp">
<rotate
android:fromDegrees="-40"
android:pivotX="365%"
android:pivotY="125%">
<shape android:shape="rectangle">
<size android:width="50dp"
android:height="50dp"
/>
<corners android:radius="10dp" />
<gradient
android:angle="270"
android:endColor="#7436f6"
android:startColor="#aa57e5"
android:type="linear" />
</shape>
</rotate>
</item>
Change
app:src="#drawable/test3"
to
app:srcCompat="#drawable/test3"
in your ImageView of MainLayout (i.e. page_text.xml)
Hopefully it will work.. Try it..

Transparent background for android

i want to make my own background transparent, can anyone guide me how.
below is the background color i wanna use and i want it transparent
<?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="10dp" />
<stroke android:width="1dip" android:color="#ffffff" />
<gradient android:angle="-90" android:startColor="#ffffff" android:endColor="#000000" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="10dp" />
<stroke android:width="1dip" android:color="#000000" />
<solid android:color="#ffffff"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="10dp" />
<stroke android:width="1dip" android:background="#null" />
<gradient android:angle="-90" android:startColor="#C0C0C0" android:centerColor="#C0C0C0" android:endColor="#C0C0C0" />
</shape>
</item>
</selector>
Try to use #android:color/transparent for your background field, it is the android's system color for transparency

How to make relative layout with box shadow

How do i make the relative layout look like the image
There are two ways to create shadow:
If sdk version is lower than 5.0...
A. You can also use android drawable image
android:background="#android:drawable/dialog_holo_light_frame"
B. Use below shadow_round.xml to make a drawable background for shadow of view. android:background="#drawable/shadow_round
<!-- Drop Shadow Stack -->
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
<corners android:radius="10dp" />
<solid android:color="#00CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
<corners android:radius="10dp" />
<solid android:color="#10CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
<corners android:radius="10dp" />
<solid android:color="#20CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
<corners android:radius="10dp" />
<solid android:color="#30CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
<corners android:radius="10dp" />
<solid android:color="#50CCCCCC" />
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="#ffffff" />
<corners android:radius="10dp" />
</shape>
</item>
If you are using SDK 5.0 or greater then use the view's elevation property.
android:elevation="2dp"
simple add elevation property in your relative layout. or
create xml in drowable folder and call as a background
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ddd" />
<stroke
android:width="5dp"
android:color="#b8b9b9" />
<corners
android:radius="3dp" />
</shape>
Try this code:- radius.xml
:- this xml add in drawable folder in and relative layout in background in set
android:background="#drawable/radius"
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<stroke android:width="2dp" android:color="#b8b9b9"/>
<corners android:bottomLeftRadius="3dp" android:bottomRightRadius="3dp" android:topLeftRadius="3dp" android:topRightRadius="3dp"/>
</shape>

Remove white background from custom button android

<?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="#000066" />
<stroke
android:width="2dp"
android:color="#3333D6" />
<corners
android:radius="50dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#1919FF"
android:endColor="#7171FF"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#ffffff" />
<corners
android:radius="50dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
This is the code I use to style some buttons , but they still appear with that ugly background (white) like this: http://prntscr.com/65yzgv
How can I get rid of this white background?
This problem is due to the fact that you are probably compiling the button with lollipop,i.e., API 21, in which shadow is by default.
So, try the way I did it (basically, set its style to 'borderless') :
yourcustombutton.xml (same as your file) :
<?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="#000066" />
<stroke
android:width="2dp"
android:color="#3333D6" />
<corners
android:radius="50dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#1919FF"
android:endColor="#7171FF"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#ffffff" />
<corners
android:radius="50dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Button Declared in your activity file :
...
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1"
android:background="#drawable/yourcustombutton"
style="?android:attr/borderlessButtonStyle"
/>
...
This is working fine... I checked it. Hope this helps !
To make a background transparent, just add android:background="#android:color/transparent" in your button properties in the layout xml file its defined in.
In button add:
android:background="#null"

adding selector and layer list to edittext

I was planning to add a shadow on my edittext I know how to add shadow but I want also to add a selector on it how can I add selector and shadow at the same time?
selector:
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_edittext.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_focused="true">
<shape>
<solid
android:color="#3f3f3f"/>
<stroke
android:width="2.3dp"
android:color="#656565" />
<corners
android:radius="2dp" />
</shape>
</item>
<item android:state_pressed="true" android:state_focused="false">
<shape>
<solid
android:color="#3f3f3f"/>
<stroke
android:width="2.3dp"
android:color="#656565" />
<corners
android:radius="2dp" />
</shape>
</item>
<item android:state_pressed="false" android:state_focused="true">
<shape>
<solid
android:color="#3f3f3f"/>
<stroke
android:width="2.3dp"
android:color="#656565" />
<corners
android:radius="2dp" />
</shape>
</item>
<item android:state_pressed="true" android:state_focused="true">
<shape>
<stroke
android:width="2.3dp"
android:color="#F6EBC9" />
<corners
android:radius="10dp" />
</shape>
</item>
<item android:state_enabled="true">
<shape>
<solid
android:color="#3f3f3f"/>
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp"
/>
</shape>
</item>
</selector>
shadow:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- most improtant is order of layers -->
<!-- Bottom right side 2dp Shadow -->
<item >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="3px" android:right="3px">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
edittext:
<EditText
android:id="#+id/searchScreen2Edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/textinputborder"
android:ems="10"
android:hint="Search Bus Station..."
android:imeOptions="actionDone"
android:inputType="textVisiblePassword"
android:padding="8dip"
android:textSize="18sp"
android:shadowColor="#000000"
android:shadowDx="1.2"
android:shadowDy="1.2"
android:shadowRadius="1.5"
>
I dont know how to implement the both cause I can add two background at the same time.. how can Do it?

Categories

Resources