I am trying to style a RelativeLayout from styles.xml because I have different themes and I am trying to change it's background. How do I do that?
I know that styling a ListView goes like this (for example):
<style name="Theme_Day" parent="Theme.Sherlock.Light">
<item name="android:textColor">#color/def_actionbar_submenu_text</item>
<item name="android:listViewStyle">#style/def_ListView</item>
<item name="vpiTabPageIndicatorStyle">#style/CustomTabPageIndicator</item>
</style>
<style name="def_ListView" parent="#android:style/Widget.ListView">
<item name="android:background">#drawable/listview_bg</item>
<item name="android:textColor">#color/def_listview_title_text</item>
<item name="android:divider">#color/def_listview_itemdivider_bg</item>
<item name="android:dividerHeight">#dimen/listview_divider_height</item>
</style>
but when I try to find "android:relativeLayoutStyle" or parent="#android:style/Widget.RelativeLayout" I cant find anything..
How do I do that?
This question has been asked before a long time ago, but you can always try like this:
<style name="RelativeLayoutStyle" parent="#android:style/Widget">
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginTop">7dp</item>
<item name="android:layout_marginBottom">5dp</item>
<item name="android:background">#android:color/holo_green_dark</item>
</style>
Now you can do:
<RelativeLayout
style="#style/RelativeLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
Hope this helps someone.
You don't need any parent just write
<style name="def_RelativeLayout">
<item name="android:background">#drawable/listview_bg</item>
</style>
and set it in your style attribute of every relative layout
Create a drawable file and then set android:background with the #drawable/layout_style property.
For example, this drawable file is used to create a Google Card like design:
background_card.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle"
android:dither="true">
<corners android:radius="2dp"/>
<solid android:color="#ccc" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle"
android:dither="true">
<corners android:radius="2dp" />
<solid android:color="#android:color/white" />
<padding android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
</shape>
</item>
</layer-list>
And from the layout XML file, you need to set the android:background property:
android:background="#drawable/backgorund_card"
_
<RelativeLayout
android:id="#+id/layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgorund_card" >
Related
If boxBackgroundColor is transparent then boxStroke is invisible
<style name="OutlinedRoundedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxBackgroundColor">#android:color/transparent</item>
<item name="boxStrokeColor">#android:color/white</item>
<item name="boxStrokeWidth">4dp</item>
</style>
So is it only possible to have transparent TextInputLayout without outline using custom background?
E.g.:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="5dp" />
<stroke
android:width="2dp"
android:color="#color/colorEditTextOutline" />
</shape>
</item>
</layer-list>
If you had posted a design for it then it would have been easier understand. For this solution If I understand you correctly, you need to add these two things
In style add:
<style name="InputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#color/text_input_box</item>
</style>
Add color resource:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/red_bright" android:state_focused="true" />
<item android:color="#color/red_bright" android:state_hovered="true" />
<item android:color="#color/red_bright" />
</selector>
This solution will have something like this if this is what you want
I have tried to move my content below the status bar with success. Used the following code:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
But the issue is that i have no status bar shadow. How do i achieve it?
shadows_bottom.xml design and color the way you like it to be
<item name="android:windowContentOverlay">#drawable/shadows_bottom</item>
Example shadows_bottom.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/darker_gray" />
<corners android:radius="5dp"/>
</shape>
</item>
<item android:right="1dp" android:left="1dp" android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
And you also need to set colorPrimary and colorPrimaryDark following official guidelines
here: https://www.materialpalette.com
Add this line in your style.xml code theme:
<item name="android:windowContentOverlay">#null</item>
In Api > lollipop you can set statusBar color, and it haven't any shadows.
Just write in your xml with theme:
<item name="colorPrimary">#color/toolbar_color</item>
<item name="colorPrimaryDark">#color/status_bar_color</item>
I have a menu and I would like to apply the following background style:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="10dip" />
<stroke android:color="#color/pink"
android:width="6dip"></stroke>
<solid android:color="#color/white"></solid>
</shape>
I have a Java class just to treat the operations related with the menu. This class extends ActionBarActivity.
The code of my menu is the following:
<menu 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"
tools:context="com.android.ExtendedActivity">
<item android:id="#+id/action_share"
android:title="Share"
android:orderInCategory="100"/>
<item android:id="#+id/action_about"
android:title="About"
android:orderInCategory="100"/>
</menu>
Does anyone know how can I set that background style to my menu?
In your style you will have something like this
<style name="YourTheme.Main" parent="Theme.AppCompat.Light.NoActionBar">
... <item name="android:activatedBackgroundIndicator">#drawable/selectable_background</item> ... </style>
and in your drawable something like
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="#color/your_color" android:state_pressed="true"/> <item android:drawable="#drawable/selected_background" android:state_selected="true"/> <item android:drawable="#drawable/selected_background" android:state_checked="true"/> </selector>
and finally you can use your backgroud as a selected_background like this
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="10dip" /> <stroke android:color="#color/pink" android:width="6dip"></stroke> <solid android:color="#color/white"></solid> </shape>.
I'm so sorry for the very simplified explanation but without a full code demo app is very tricky! I hope this help :-)
Iv've been trying to match a button solid color with its border color while also adding a ripple effect. I've have a matching border and solid color (without a ripple) but when I add the ripple it seems to add a border of its own thats slightly darker. Below are the styles and drawable,
drawable-v21/my_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#ff0000"
tools:ignore="NewApi">
<item>
<shape
android:shape="rectangle">
<corners
android:radius="2dp" />
<stroke
android:width="1dp"
android:color="#ff0000" />
<solid
android:color="#ffffff" />
</shape>
</item>
</ripple>
styles.xml
<style name="Border_Button">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize">14sp</item>
<item name="android:textAllCaps">true</item>
<item name="android:layout_marginLeft">16dp</item>
<item name="android:layout_marginRight">16dp</item>
<item name="android:background">#drawable/my_btn</item>
</style>
and layout.xml
<Button
style="#style/Border_Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Click" />
Does anyone know how this border color can be overridden with the ripple or is there is some other way to achieve this (only XML preferrably)?
I have defined the below files in res folder.
styles_apptheme.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ButtonAppTheme" parent="android:Widget.Holo.Light.Button">
<item name="android:background">#drawable/apptheme_btn_default_holo_light</item>
</style>
</resources>
themes_apptheme.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="#style/_AppTheme"/>
<style name="_AppTheme" parent="Theme.AppCompat.Light">
<item name="android:editTextBackground">#drawable/apptheme_edit_text_holo_light</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
</style>
</resources>
In my Layout.xml file, i have defined my button as below
<Button
android:id="#+id/btnAddTitle"
android:layout_below="#id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_AddTitle"
android:layout_margin="20dp"
/>
If i add the below line to the button view above,
android:background="#style/ButtonAppTheme"
Application crashes saying that drawable resource should be set for the background attribute.
So i created the below drawable file - abc.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/apptheme_btn_default_normal_holo_light" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/apptheme_btn_default_disabled_holo_light" />
<item android:state_pressed="true"
android:drawable="#drawable/apptheme_btn_default_pressed_holo_light" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/apptheme_btn_default_focused_holo_light" />
<item android:state_enabled="true"
android:drawable="#drawable/apptheme_btn_default_normal_holo_light" />
<item android:state_focused="true"
android:drawable="#drawable/apptheme_btn_default_disabled_focused_holo_light" />
<item
android:drawable="#drawable/apptheme_btn_default_disabled_holo_light" />
</selector>
If i set android:background="#drawable/abc" i dont see the style set in the button.
So please let me know how i can set the style for the button.
Thanks.
It would be rather simple if you do it this way.
First create a button_selector.xml in drawable folder.
<?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="5dp" />
<stroke android:width="1dip" android:color="#color/green_temp" />
<gradient android:angle="-90" android:startColor="#color/green_temp" android:endColor="#color/green_temp" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="#971E05" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="#color/bright_green" />
<gradient android:angle="-90" android:startColor="#color/green_temp" android:endColor="#color/button_green" />
</shape>
</item>
</selector>
Add these colors in colors.xml of values folder.
<!-- Green -->
<color name="green_temp">#23A96E</color>
<color name="green_dark">#159204</color>
<color name="bright_green">#02D8B0</color>
<color name="button_green">#10a54a</color>
Finally in your desired button of layout.xml put background from above selector.
<Button
android:id="#+id/btnAddTitle"
android:layout_below="#id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_AddTitle"
android:background="#drawable/button_selector"
android:layout_margin="20dp"
/>
Then its all done your button will be styled with color you want.
Android Button Maker is online tool to generate buttons code for Android Apps. Android API provide Drawable Resources where XML file defines geometric shape, including colors, border and gradients.
These button is generating based on shape drawable XML code which load faster compare to normal png buttons. You can customize button properties in setting panel and get source code.
Check this link Button Maker
No need to crack brains...this tool make it simple
replace
android:background="#style/ButtonAppTheme"
with
style="#style/ButtonAppTheme"
and every thing is ok!
Try this out as alternative. create a drawable folder under res then create xml files and copypaste below code and try first then you customize as per your requirement.
button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00acc1"/>
<stroke android:width="2dp" android:color="#ffffff"/>
<corners android:radius="2dp"/> </shape>
button_bg_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#006064"/>
<stroke android:width="1dp" android:color="#ffffff"/>
<corners android:radius="2dp"/> </shape>
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/button_bg_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/button_bg_pressed" />
<item android:drawable="#drawable/button_bg" /> </selector>
And now in your button xml set the background as button_selector.xml
<Button
android:id="#+id/btnAddTitle"
android:layout_below="#id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_AddTitle"
android:background="#drawable/button_selector"
android:layout_margin="20dp"/>
This will do the job for you. You can customize your entire button style by this way.
If you want to have the default animation of button by material design and change button color, try this.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/padding_12"
android:text="button text here"
android:theme="#style/NavyBtn" />
in style.xml
<style name="NavyBtn" parent="Theme.AppCompat.Light">
<item name="colorButtonNormal">#020e39</item>
<item name="android:textColor">#FFFFFF</item>
</style>