I'm searching for a specific value adopted in default component in android.
For example the colorPrimary is adopted to background color and the colorOnPrimary is adopted to text color in default button.
I want to know where the attrs are set to the button.
The button class is like below.
#RemoteView
public class Button extends TextView {
public Button(Context context) {
this(context, null);
}
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
public Button(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public CharSequence getAccessibilityClassName() {
return Button.class.getName();
}
#Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
if (getPointerIcon() == null && isClickable() && isEnabled()) {
return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND);
}
return super.onResolvePointerIcon(event, pointerIndex);
}
}
So I searched R.attr.buttonStyle in android platform_frameworks_base repository, and found this codes, so searched again about this.
<!-- Button styles -->
<item name="buttonStyle">#style/Widget.Button</item> <!-- themes.xml -->
<!-- styles.xml -->
<style name="Widget.Button">
<item name="background">#drawable/btn_default</item>
<item name="focusable">true</item>
<item name="clickable">true</item>
<item name="textAppearance">?attr/textAppearanceSmallInverse</item>
<item name="textColor">#color/primary_text_light</item>
<item name="gravity">center_vertical|center_horizontal</item>
</style>
btn_default.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_normal_disable" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_selected" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_normal_disable_focused" />
<item
android:drawable="#drawable/btn_default_normal_disable" />
</selector>
About #drawable/btn_default_normal, it was a .9.png.
What I expected was that a background is colorPrimary, and textColor is colorOnPrimary.
But I can't find anything about this.
How can I find this?
Related
First of all I am pretty new in programming Xamarin and Android. I have created a SeekbarPreference but some how they layout does not display all correctly. The value is dropping of from the box (see picture).
My styles.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#EC6A1E</item>
<item name="colorAccent">#EC6A1E</item>
<item name="toolbarStyle">#style/custom_toolbar</item>
<item name="preferenceTheme">#style/PreferenceThemeOverlay.v14.Material.Fix</item>
</style>
<style name="custom_toolbar" parent="#style/Widget.AppCompat.Toolbar">
<item name="titleTextColor">#FFFFFF</item>
</style>
<style name="PreferenceThemeOverlay.v14.Material.Fix" parent="PreferenceThemeOverlay.v14.Material">
<item name="seekBarPreferenceStyle">#style/Preference.SeekBarPreference.Fix</item>
</style>
<style name="Preference.SeekBarPreference.Fix">
</style>
</resources>
Here is my settings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.SeekBarPreference
android:id="#+id/preference_range_seek_bar"
android:layout_height="wrap_content"
android:key="range"
android:title="Range"
android:summary="Here you can set the range of your location"
android:max="10000"
android:defaultValue="500" />
<android.support.v7.preference.SeekBarPreference
android:key="favRange"
android:title="Favorite Range"
android:summary="Here you can set the range of your Favorite location"
android:max="10000"
android:defaultValue="500" />
<android.support.v7.preference.Preference android:title="Title" android:summary="This is the summary">
</android.support.v7.preference.Preference>
</android.support.v7.preference.PreferenceScreen>
What I do not understand is how can I find which xml attributes I can use on the SeekBarPreference to fix this. When I look in Googles Documents I cannot find a description of xml attributes on this SeekBarPreference.
I know my theme is fuzzy as I played a lot with it. But when I have it working I will adjust this.
Hopefully someone could help me, or have an example..
Ran into the same issue. Here you go with the solution, testion on emulator running API 25:
Step 1, Create a new class that extends SeekBarPreference
public class CustomSeekBarPreference extends SeekBarPreference {
private TextView mSeekBarValueTextView;
public CustomSeekBarPreference(Context context,
AttributeSet attributeSet,
int i, int i1) {
super(context, attributeSet, i, i1);
}
public CustomSeekBarPreference(Context context, AttributeSet attributeSet, int i) {
super(context, attributeSet, i);
}
public CustomSeekBarPreference(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public CustomSeekBarPreference(Context context) {
super(context);
}
#Override
public void onBindViewHolder(PreferenceViewHolder preferenceViewHolder) {
super.onBindViewHolder(preferenceViewHolder);
mSeekBarValueTextView = (TextView) preferenceViewHolder.findViewById(android.support.v7.preference.R.id.seekbar_value);
if (mSeekBarValueTextView != null) {
LayoutParams layoutParams = mSeekBarValueTextView.getLayoutParams();
layoutParams.height = LayoutParams.WRAP_CONTENT;
mSeekBarValueTextView.setLayoutParams(layoutParams);
}
}
}
In your preferences.xml replace the SeekBarPreference class name with the custom class name.
<com.google.xxx.view.CustomSeekBarPreference
android:key="cacheLimit"
android:title="#string/setting_cache_limit_title"
android:dependency="enableCaching"
android:defaultValue="20"
android:max="40"/>
In my Android app I have two different themes (light and dark).
For example:
<style name="AppThemeDark" parent="Theme.AppCompat">
<item name="colorPrimary">#android:color/black</item>
<item name="colorPrimaryDark">#android:color/black</item>
<item name="colorAccent">#android:color/holo_red_dark</item>
<item name="android:textColor">#android:color/white</item>
<item name="windowActionModeOverlay">true</item>
</style>
<style name="AppThemeLight" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionModeOverlay">true</item>
</style>
So, now I can apply, for example, different text colors to a TextView (white for dark theme and black for light):
<item name="android:textViewStyle">#style/TextViewDark</item>
<style name="TextViewDark">
<item name="android:textColor">?android:attr/colorAccent</item>
</style>
But it will apply to all TextViews.
The main question, is it possible to make in XML (not programmatically) next:
Light theme: Half of TextViews text color black, and another half green.
Black theme: TextViews that black in Light theme - red, and another half - blue (which are green in Light theme).
Create 2 classes extends TextView
public class OneTextView extends TextView {
public OneTextView(Context context) {
super(context);
init(context);
}
public OneTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public OneTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context){
int[] attrs = new int[] { R.attr.myFirstColor};
TypedArray ta = context.obtainStyledAttributes(attrs);
int appColor = ta.getColor(0, 0);
ta.recycle();
// set theme color
setTextColor(appColor);
}
}
public class SecondTextView extends TextView {
public SecondTextView(Context context) {
super(context);
init(context);
}
public SecondTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SecondTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context){
int[] attrs = new int[] { R.attr.mySecondColor};
TypedArray ta = context.obtainStyledAttributes(attrs);
int appColor = ta.getColor(0, 0);
ta.recycle();
// set theme color
setTextColor(appColor);
}
}
each class you can use in xml like this
<com.route.to.class.OneTextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
OneTextView can have black and red colors
SecondTextView can have green and blue colors
define attr.xml in values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myFirstColor" format="color" />
<attr name="mySecondColor" format="color" />
</resources>
Then in your styles.xml, define colors for each theme:
<style name="Theme.MyApp" parent="#style/Theme.Light">
<item name="myFirstColor">#color/black</item>
<item name="mySecondColor">#color/green</item>
</style>
<style name="Theme.MyApp.Dark" parent="#style/Theme.Dark">
<item name="myFirstColor">#color/green</item>
<item name="mySecondColor">#color/blue</item>
</style>
you have defined in styles.xml
<style name="TextViewDark">
<item name="android:textColor">?android:attr/colorAccent</item>
</style>
<style name="TextViewLight">
<item name="android:textColor">#color/green</item>
</style>
then you can use it in main.xml
<LinearLayout>
<TextView
android:id="#+id/light_text_view"
android:text"i´m use light theme"
style="#style/TextViewLight"/>
<TextView
android:id="#+id/dark_text_view"
android:text"i´m use darktheme"
style="#style/TextViewDark"/>
</LinearLayout>
you don´t need to define styles in AppThemes
Background:
I am using SwitchPreferenceCompat of the support preference v7 library by extending it as below in order to have more than one line for the Switch title:
public class MoreLinesSwitchPreference extends SwitchPreferenceCompat {
public MoreLinesSwitchPreference(Context context) {
super(context);
}
public MoreLinesSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MoreLinesSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
TextView textView = (TextView) holder.itemView.findViewById(android.R.id.title);
if (textView != null) {
textView.setSingleLine(false);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
}
}
}
My Preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Settings">
<com.sed.al_mabadi_ul_mufeedah.MyCustomListPreference
android:defaultValue="20"
android:entries="#array/fontSize"
android:entryValues="#array/fontSize"
android:icon="#drawable/ic_format_size_24dp"
android:key="fontSize"
android:summary="20"
android:title="#string/settings_font_size" />
<com.sed.al_mabadi_ul_mufeedah.MyCustomListPreference
android:defaultValue="DroidNaskh-Regular"
android:entries="#array/font_face"
android:entryValues="#array/font_face"
android:icon="#drawable/ic_font_24dp"
android:key="fontFace"
android:summary="Choose Font Name"
android:title="#string/settings_font_name" />
<com.sed.al_mabadi_ul_mufeedah.MyCustomListPreference
android:defaultValue="en"
android:entries="#array/language"
android:entryValues="#array/language_short"
android:icon="#drawable/ic_language_24dp"
android:key="language"
android:summary="#string/app_restart"
android:title="#string/app_language_title" />
<com.sed.al_mabadi_ul_mufeedah.MoreLinesSwitchPreference
android:defaultValue="true"
android:icon="#drawable/ic_harakath_8b64ce_24dp"
android:key="diacriticsOn"
android:summary="#string/app_restart"
android:title="#string/show_hide_diacritics" />
<com.sed.al_mabadi_ul_mufeedah.MoreLinesSwitchPreference
android:defaultValue="false"
android:icon="#drawable/ic_night_24dp"
android:key="nightModeOn"
android:summary="#string/app_restart"
android:title="#string/settings_night_mode" />
<com.sed.al_mabadi_ul_mufeedah.MoreLinesSwitchPreference
android:defaultValue="false"
android:icon="#drawable/ic_screen_on"
android:key="alwaysOn"
android:summary="#string/sleep_on"
android:title="#string/settings_screen_on" />
</android.support.v7.preference.PreferenceScreen>
from my Compile dependencies:
compile 'com.android.support:preference-v7:24.2.0'
MyStyle.xml:
<style name="AppTheme.Base" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorControlActivated">#color/colorCursor</item>
<item name="colorControlHighlight">#color/colorFocus</item>
<item name="android:textColorPrimary">#color/colorPrimaryText</item>
<item name="android:textColorSecondary">#color/colorSecondaryText</item>
<item name="android:colorBackground">#color/background</item>
<item name="windowActionModeOverlay">true</item>
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
</style>
Problem:
The SwitchPreferenceCompat is not animating between ON and OFF values.
I researched:
Why SwitchPreference is not showing animation when switching from on to off & vice versa?
Extending Preference classes in Android Lollipop = losing animation
https://code.google.com/p/android/issues/detail?id=185323
Why the above does not answer my question:
The above and other posts related to SwitchPreference does not speak about SwitchPreferenceCompat rather simply Switch or SwitchPreference
Need help in animating the switch between ON and OFF values.
Thanks for any help.
The problem is fixed now.
I updated the compile dependencies from
compile 'com.android.support:preference-v7:24.2.0'
to
compile 'com.android.support:preference-v7:25.3.0'
and now the same switch preference is animating.
This could probably be a bug in the older versions of support preference.
In my Android application I need to apply style for a group of buttons, instead of styling each button individual. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<!-- put here style="#ststyle/Button_Style" -->
<Button android:id="#+id/button1" android:text="#string/b01" />
<Button android:id="#+id/button2" android:text="#string/b02" />
<Button android:id="#+id/button4" android:text="#string/b03" />
<!-- end style -->
You can write the style for button like this ;
style_btn.xml
<style name="style_btn" parent="Wrap">
<item name="android:background">#drawable/btn_bg</item>
<item name="android:gravity">center</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_marginTop">4dp</item>
<item name="android:minWidth">90dp</item>
</style>
apply that style to your button :
<Button
android:id="#+id/attach_file"
style="#style/style_btn"
android:layout_centerVertical="true"
android:background="#drawable/orange_bg"
android:drawablePadding="10dp"
android:drawableRight="#drawable/attach"
android:text="#string/str_email_attach" />
If you need the style to all of the buttons in your application, mention in your App theme style, Then no need to apply for every button.
<style name="YourTheme" parent="android:Theme.Light">
<item name="android:buttonStyle">#style/Button</item>
</style>
If you need the style to particluar button , then apply to every button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Button"
android:text="Button" />
I know I'm late joining the party but I stumbled across this when trying to figure out the same problem myself.
What I did was:
Depending on how may button groups you have (say 3 for example) you need to subclass button and create three custom button classes (see below)
//Custom button 1
public class CustomButton1 extends Button {
public CustomButton1(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.attrStyle1);
}
public CustomButton1(Context context, AttributeSet attrs, int defStyle) {
super(context, null, R.attr.attrStyle1);
}
}
//Custom button 2
public class CustomButton2 extends Button {
public CustomButton2(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.attrStyle2);
}
public CustomButton2(Context context, AttributeSet attrs, int defStyle) {
super(context, null, R.attr.attrStyle2);
}
}
//Custom button 3
public class CustomButton3 extends Button {
public CustomButton3(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.attrStyle3);
}
public CustomButton3(Context context, AttributeSet attrs, int defStyle) {
super(context, null, R.attr.attrStyle3);
}
}
You can see from the custom classes I have passed a custom attr. These I define in my styles.xml and use them as reference. See my styles.xml below:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="theme1">
<item name="#attr/attrStyle1">#style/CustomButton1</item>
<item name="#attr/attrStyle2">#style/CustomButton2</item>
<item name="#attr/attrStyle3">#style/CustomButton3</item>
<item name="android:background">#color/warning_yellow_colour</item>
</style>
<style name="CustomButton1" parent = "#android:style/Widget.Button">
<item name="android:textColor">#color/white_colour</item>
<item name="android:padding">20dp</item>
<item name="android:background">#color/banner_background_sensor_colour</item>
</style>
<style name="CustomButton2" parent = "#android:style/Widget.Button">
<item name="android:textColor">#color/white_colour</item>
<item name="android:padding">20dp</item>
<item name="android:background">#color/button_red_colour</item>
</style>
<style name="CustomButton3" parent = "#android:style/Widget.Button">
<item name="android:textColor">#color/white_colour</item>
<item name="android:padding">20dp</item>
<item name="android:background">#color/text_blue_colour</item>
</style>
<attr name="attrStyle1" format="reference"/>
<attr name="attrStyle2" format="reference"/>
<attr name="attrStyle3" format="reference"/>
By linking the style to the attr, you then apply that style to your custom class, which you can then duplicate as many times as needed
CustomButton1 theme1 = (CustomButton1)findViewById(R.id.theme1);
CustomButton2 theme2 = (CustomButton2)findViewById(R.id.theme2);
CustomButton3 theme3 = (CustomButton3)findViewById(R.id.theme3);
Hopefully this is of benefit to someone!
I am trying to implement my own custom progressbar using 9 patch images. I am looking at the Sound ProgressBar example.
http://www.jagsaund.com/blog/2011/11/6/customizing-your-progress-bar-part-one.html
I have two problems that I have been unable to figure out why its happening:
No matter what I do my progressbar is not an instance of
layerdrawable, I am not sure why this is happening so I am unable to
get reference to the layer I want and change it grammatically.
The 9 patch image starts off stretched to the entire width of the container (relativelayout) not sure how to tell it to not
automatically scale so I can control its width grammatically.
Here are the following code snipets:
Drawable progressbar.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background"
android:drawable="#drawable/progressbar_bg" >
</item>
<item android:id="#+id/progress">
<bitmap
android:src="#drawable/progressbar"
android:tileMode="repeat" />
</item>
</layer-list>
Style.xml
<style name="Widget">
</style>
<style name="Widget.ProgressBar">
<item name="android:indeterminateOnly">true</item>
<item name="android:indeterminateBehavior">repeat</item>
<item name="android:indeterminateDuration">3500</item>
<item name="android:minWidth">48dip</item>
<item name="android:maxWidth">48dip</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
<style name="Widget.ProgressBar.RegularProgressBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">#drawable/progressbar</item>
<item name="android:indeterminateDrawable">#android:drawable/progress_indeterminate_horizontal</item>
<item name="android:minHeight">1dip</item>
<item name="android:maxHeight">10dip</item>
</style>
Custom View inside XML layout file:
<com.custom.ahmad.views.NfcProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/timeOutProgress"
android:layout_above="#+id/tvStart"
style="#style/Widget.ProgressBar.RegularProgressBar" />
Finally The custom Progressbar class:
public class NfcProgressBar extends ProgressBar {
private String text = "";
private int textColor = Color.BLACK;
private float textSize = 15;
public NfcProgressBar(Context context) {
super(context);
}
public NfcProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NfcProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected synchronized void onDraw(Canvas canvas) {
Drawable progressDrawable = this.getProgressDrawable();
Log.i("Testing", "progressDrawable : " + ((progressDrawable != null) ? progressDrawable : "null"));
Log.i("Testing", "progressDrawable instanceof LayerDrawable : " + ((progressDrawable instanceof LayerDrawable)? "true" : "false"));
if (progressDrawable != null && progressDrawable instanceof LayerDrawable) {
// Get the layer and do some programming work.
}
}