How to change button's drawable background's direction which uses XML - java

I have 2 buttons ( left and right ) with one background drawable.
The background has corners on the right side.
I don't want to create one more background just for a change the side of the corners to left, so how can I rotate the background with corners on right in the XML file to put the corners on left?
I've tried to add android:rotation="180dp" but it's not worked for me:
<Button
android:background="#drawable/toggle_buttons"
android:id="#+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/login"
android:textAllCaps="false" />
<Button
android:background="#drawable/toggle_buttons"
android:id="#+id/btnSignUp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/sign_up"
android:textAllCaps="false" />
code for the drawable background as below
<item>
<shape android:shape="rectangle">
<solid android:color="#color/background_color"/>
<corners android:bottomRightRadius="100dp" android:topRightRadius="100dp"/>
<stroke android:color="#color/text_color" android:width="0.5dp"/>
</shape>
</item>

You don't need a custom background. You can use the standard MaterialButton in the Material Components Library and apply a shapeAppearanceOverlay:
<com.google.android.material.button.MaterialButton
app:strokeWidth="..."
app:strokeColor="#color/...."
app:shapeAppearanceOverlay="#style/left"/>
<com.google.android.material.button.MaterialButton
app:strokeWidth="..."
app:strokeColor="#color/...."
app:shapeAppearanceOverlay="#style/right"/>
with:
<style name="right">
<item name="cornerSize">50%</item>
<item name="cornerSizeBottomLeft">0dp</item>
<item name="cornerSizeTopLeft">0dp</item>
</style>
<style name="left">
<item name="cornerSize">50%</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeTopRight">0dp</item>
</style>

Related

Fill the icons in bottomNavigation

I use bottomNavigation.
I wanna fill the icon with the color "not the outside of the icon" when selected.
How can I do that?
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:paddingTop="2dp"
android:background="#FFFFFF"
android:theme="#style/BottomNavigationTextStyle"
app:labelVisibilityMode="labeled"
app:itemIconSize="24dp"
android:layout_alignParentBottom="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:menu="#menu/buttom_navigation_menu">
</com.google.android.material.bottomnavigation.BottomNavigationView>
Use the itemIconTint attribute:
<com.google.android.material.bottomnavigation.BottomNavigationView
app:itemIconTint="#color/my_selector"
..>
and define a selector like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="1.0" android:color="..." android:state_checked="true"/>
<item android:alpha="0.6" android:color="..."/>
</selector>
Use custom made images as per your requirement. You would need to add 2 images one for the active state and for deactive state.
Create a selector home_icon_selector.xml for your icon
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/your_active_icon_here" android:state_selected="true" />
<item android:drawable="#drawable/your_active_icon_here" android:state_pressed="true" />
<item android:drawable="#drawable/your_active_icon_here" android:state_checked="true" />
<item android:drawable="#drawable/your_deactive_icon_here" />
</selector>
Update your menu buttom_navigation_menu to use the selector above
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_home"
android:enabled="true"
android:icon="#drawable/home_icon_selector"
android:title="#string/tab_title_home"/>
</menu>

How do I enable/disable a drawable for a button object in android?

I have a border that I made for a button that should be enabled when the button is clicked on.
<Button
android:id="#+id/imageView_red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:src="#drawable/border" />
My question is how can I enable/disable this drawable? Does java provide a solution for this?
I tried looking at this post, but it didn't seem to answer that question
Android - border for button
All you need to do is you have to put the selector drawable in the button like this
<Button
android:id="#+id/button1"
android:background="#drawable/selector_xml_name"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
And the drawable like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<stroke android:width="10dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape>
<stroke android:width="0dp"/>
</shape>
</item>

Android Spinner suggestions needed

I am using spinner in my layout and its appearance is as follows:
I want to increase the spinner size to make it more attractive and visible.
here is spinner xml and activity snippet.
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_room"
android:layout_below="#id/Toolbar_Room"
android:textSize="52sp"
android:prompt="#string/room_prompt"
android:spinnerMode="dropdown"
android:dropDownSelector="#drawable/img_small_box_green"
android:drawSelectorOnTop="true"></Spinner>
Create a custom layout for your Spinner and inflate it through an adapter.
Check out this.
http://abhiandroid.com/ui/custom-spinner-examples.html
I have created spinner with card view check this you may like this design.
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_5"
android:background="#android:color/white"
app:cardElevation="#dimen/margin_2"
app:contentPaddingBottom="#dimen/margin_5"
app:contentPaddingTop="#dimen/margin_5">
<Spinner
android:id="#+id/sp_filter_status"
style="#style/Widget.Spinner"
android:background="#drawable/spinner_expand_black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="0dp" />
</android.support.v7.widget.CardView>
drawable/spinner_expand_black
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item>
<shape>
<solid android:color="#android:color/white" />
<padding
android:bottom="#dimen/margin_1"
android:left="#dimen/margin_1"
android:right="#dimen/margin_1"
android:top="#dimen/margin_1" />
</shape>
</item>
<item>
<bitmap
android:gravity="end"
android:src="#mipmap/ic_action_expand_more" />
</item>
</layer-list>
#mipmap/ic_action_expand_more is image of bottom arrow you can find from material icon site.
Style
<style name="Widget.Spinner" parent="Widget.AppCompat.Spinner.DropDown">
<item name="android:background">?android:selectableItemBackground</item>
<item name="android:dropDownSelector">?android:selectableItemBackground</item>
<item name="android:divider">#null</item>
<item name="overlapAnchor">true</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:textSize">#dimen/text_16</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:focusable">true</item>
</style>
put your Spinner inside CardView.
it will look more better.

custom rating bar drawables not overlapping properly

I am creating custom rating bar.The problem is that drawables are not overlaying properly,because there are two different images with same hight and width.It should be like this when the progress will be updated the default image should be replaced by the image2. Any idea?
<RatingBar
android:layout_weight="1"
android:numStars="5"
android:stepSize="1"
android:progressDrawable="#drawable/customselector"
android:layout_width="wrap_content"
android:layout_height="0dp"/>
Here is my custom selector
<?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/image1" />
<item android:id="#android:id/secondaryProgress"
android:drawable="#drawable/image1" />
<item android:id="#android:id/progress"
android:drawable="#drawable/image2" />
</layer-list>
The way I set the selector to the RatingBar is a bit different, but I don't know if it will help:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ratingBar"
android:rating="3"
android:numStars="5"
android:stepSize="1"
android:isIndicator="false"
style="#style/mileageRatingBar"/>
In your values/styles.xml file put this:
<style name="mileageRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/rating_stars</item>
<item name="android:minHeight">32dip</item>
<item name="android:maxHeight">32dip</item>
</style>
You can of course change the size. And as you can see here is the reference to the custom drawable.
Remove
android:progressDrawable="#drawable/customselector"
and add style in your values/styles.xml
to be like this
<RatingBar
android:layout_weight="1"
android:numStars="5"
android:stepSize="1"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/customRatingBar"/>
your style from styles.xml
<style name="customRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/customselector</item>
<item name="android:minHeight">32dip</item>
<item name="android:maxHeight">32dip</item>
</style>
the customselector.xml file must place in drawable folder
<?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/image1" />
<item android:id="#android:id/secondaryProgress"
android:drawable="#drawable/image1" />
<item android:id="#android:id/progress"
android:drawable="#drawable/image2" />
</layer-list>
make sure item id is written correctly , then you have two option to assign drawable as image or put your selector into your drawable folder selector.xml with your custom setting
instead of image1 put ratingbar_empty.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/image1" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/image1" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/image1" />
<item android:drawable="#drawable/image1" />
</selector>
the same to progress but change selector to image2

Change <Button> color onClick?

<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/btnAdd"
android:background="#ff6347"
android:textColor="#f8f8f8"
android:layout_marginTop="36dp"
android:clickable="true"
android:layout_below="#+id/txtAddress"
android:layout_centerHorizontal="true"
android:focusable="true" />
This is my XML Button element and it's fine and dandy...but I want it to be known when it's pressed. How can I go about making it have different styling when pressed vs when not pressed?
You can use a selector and use a drawables for different states.
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
You can also use OnTouchListener and on ACTION_DOWN and ACTION_UP set the color to the buttons
You want to use a selector as the background for the button. Basically you can define the the different shape you want for each state of the button and the shape consists of color, corner radius, and other cool things.
XML file saved at res/drawable/button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"> <!-- pressed -->
<shape android:shape="rectangle" >
<solid android:color="#color/blue" />
</shape>
</item>
<item> <!-- default -->
<shape android:shape="rectangle" >
<solid android:color="#color/red" />
</shape>
</item>
</selector>
This layout XML applies the state list drawable to a Button:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />

Categories