how to fix the button from rectangle to rounded rectangle? - android

I can find many rounded-rectangle button but it is not working to me.
What do I miss?
It is layout.xml:
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#ebf1ff"
android:orientation="vertical">
<LinearLayout
style="#style/wrapperWidthWeightSumOne"
android:paddingTop="50dp">
<Button
android:id="#+id/btnTypeAll"
style="#style/ConsentTypeBtn"
android:layout_width="0dp"
android:layout_weight="0.85"
android:text="All"/>
</LinearLayout>
(...)
</LinearLayout>
This is button's style:
<style name="ConsentTypeBtn">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:onClick">onClick</item>
<item name="android:background">#drawable/selector_btn_consent_all</item>
<item name="android:layout_marginBottom">2dp</item>
<item name="android:singleLine">true</item>
<item name="android:layout_gravity">center</item>
<item name="android:textColor">#fff</item>
</style>
...and this is selector_btn_consent_all.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/consent_type_on">
<shape android:shape="rectangle">
<corners android:radius="4dp" />
<stroke android:width="2dp" android:color="#1F3259"/>
</shape>
</item>
<item android:state_pressed="true" android:drawable="#drawable/consent_type_on"></item>
<item android:drawable="#drawable/consent_type_off"></item>

your selector_btn_consent_all.xml has status like this: when android:state_selected="true", use android:drawable="#drawable/consent_type_on".
So, find consent_type_on.xml at your drawable folder, and it would be rectangle, add <corners android:radius="4dp" />
EDIT
then, make consent_type_on.xml at your drawable folder. example here
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="#android:color/holo_blue_bright" />
</shape>

Related

AppCompatButton style show a ghost rectangle

I am styling a button in my android app but I am on the verge on losing my mind. There is rectangle which appears below the text as you can see in the picture attached:
I can not understand how that's shows up...
Below is my button:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:paddingTop="24dp"
android:paddingBottom="48dp"
android:background="#drawable/bottom_sheet_rounded_corner">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/ctaButton"
style="#style/AppTheme.ButtonStyleLightness200"
android:textAppearance="#style/SubHead2_bld14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Button"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
There is nothing really fancy. the Theme is defined as below:
<style name="AppTheme.ButtonStyleLightness200" parent="Widget.MaterialComponents.Button">
<item name="android:textAppearance">#style/SubHead2_bld14</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#drawable/bottom_button_text_color_selector</item>
<item name="android:background">#drawable/bottom_button_bkg_lightness200_selector</item>
<item name="android:textAllCaps">false</item>
<item name="android:layout_marginStart">24dp</item>
<item name="android:layout_marginEnd">24dp</item>
<item name="android:height">48dp</item>
</style>
and the background is defined as below:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#android:color/transparent" />
<stroke android:width="2dp" android:color="#color/lightness200"/>
</shape>
</item>
<item android:state_enabled="true">
<shape android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#color/solidnight" />
</shape>
</item>
</selector>
lightness color are dark color with transparency
Any idea??

layout clickable selector doesn't work - why?

Create a login page. but I don't know what problem this code
login button don't work change color when I clicked!
I want to when I click then change color button
why doesn't changed that 'LOGIN' button?
how I can solve this?
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="16dp"
android:background="#drawable/btn_login"
android:clickable="true"
android:gravity="center"
android:paddingLeft="32dp"
android:paddingRight="32dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/icon_user" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="LOGIN"
android:textColor="#fff"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
selector xml ->
btn_login.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true">
<shape android:shape="rectangle">
<solid android:color="#4DAEEA" />
<corners android:radius="30dp" />
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#00ff00" />
<corners android:radius="30dp" />
</shape>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle">
<solid android:color="#999" />
<corners android:radius="30dp" />
</shape>
</item>
</selector>
you should make LinearLayout behave like a button by adding style and removing background , follow these steps
<LinearLayout
......
style="#style/btn_stand"
.......
>
</LinearLayout>
My style definition for the button:
<style name="btn_stand" parent="AppBaseTheme">
<item name="android:background">#drawable/btn_stand_sel</item>
<item name="android:textColor">#drawable/btn_stand_text_color</item>
<item name="android:minHeight">48dp</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:paddingRight">5dp</item>
</style>
My #drawable/btn_stan_sel file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:drawable="#drawable/btn_stand_disabled" android:state_enabled="false"/>
<!-- enabled and pressed state -->
<item android:drawable="#drawable/btn_stand_pressed" android:state_enabled="true" android:state_pressed="true"/>
<!-- enabled and focused state -->
<item android:drawable="#drawable/btn_stand_focused" android:state_enabled="true" android:state_focused="true"/>
<!-- enabled state -->
<item android:drawable="#drawable/btn_stand_enabled" android:state_enabled="true"/>
</selector>
My drawable file repeated for each state just with different colors for each state:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#color/stroke" />
<solid android:color="#color/blue" />
<corners android:radius="6dp" />
</shape>
this was an example that works for me , hope you use it and choose the color you want
Add android:focusable="true"to your linear layout xml code.
In the event that that doesn't work, you can try to replace android:background=#drawable/btn_login with android:foreground="?android:attr/selectableItemBackground".

Spinner with border

I would like to design a simple spinner like below:
Below is my code:
sinnerBg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false" android:state_enabled="true"
android:drawable="#android:color/black" />
<item>
<layer-list>
<item>
<shape>
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#000000" />
<padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
</shape>
</item>
<item >
<bitmap android:gravity="right" android:src="#drawable/arrow_down" />
</item>
</layer-list>
</item>
</selector>
In style.xml:
<style name="spinner_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#drawable/spinnerBg</item>
<item name="android:layout_margin">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">20dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:popupBackground">#000000</item>
<item name="backgroundTint">#ffffff</item>
<item name="android:textColor">#000000</item>
</style>
Spinner in my axml file:
<Spinner
android:layout_width="64.0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:ems="10"
android:spinnerMode="dropdown"
android:layout_marginRight="6.0dp"
android:id="#+id/PIC"
android:layout_marginTop="12.5dp"
style="#style/spinner_style" />
The code to populate the spinner:
ArrayAdapter<ViewModel> myAdapter = new ArrayAdapter<ViewModel>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, InfoList);
_spinner.Adapter = myAdapter;
The selected text color of my spinner is coming white which is why the spinner selected text is not displayed in my page:
What am I missing? Any help?
EDIT
This is happening because my spinner is inside linear layout which has background color white:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:background="#color/white">
I want my page to be white. That is the reason I had set white background for root linearlayout.
I managed to solve the issue. Hope it helps. Here is my solution for it:
style.xml:
<style name="spinner_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#drawable/gradient_spinner</item>
<item name="android:layout_marginTop">2dp</item>
</style>
gradient_spinner.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape>
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#000000" />
<padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
</shape>
</item>
<item >
<bitmap android:gravity="bottom|right" android:src="#drawable/ic_arrow_down" />
</item>
</layer-list>
</item>
</selector>
I made a change here, added SetDropDownViewResource with custom layout:
ArrayAdapter<AgentViewModel> agentAdapter = new ArrayAdapter<AgentViewModel>(this, Resource.Layout.custom_spinner_item, InfoList);
agentAdapter.SetDropDownViewResource(Resource.Layout.spinner_dropDowm_item_view);
_spinner.Adapter = agentAdapter;
spinner_dropDowm_item_view.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#color/white"
android:padding="10dp"
/>
And added spinner_style style to the spinner:
<Spinner
android:layout_width="64.0dp"
android:layout_weight="0.75"
android:ems="10"
android:spinnerMode="dropdown"
android:id="#+id/spinner"
android:layout_marginTop="12.5dp"
android:layout_column="1"
android:layout_columnSpan="3"
android:layout_row="0"
android:layout_columnWeight="3"
android:layout_marginRight="7.0dp"
android:layout_marginBottom="4.5dp"
style="#style/spinner_style" />
You can use
android:spinnerItemStyle
android:spinnerDropDownItemStyle
You are missing to set
textAppearanceSmallPopupMenu
<item name="android:textAppearanceSmallPopupMenu">#style/my_style</item>
Finally
<style name="my_style" parent="#android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Small">
<item name="android:textColor">#000000</item>
<item name="android:textSize">12sp</item>
</style>

Android : Change the color of radio button in custom layout for spinner

I want to change the color of radio button on being tapped. I am using custom layout with on custom edit text and radio button in it.This is my xml for layout item.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
<com.ekcoffee.ekcoffeeapp.widgets.AppTextView
android:id="#+id/textViewCountryCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingRight="#dimen/dimen_10dp"
android:text="+91 (India)"
android:textColor="#color/color_black_text"
android:textSize="#dimen/dimen_16sp"
app:textStyle="#integer/OPEN_SANS_REGULAR" />
</RelativeLayout>
<RadioButton
android:id="#+id/selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:backgroundTint="#color/color_accent"
android:checked="false"
android:clickable="false"
android:focusable="false"
/>
This is my xml for spinner
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spinnerCountryCode"
style="#style/spinner_style"
android:layout_width="#dimen/dimen_1dp"
android:layout_height="#dimen/dimen_1dp"
android:layout_alignParentBottom="true"
android:hint="+91"
android:spinnerMode="dialog"
android:textColor="#color/color_splash_screen_text"
android:textColorHint="#color/color_white"
android:textSize="#dimen/dimen_25dp" />
i have used style also for spinner
<style name="spinner_style" parent="Widget.AppCompat.Spinner.Underlined">
<item name="android:background">#color/color_primary</item>
<item name="android:layout_marginLeft">#dimen/dimen_48dp</item>
<item name="android:layout_marginRight">#dimen/dimen_24dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:checkMark">#drawable/ic_radio_checked</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:layout_marginTop">100dp</item>
<item name="android:paddingBottom">5dp</item>
</style>
In your styles.xml put -
<!-- custom style -->
<style name="radionbutton"
parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:button">#drawable/radiobutton_drawable</item>
</style>
Your radio button in xml should look like-
<RadioButton
android:layout_width="wrap_content"
style="#style/radionbutton"
android:checked="false"
android:layout_height="wrap_content"
/>
Now all you need to do is make a radiobutton_drawable.xml in your drawable folder. Here is what you need to put in it-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/radio_unchecked" android:state_checked="false" android:state_focused="true"/>
<item android:drawable="#drawable/radio_unchecked" android:state_checked="false" android:state_focused="false"/>
<item android:drawable="#drawable/radio_checked" android:state_checked="true" android:state_focused="true"/>
<item android:drawable="#drawable/radio_checked" android:state_checked="true" android:state_focused="false"/>
</selector>
Your radio_unchecked.xml-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="1dp" android:color="#color/colorAccent"/>
<size android:width="30dp" android:height="30dp"/>
</shape>
Your radio_checked.xml-
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="1dp" android:color="#color/colorAccent"/>
<size android:width="30dp" android:height="30dp"/>
</shape>
</item>
<item android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp">
<shape android:shape="oval">
<solid android:width="1dp" android:color="#color/colorAccent"/>
<size android:width="10dp" android:height="10dp"/>
</shape>
</item>
</layer-list>
Just replace #color/colorAccent with the color of your choice.
More simple, just set the buttonTint color: (only works on api level 21 or above)
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radio"
android:checked="true"
android:buttonTint="#color/your_color"/>
in your values/colors.xml put your color in this case a reddish one:
<color name="your_color">#e75748</color>
Result:
int textColor = Color.parseColor(#000000);
yourradiobutton.setButtonTintList(ColorStateList.valueOf(textColor));
put above code inside your button's onClick method

Style file to add border to all buttons?

How can I make a style file that adds borders to all buttons? All buttons already have a background image.
Create your style in file res/values/styles.xml
<style name="KeyBoardButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:layout_margin">4dp</item>
<item name="android:paddingTop">8dp</item>
<item name="android:paddingBottom">8dp</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:textColor">#555</item>
<item name="android:background">#drawable/button_border</item>
</style>
Define the border that will be used in file res/drawable/button_border.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"/>
<solid
android:color="#d5cbc6" />
<stroke
android:width="2dp"
android:color="#dedede" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"/>
<solid
android:color="#f9f9f9" />
<stroke
android:width="2dp"
android:color="#dedede" />
</shape>
</item>
Now you can use it in your layout files, as follows:
<Button android:id="#+id/my_button"
style="#style/KeyBoardButton"
android:text="1" />

Categories

Resources