How to programmatically change button color after set android:background to make it rounded corner? - android-layout

I've 6 button in my layout. All of it were using custom xml to make it rounded corner in android:background using single xml file. But now, how do I change the colour of every button programmatically? Since the android:background has been used.

here is the code.
1.Create a xml file in your drawable folder like mybutton.xml and paste the following markup:
<?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="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
2.Now use this drawable for the background of your view. If the view is button then something like this:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:background="#drawable/mybutton"
android:text="Buttons" />
And for colour change use style

Related

android studio background behind TextView

I'm trying to achieve something like this in Android Studio:
I don't want the background shape to be as the TextView size, I want it half the size just like the above image. This is what I tried to do:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="30dp">
<shape android:shape="rectangle" >
<solid android:color="#f3f3f3" />
</shape>
</item>
<item android:top="20dp">
<shape android:shape="rectangle" >
<solid android:color="#FFC107" />
</shape>
</item>
</layer-list>
and it looks like this:
not exactly what I wanted. how can I do it like in the image above?
Try this:
define in a res/drawable new file called double_color.xml :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/shape_rectangle">
<shape android:shape="rectangle" >
<solid android:color="#color/green" />
</shape>
</item>
<item android:top="-10dp" android:right="-10dp" android:left="-10dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="10dp"
android:color="#android:color/white"/>
</shape>
</item>
</layer-list>
Than define a new style in res/values as:
<style name="TexViewStyle" parent="TextAppearance.AppCompat">
<item name="android:background">#drawable/double_color</item>
</style>
And last add style to your textView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Full name"
style="#style/TexViewStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Will be like this:

How to implement these type of Edittext?

enter image description here
They have little elevation effect and not outline box.
Just create an xml file in your drawable folder name as round_corner.xml.And add this following code.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/darkGray" />
<corners android:radius="7dp" />
</shape>
</item>
<item
android:bottom="3dp"
android:right="3dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
<corners android:radius="7dp" />
</shape>
</item>
</layer-list>
At Last you add this xml in background property of your Edittext as follows:-
<EditText
android:id="#+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_corner"
/>

Android Studio - Can't manually resize buttons

For some reason I can't manually resize any buttons on the design view using the handles. I can click and drag the handles but when I release, no changes are seen anywhere.
EDIT:
Here is the xml for the button:
<Button
android:text="√"
android:id="#+id/button15"
android:background="#drawable/operator_style"
android:textAllCaps="false"
android:fontFamily="sans-serif"
android:textSize="24sp"
android:textColor="#707070"
android:layout_alignBaseline="#+id/buttonLog"
android:layout_alignBottom="#+id/buttonLog"
android:layout_alignStart="#+id/button8"
android:elevation="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
And the linked background "operator_style" xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#00C7FC" />
<stroke
android:width="4dp"
android:color="#9FEDFF" />
<corners
android:radius="0dp" />
</shape>
</item>
<item>
<shape>
<solid
android:color="#cccccc" />
<corners
android:radius="0dp" />
<elevation
android:elevation="2dp" />
</shape>
</item>
</selector>

How to get a round image button in android studio

I have tried to get an image button of round shape. But it is displaying the circular image with a square shaped background but i need only the image without any background.
I also used round_image.XML as background drawable file but its not working.
menu.xml
<ImageButton
android:id="#+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:src="#drawable/sliderr" android:background="#drawable/round_image"/>
round_image.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval">
<gradient android:startColor="#50d050
android:endColor="#008000" android:angle="270"/>
<stroke
android:width="1px"
android:color="#000000"/>
</shape>
Use this corner , instead of yours
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/colorPrimary" />
<stroke android:color="#color/colorPrimary" />
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>

Seekbar rectangular corners

I have a situation similar to this one, however, the method supposed there isn't working for me. I've tried different variation with tag but nothing has helped so far.
Here's my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="20"
android:progress="0"
android:secondaryProgress="0"
android:progressDrawable="#drawable/seekbar_progress" />
</LinearLayout>
drawable-mdpi/seekbar_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background"
android:drawable="#drawable/passive_progress">
<shape>
<corners android:radius="0dp" />
</shape>
</item>
<item
android:id="#android:id/progress"
android:drawable="#drawable/active_progress">
<clip>
<shape>
<corners android:radius="0dp" />
</shape>
</clip>
</item>
</layer-list>
Thank you.

Resources