I have defined a style for Linear layout but the layout is not visible in graphical view of main.xml
main.xml.
<LinearLayout style="VerticalThemeLayoutInputs">
styles.xml
<resources>
<style name="VerticalThemeLayoutInputs">
<item name="android:background">#6699FF</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
</style>
</resources>
Edit:
But below code will work fine but I don't want this code
<LinearLayout
android:background="#6699FF"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
Interesting Fact
I used a scrollview. Within that I placed many Linear layout. Layouts properties are almost same so I decided to have a style for that. But what is happening know? the layout is not visible only in graphical-view but it works fine in emulator.
But actually what is happening? I think its due to Scroll view.
I think what you want is style="#style/VerticalThemeLayoutInputs".
E.g.
<LinearLayout
style="#style/VerticalThemeLayoutInputs">
main.xml in res/layout/:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/VerticalThemeLayoutInputs"
>
</LinearLayout>
styles.xml in res/values/:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="VerticalThemeLayoutInputs">
<item name="android:background">#6699FF</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:orientation">horizontal</item>
<item name="android:gravity">center_vertical</item>
</style>
</resources>
onCreate method in activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
<item name="android:layout_marginTop">#dimen/margin_top</item>
remove this tag and check.. this seems to be an invalid one.. and in main.xml add style="#style/VerticalThemeLayoutInputs" just like the above answer given by steve
Are you sure from that the style.xml located in Value folder NOT in Ar/EN/..
because he called the default in the first "value\style.xml "
Related
I have a view which is populated by a ListView and HeaderView , my problem is a left margin is applied to the HeaderView from unknown source because I have not applied any layout margin or padding to the layout, neither via xml nor programmatically.
so is this a normal ListView behavior ?
Listview layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/archos_list_view"
style="#style/ArchosGridView"
android:layout_alignWithParentIfMissing="true"
android:divider="#null"
android:dividerHeight="#dimen/content_list_vertical_spacing_between_items" />
</RelativeLayout>
I found the issue by doing breakpoint to find the parent of the headerview and found the ListView which was the source of the issue, it was the style="#style/ArchosGridView" that was applied to the ListView , so here is the source of the issue
ArchosGridView
<style name="ArchosGridView">
<item name="android:id">#id/browser_items</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:paddingTop">10dip</item>
<item name="android:paddingLeft">#dimen/content_list_left_margin</item>
<item name="android:paddingRight">#dimen/content_list_right_margin</item>
<item name="android:clipToPadding">false</item>
<item name="android:textSize">18sp</item>
<item name="android:drawSelectorOnTop">false</item>
<item name="android:fastScrollEnabled">false</item><!-- true lead to errors on 4.0 -->
<item name="android:choiceMode">singleChoice</item>
<item name="android:cacheColorHint">#00000000</item>
<item name="android:textFilterEnabled">true</item>
<item name="android:scrollbarStyle">outsideOverlay</item>
</style>
the issue was the #dimen/content_list_left_margin of 4 dip was applied (exactly the amount that I expected) but the right one was 0,
<dimen name="content_list_left_margin">4dip</dimen>
<dimen name="content_list_right_margin">0dip</dimen>
so I changed the 4dip to 0dip and problem solved
Help! Using Xamarin on VS2017, I'm trying to achieve this:
A MyHeading class that I can use anywhere just like a normal TextView class, except that any of the MyHeading instances will inherit the padding/margin/styles/font etc that I only define for MyHeading.
I'm lost and having no luck and can't find anything useful on Google.
I've created MyHeading.cs which inherits from TextView:
public class MyHeading : TextView
{
public MyHeading(Context context) : base(context)
{
}
}
Then I've also created myheading.xml in the Resources/layout folder that contains this XML:
<?xml version="1.0" encoding="utf-8"?>
<com.my.app.MyHeading
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/mygrey"
android:textSize="18dp"
android:textStyle="bold"
android:typeface="sans"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp" />
Please help! How can I achieve such a simple requirement i.e. to have a re-usable TextView with the same style whenever I use it.
With my code above, the project builds successfully, but when I run it, it bombs out with this:
Java.Lang.NoClassDefFoundError: android.support.v7.appcompat.R$drawable
As the comments suggest (and I as well), creating a style is more appropriate and a much more common pattern on Android than creating View subclasses.
Example
Resources/values/styles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyHeadingStyle" parent="android:Widget.TextView">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#color/red</item>
<item name="android:textSize">18dp</item>
<item name="android:textStyle">bold</item>
<item name="android:typeface">sans</item>
<item name="android:gravity">center</item>
<item name="android:layout_marginTop">20dp</item>
<item name="android:layout_marginBottom">10dp</item>
</style>
~~~ other styles ~~~
</resources>
AXML Example:
<TextView
android:text="StackOverFlow"
style="#style/MyHeadingStyle" />
Output:
Re: Android Styles and Themes
The ListView divider is not visible in my Android Xamarin app (Visual Studio). Unfortunately
this solution
is not working.
Here is my theme.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="bg">#ffffff</color>
<style name="MyTheme">
<item name="android:colorAccent">#000000</item>
<item name="android:colorPrimary">#ff2f4453</item>
<item name="android:colorPrimaryDark">#000000</item>
<item name="android:colorButtonNormal">#ff527791</item>
<item name="android:navigationBarColor">#ff527791</item>
<item name="android:textColorPrimary">#ff000000</item>
<item name="android:textColorSecondary">#ffffffff</item>
<item name="android:colorActivatedHighlight">#000000</item>
<item name="android:divider">#B6B6B6</item>
<item name="android:dividerHeight">1px</item>
<item name="android:windowBackground">#color/bg</item>
</style>
</resources>
and here the Main.xml from the activity with the ListView-Control:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:p1="http://schemas.android.com/apk/res/android"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="#+id/usersList">
</ListView>
When i change the theme to one of the defaults, the divider is shown correctly, but when i use my own theme, it's no more visible.
Did i forgot something to add in the colors?
Thanks in Advance!
Try this:
android:dividerHeight="5dp"
In my project I use special fonts for my buttons. So I've added the PixlUI library so I can set the font in xml.
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_login"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/login"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_register"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/make_new_profile"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_broker_register"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/make_new_profile_broker"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
These are my buttons, since they all have the same typeface I want to include the typeface in the 'custom_button_style'
This my custom style:
<style name="custom_button_style" parent="#android:style/Widget.Button">
<item name="android:textSize">#dimen/hello_medium_fontsize</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#drawable/custom_btn_background</item>
<item name="android:layout_marginBottom">#dimen/login_button_margin</item>
</style>
How do I include
pixlui:typeface
in this style?
It´s not clear if we can´t see the complete xml layout file, but things that often happens is that the poeple forget to add the xmlns attribute to the parent. You parent layout should look something like this. It´s part of this library and the important thing is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui" <----thats the important attribute
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
You don´t need to add something in Your style.xml. So if this does not fix Your problem, come back.
EDIT
It is not tested yet, but for the style it could be:
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui">
<style "custom_button_style" parent="#android:style/Widget.Button">
<item name="android:textSize">#dimen/hello_medium_fontsize</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#drawable/custom_btn_background</item>
<item name="android:layout_marginBottom">#dimen/login_button_margin</item>
<item name="pixlui:typeface">AvenirNextCondensed-Regular.ttf</item>
</style>
I'm trying to customize ToggleButton in my app. I'm setting 9-patch image as background as written here.
And then in my layout xml:
<ToggleButton
android:id="#+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/btn_toggle_bg"
android:checked="true"
android:gravity="center_horizontal|center_vertical" />
btn_toogle_bg.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="#android:color/transparent"/>
<item
android:id="#+android:id/toggle"
android:drawable="#drawable/btn_toggle"/>
</layer-list>
btn_toggle.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_toggle_off" android:state_checked="false"/>
<item android:drawable="#drawable/btn_toggle_on" android:state_checked="true"/>
</selector>
9-patch looks like this (btn_toggle_off):
The same image for checked state.
But when I'm applying this as background I get some unexpected top and bottom padding:
I've got the same result when applying this background to Button or using xml-drawable as background.
How to avoid unexpected padding? Help, please.
Update:
Also, when I'm adding this style to the app theme there is no padding but ToggleButton becames unclickable (doesn't change its state):
styles.xml:
<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">#drawable/btn_toggle_bg</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
</style>
themes.xml:
<style name="MyThemeName" parent="#android:Theme.Black">
<item name="android:buttonStyleToggle">#style/Widget.Button.Toggle</item>
</style>
It seems that I needed to extend android:Widget.Button.Toggle style:
<style name="Widget.Button.Toggle" parent="android:Widget.Button.Toggle">
<item name="android:background">#drawable/btn_toggle_bg</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
</style>
The reason for the padding is your right black pixel that describes where the content should be placed, should be the whole length of the image from top to bottom.
try this: