android - Specific styles for individual views -


short question: possible create theme , set specific styles individual views?

lets want text color of textview in red , text color edittext in green.

somithing (but isn't working):

<style name="myapptheme" parent="android:theme.light">       <item name="android:windownotitle">true</item>     <item name="android:textcolor">#0000ff</item>      <style  parent="@android:style/widget.edittext">         <item name="android:textcolor">#ff0000</item>      </style> </style> 

update:

the core element of app views draw on own. support theming have abstract “theme” base class has methods provide colors , drawables need. class derive several classes “blacktheme”, “whitetheme”… user can set right theme in settings. every additional information need user use dialogfragment default android widgets. in dialogfragments apply dialog style in oncreateview.

@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {      releasenotespopup viewlayout = new releasenotespopup(getactivity(), _callback, this);      getdialog().requestwindowfeature(window.feature_no_title);     getdialog().getwindow().setsoftinputmode(style_no_input);       viewlayout.setbackgrounddrawable(this.getresources().getdrawable(_theme.popupbackground()));      return viewlayout; } 

the core problem have when have black background widgets become invisible. therefore need custom selector's.unfortunately can't apply widget style programmatically. thats why way joseph described in answer: attrs.xml

<resources>  <attr name="myedittextstyle" format="reference" /> <attr name="mycheckboxstyle" format="reference" /> 

styles.xml

<style name="whitetheme" parent="appbasetheme">     <item name="android:windownotitle">true</item> </style>   <style name="appthemeblack" parent="appbasetheme">       <item name="android:windownotitle">true</item>     <item name="android:textcolor">#b1b1b1</item>      <item name="myedittextstyle">@style/edittextmytime</item>      <item name="mycheckboxstyle">@style/checkboxmytime</item> </style> 

i have added: style="?attr/mycheckboxstyle" of checkboxes. if blacktheme active settheme(r.style.blacktheme); in activity.oncreate()

for blacktheme need special selectors because default checked/unchecked icons invisible when background black.

for theme higher contrast, lets whitetheme, don't set item "mycheckboxstyle".

this implementaion works guess not optimal...

update2:

here checkbox style. downloaded from:http://android-holo-colors.com/

<?xml version="1.0" encoding="utf-8"?>   <resources xmlns:android="http://schemas.android.com/apk/res/android">  <style name="edittextmytime" parent="android:widget.edittext">   <item name="android:background">@drawable/mytime_edit_text_holo_dark</item>   <item name="android:textcolor">#ffffff</item> </style>   <style name="checkboxmytime" parent="android:widget.compoundbutton.checkbox">   <item name="android:button">@drawable/mytime_btn_check_holo_dark</item> </style>  </resources> 

and

<?xml version="1.0" encoding="utf-8"?>   <selector xmlns:android="http://schemas.android.com/apk/res/android">  <!-- enabled states -->    <item android:drawable="@drawable/mytime_btn_check_on_holo_dark" android:state_checked="true" android:state_enabled="true" android:state_window_focused="false"/> <item android:drawable="@drawable/mytime_btn_check_off_holo_dark" android:state_checked="false" android:state_enabled="true" android:state_window_focused="false"/> <item android:drawable="@drawable/mytime_btn_check_on_pressed_holo_dark" android:state_checked="true" android:state_enabled="true" android:state_pressed="true"/> <item android:drawable="@drawable/mytime_btn_check_off_pressed_holo_dark" android:state_checked="false" android:state_enabled="true" android:state_pressed="true"/> <item android:drawable="@drawable/mytime_btn_check_on_focused_holo_dark" android:state_checked="true" android:state_enabled="true" android:state_focused="true"/> <item android:drawable="@drawable/mytime_btn_check_off_focused_holo_dark" android:state_checked="false" android:state_enabled="true" android:state_focused="true"/> <item android:drawable="@drawable/mytime_btn_check_off_holo_dark" android:state_checked="false" android:state_enabled="true"/> <item android:drawable="@drawable/mytime_btn_check_on_holo_dark" android:state_checked="true" android:state_enabled="true"/>  <!-- disabled states -->  <item android:drawable="@drawable/mytime_btn_check_on_disabled_holo_dark" android:state_checked="true" android:state_window_focused="false"/> <item android:drawable="@drawable/mytime_btn_check_off_disabled_holo_dark" android:state_checked="false" android:state_window_focused="false"/> <item android:drawable="@drawable/mytime_btn_check_on_disabled_focused_holo_dark" android:state_checked="true" android:state_focused="true"/> <item android:drawable="@drawable/mytime_btn_check_off_disabled_focused_holo_dark" android:state_checked="false" android:state_focused="true"/> <item android:drawable="@drawable/mytime_btn_check_off_disabled_holo_dark" android:state_checked="false"/> <item android:drawable="@drawable/mytime_btn_check_on_disabled_holo_dark" android:state_checked="true"/>  </selector> 

cheers, stefan

if want full control on individual elements (i.e. 2 textviews on page might different, or textviews on different pages might different) want controllable theme:

res/values/styles.xml

<resources>     <style name="myapptheme" parent="android:theme.light">         <item name="android:windownotitle">true</item>         <item name="mytextappearance">@style/mytextappearance</item>         <item name="myedittextstyle">@style/myedittextstyle</item>     </style>      <style name="mytextappearance" parent="android:textappearance">         <item name="android:textcolor">#0000ff</item>     </style>      <style name="myedittextstyle" parent="android:widget.edittext">         <item name="android:textcolor">#ff0000</item>     </style> </resources> 

res/values/attrs.xml

<resources>     <attr name="mytextappearance" format="reference" />     <attr name="myedittextstyle" format="reference" /> </resources> 

res/layout/layout.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <textview         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="hello world"         android:textappearance="?attr/mytextappearance"         />      <edittext         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="message here"         style="?attr/myedittextstyle"         />  </linearlayout> 

if don't want control on individual views, can override defaults:

res/values/styles.xml

<resources>     <style name="myapptheme" parent="android:theme.light">         <item name="android:windownotitle">true</item>         <item name="android:textcolorprimary">#0000ff</item>         <item name="android:edittextstyle">@style/myedittextstyle</item>     </style>      <style name="myedittextstyle" parent="android:widget.edittext">         <item name="android:textcolor">#ff0000</item>     </style> </resources> 

res/layout/layout.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <textview         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="hello world"         />      <edittext         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="message here"         />  </linearlayout> 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -