white padding and image from bitmap - android

I have the following xml
<item
android:state_checked="true" >
<layer-list>
<item>
<shape android:shape="oval">
<solid android:color="#color/White" />
</shape>
</item>
<!-- Second layer is a arrow bitmap -->
<item>
<bitmap android:src="#drawable/playbtn" />
</item>
</layer-list>
</item>
What I want is the image/bitmap to be loaded from the original size and the white padding to be 10dp.
How can I achieve that? What's happening is that the image is scaled depending if I add the padding/size tags above

Use the following code:
item_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">
<solid android:color="#android:color/white" />
</shape>
</item>
<!-- Second layer is a arrow bitmap -->
<item android:drawable="#drawable/playbtn"
android:top="10dp" android:bottom="10dp"
android:left="10dp" android:right="10dp" />
</layer-list>
item_states.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/item_checked" />
<item android:drawable="#drawable/item_default" />
</selector>

Related

How to change the bottom navigation indicator width

activity_main_tab_background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<layer-list>
<item android:gravity="top">
<shape android:shape="rectangle">
<size android:height="#dimen/_5fdp"/>
<solid android:color="#color/red"/>
<corners android:bottomLeftRadius="#dimen/_3fdp"
android:bottomRightRadius="#dimen/_3fdp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
bottom navigation in main activity
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/main_bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#drawable/activity_main_tab_background"
app:labelVisibilityMode="unlabeled"
app:menu="#menu/home_bottom_nav" />
My output
expected output
How can I change the indicator width?
<size android:height="#dimen/_5fdp" android:width="#dimen/_10fdp"/> // tried and not working
You can tackle this by designating the desired width in your drawable and use a center or center_horizontal gravity besides the top:
For API Level 23+:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<layer-list>
<item android:width="50dp" android:gravity="top|center_horizontal">
<shape android:shape="rectangle">
<size android:height="#dimen/_5fdp" />
<solid android:color="#color/red" />
<corners android:bottomLeftRadius="#dimen/_3fdp" android:bottomRightRadius="#dimen/_3fdp" />
</shape>
</item>
</layer-list>
</item>
</selector>
UPDATE:
The down side of the above is that the android:width is available at API level 23. But you can use it within the <size> tag to be available on any API level, and the result is the same:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<layer-list>
<item android:gravity="top|center_horizontal">
<shape android:shape="rectangle">
<size android:width="50dp" android:height="#dimen/_5fdp" />
<solid android:color="#color/red" />
<corners android:bottomLeftRadius="#dimen/_3fdp" android:bottomRightRadius="#dimen/_3fdp" />
</shape>
</item>
</layer-list>
</item>
</selector>
Preview:

Displaying the arrow of an Android spinner

I have a spinner, whose background is defined in a drawable file:
<style name="spinner_style" parent="android:Widget.Material.Light.Spinner">
<item name="android:background">#drawable/background_spinner</item>
</style>
And the style:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/red" />
<corners android:radius="3dp" />
</shape>
The problem is that the arrow isn't displayed. It's due to: <item name="android:background">#drawable/background_spinner</item>.
How could I add it, for example within the drawable file?
Solution:
You should define an <layer-list> to do so:
<?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="0.33dp" android:color="#0fb1fa" />
<corners android:radius="0dp" />
<padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
</shape>
</item>
<item android:right="5dp">
<bitmap android:gravity="center_vertical|right" android:src="#drawable/arrow_down_gray" />
</item>
</layer-list>
</item>
</selector>
Then, replace the line: android:src="#drawable/arrow_down_gray" with your arrow drawable.
Finally, apply this as your style="#style/spinner_style" in your spinner.
Explaination: <layer-list> info can be found here: https://developer.android.com/guide/topics/resources/drawable-resource#LayerList and <bitmap> are used to draw an image on the screen. We choose the drawable image and set the gravity.
More info on this may be other senior experts can provide you.
Hope it helps.

Android EditText bottom line color and theme changes

hello all i need to remove the edittext default style i want to remove the selected portion from both the side on focus of edittext or on normal edittext i just want the simple black line on normal edittext and blue line on focus
I have created 1 xml:
<?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="line">
<!-- Draw a 2dp width border around shape -->
<stroke android:width="1dp" android:color="#color/blue" />
</shape>
<!-- Overlap the left, top and right border using background color -->
<item
android:bottom="1dp"
>
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
</shape>
</item> <!-- pressed -->
</item>
<item android:state_focused="true"><shape android:shape="rectangle">
<!-- Draw a 2dp width border around shape -->
<stroke android:width="1dp" android:color="#color/blue" />
</shape></item>
<!-- focused -->
<item><shape android:shape="rectangle">
<!-- Draw a 2dp width border around shape -->
<stroke android:width="1dp" android:color="#color/orange" />
</shape></item>
<!-- default -->
</selector>
but it giving me RECTANGLE i just want LINE #bottom
Create the new xml file inside drawable folder.
lets say
a.xml
<?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="#android:color/transparent" /> <!--background color of box-->
</shape>
</item>
<item
android:top="-2dp"
android:right="-2dp"
android:left="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="<Focus color>" /> <!-- color of stroke -->
</shape>
</item>
</layer-list>
Create another new xml file in drawable, lets say
b.xml
<?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="#android:color/transparent" /> <!--background color of box-->
</shape>
</item>
<item
android:top="-2dp"
android:right="-2dp"
android:left="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#8AC42F" /> <!-- color of stroke -->
</shape>
</item>
</layer-list>
Create another new xml file in drawable.
lets say
c.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true"
android:drawable="#drawable/a"></item>
<item android:drawable="#drawable/b">
</item>
</selector>
Try this code.
this is a selector file. it has to be made background to the edittext.
selector automatically switches between two drawable file acording to its (edittext's) state.
Using This EditText
<EditText
android:id="#+id/id_Login_emal_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/c"
android:ems="10"
android:padding="10dip"
android:hint="Email"
android:inputType="textEmailAddress"
>
Use properties that define color for edit text that is colorControlActivated, colorControlHighlight and colorControlNormal in your app theme for style.xml.
<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">#color/blackColor</item>
<item name="colorControlActivated">#color/blueColor</item>
<item name="colorControlHighlight">#color/blueColor</item>
</style>
Use Selector for different edittext states:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/twitter_im_edittext_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/twitter_im_edittext_normal" />
<item android:state_pressed="true" android:drawable="#drawable/twitter_im_edittext_normal" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#drawable/twitter_im_edittext_focused" />
<item android:state_enabled="true" android:drawable="#drawable/twitter_im_edittext_normal" />
<item android:state_focused="true" android:drawable="#drawable/twitter_im_edittext_focused" />
<item android:drawable="#drawable/twitter_im_edittext_normal" />
</selector>
and your drawable would be as Jayanth sample:
<?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="#android:color/transparent" /> <!--background color of box-->
</shape>
</item>
<item
android:top="-2dp"
android:right="-2dp"
android:left="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#8AC42F" /> <!-- color of stroke -->
</shape>
</item>
</layer-list>
Try this code:
edit_text.xml
<EditText
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/edit_text"
android:background="#drawable/custom_edit_text_black_line"
/>
Put the Xml file in Drawable Folder (custom_edit_text_black_line.xml)
<?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="#android:color/black" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>
Put the Xml file in Drawable Folder (custom_edit_text_blue_line.xml)
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryDark" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>
And in Activity write the below code :
edit_text.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
edit_text.setBackground(MainActivity.this.getResources().getDrawable(R.drawable.custom_edit_text_blue_line));
} else{
edit_text.setBackground(MainActivity.this.getResources().getDrawable(R.drawable.custom_edit_text_black_line));
}
}
});

How to do an outer glow via xml on button?

I have a customized button with rounded corners, I put some shadow for when it's pressed but I want to do an outer shadow just to the bottom part of the button, I am making the drawable via xml, so if the glow could be that way would be great.
These are the relevant parts of the code:
button_pressed_shadows.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<gradient
android:startColor="#color/black_overlay"
android:endColor="#color/btn_login"
android:angle="270"/>
<corners android:radius="4dip" />
</shape>
</item>
<item
android:top="2px">
<shape android:shape="rectangle">
<solid android:color="#color/btn_login"/>
<corners android:radius="4dip" />
</shape>
</item>
</layer-list>
style_login.xml
<?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/button_pressed_shadows" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_login" /> <!-- default -->
</selector>
Since I have the button design on photoshop, I made a 9 patch image with it, and put it on the style selector, everything went fine, think is the better (easiest) way.
Also while using layer-list you can use appropriate color combinations and padding to make it like glow or shadow.
edit_text_background
<?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="#fff" />
<corners android:radius="4dp" />
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle" >
<corners android:radius="4dp" />
<stroke
android:width="1dp"
android:color="#dadad7" />
<solid android:color="#fff" />
</shape>
</item>

Android Using layer-list for button selector

How do we use a layer-list as a drawable for a button.
I have a button:
<item android:state_pressed="true">
<shape>
<gradient android:endColor="#color/white"
android:startColor="#color/grey_blue_light" android:angle="90" />
<stroke android:width="1dp" android:color="#color/aqua_blue" />
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true">
</item>
<item>
</item>
Now I need a layer-list to be used as a shape when say button state is pressed:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/aqua_blue" />
</shape>
</item>
<item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp">
<shape android:shape="oval">
<solid android:color="#color/aqua_blue" />
</shape>
</item>
How do we use this layer list in the button selector?
Step-1
create three different layer_list xml under drawable folder for three different state of button. example the name of those xml is layer1.xml, layer2.xml, layer3.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<gradient
android:angle="270"
android:startColor="#0000ff"
android:endColor="#0000dd"
android:type="linear"
/>
</shape>
</item>
</layer-list>
Step-2
create a selector xml named as btn_background.xml and pass the layer_list xml in drawable attribute
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/layer1">
</item>
<item android:state_focused="true" android:drawable="#drawable/layer2">
</item>
<item android:drawable="#drawable/layer3">
</item>
</selector>
step-3
Set the selector xml as background of the button android:background="#drawable/btn_background"
Mygod's answer is actually better than the accepted one. It works and only needs one xml file. Here's something similar that I did. It sets the background under a drawable with transparencies. The same could be done with a background and a shape:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<layer-list>
<item android:drawable="#color/Black"/>
<item android:drawable="#drawable/notes_white"/>
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="#color/White"/>
<item android:drawable="#drawable/notes_black"/>
</layer-list>
</item>
</selector>
Just replace the shape tag with your layer-list and everything will work fine.

Categories

Resources