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
Related
I was doing something like this in layout file
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textColorHint="#color/colorPrimary"
app:hintTextAppearance="#color/colorPrimary">
But i want to use this style at multiple place so i tried to move it in style.xml file
<style name="FindInstituteInputTextViewStyle" >
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:layout_marginBottom">5dp</item>
<item name="android:textColorHint">#color/colorPrimary</item>
<item name="app:hintTextAppearence">colorPrimary</item>
</style>
But now this give me following exceptions.
Error:(2376, 21) No resource found that matches the given name: attr 'app:hintTextAppearence'.
I also tried placing app namespace attr in < resource >
tag inside style.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
But again it didn't work for me. Please help me and sorry if i am doing some silly mistake
<style name="FindInstituteInputTextViewStyle" >
... ...
<item name="hintTextAppearence">#color/colorPrimary</item>
</style>
You did not create own tag, so trying to use such will cause error as you see it. Own styles you created are used by reference with existing tags, no namespace needed. i.e.:
<TextView
style="#style/FindInstituteInputTextViewStyle" /
See docs: https://developer.android.com/guide/topics/ui/themes.html
Also this line:
<item name="app:hintTextAppearence">colorPrimary</item>
does not seem to refer to color properly, so it should not even compile.
I have a custom-styled checkbox that I'd like to replicate a number of times in my Android app. The XML for this checkbox is:
<CheckBox
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_large"
android:text="Temporary Text"/>
Obviously, I can simply copy the XML and paste it lower down in my layout file, but in the event that I want to change the design I would have to do it for every pasted checkbox.
Is there a clean way to create a custom template for this checkbox, or should I be implementing this in code?
Is there a clean way to create a custom template for this checkbox, or should I be implementing this in code?
You can use styles. Create
<style name="MyCheckbox">
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">#drawable/checkbox_large</item>
<item name="android:text">Temporary Text</item>
</style>
and then just apply to all checkboxes you want:
<CheckBox
style="#style/MyCheckbox"/>
When in need of change, just alter your style.
edit:
for anyone coming across this, the way I got it to work is to either:
Set the layout directly for each preference item, as in:
<CheckboxPreference
android:layout="#layout/checkbox_preference_item"/>
OR set the style via a theme as I was doing before, but define a widgetlayout and set it instead of an actual layout, like so, where preferenceStyle is set to #style/Preference and checkBoxPreferenceStyle is set to #style/Preference.Checkbox:
<Style name="Preference">
<item name="android:layout">#layout/checkbox_preference_item</item>
</Style>
<Style name="Preference.Checkbox">
<item name="android:widgetLayout">#layout/your_custom_widget</item>
</Style>
I'm trying to customize the layout of a Checkbox Preference item and the checkbox itself is not showing up. I'm modeling after android code and not sure where I'm going wrong. The text views are working fine as well as the rest of the layout but the checkbox is not showing up. Would appreciate any help with this.
Relevant code:
theme.xml:
<style name="Theme.Settings" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/SettingsActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:preferenceCategoryStyle">#style/PreferenceCategory</item>
<item name="android:preferenceStyle">#style/Preference</item>
<item name="android:checkBoxPreferenceStyle">#style/Checkbox</item>
</style>
style.xml
<style name="Checkbox">
<item name="android:layout">#layout/checkbox_preference_item</item>
</style>
<style name="Preference.Text">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textSize">#dimen/preferences_item_font</item>
<item name="android:textColor">#color/preference_text</item>
</style>
<style name="Preference.Text.Extra" parent="Preference.Text">
<item name="android:textSize">#dimen/preferences_header_font</item>
</style>
checkbox_preference_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/preferences_item_height"
android:background="#color/white"
android:paddingLeft="#dimen/preferences_item_left">
<LinearLayout android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center_vertical">
<TextView android:id="#android:id/title"
style="#style/Preference.Text"/>
<TextView android:id="#android:id/summary"
style="#style/Preference.Text.Extra"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible"
android:id="#android:id/widget_frame"
android:layout_alignParentRight="true"/>
</RelativeLayout>
I am not seeing a CheckBox in the layout xml. Am I missing something? You need to specify something like:
<CheckBox android:id="..." ... />
Then you may need to specify something like
<CheckBoxPreference android:key="..." ... />
in the preference xml file. Setting should be android:widgetLayout to the custom CheckBox.
See this post may help you more customizing checkbox preference
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>
In HTML/CSS you can override all paragraphs by defining p in CSS
I.e.
p { color: blue;
}
Can you do mimc this in Android? I'd like to define my set of styles for all form elements in styles.xml and then every layout I write would use these properties.
At the moment I have to specify the style in the layout: i.e
<EditText android:id="#+id/address1" style="#style/EditText" />
Which seems a bit redundant (and error prone).
Thanks,
John
You have to create an xml file which defines the style that you want, and store it in res/values folder.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
This is an example of an style, once you have your style. you can applay it to every view by doing something like this:
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
This example is from:
http://developer.android.com/guide/topics/ui/themes.html