Android actionbarsherlock get height return 0 - android

I was looking for get the height of my actionbar(sherlock). The value returned is 0.
value/styles.xml (and for value11 I used "android:actionBarSize")
<style name="AppTheme" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/Widget.AppTheme.ActionBar</item>
<item name="actionBarSize">48dip</item>
</style>
code :
getSupportActionBar().getHeight()

This didn't work?
// Calculate ActionBar height
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
ref: How to get the ActionBar height?

Related

how to dynamically change color using the defined attribute

Having two theme, it can be dynamically switched.
There is a txtColor attribute defined in
attrs.xml
<attr name=“txtColor” format="reference" />
in themes.xml, defined the color for the attribute in different theme
<style name=“CustomLight" parent="AppTheme.Base">
<item name="txtColor”>#000000</item>
<style name=“CustomDark" parent="AppTheme.Base">
<item name="txtColor”>#ffffff</item>
in layout file, using the attribute is fine
android:textColor="?attr/txtColor"
but got exception when try to use the txtColor attribute
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f010015
txtView.setTextColor(getResources().getColor(R.attr.txtColor));
question: how to change the color dynamically using the attribute?
First the attribute format should be "color"
<attr name="txtColor" format="color"/>
Then you can set the color doing this:
int[] attrs = {R.attr.txtColor} ;
try { //getPackageManager() can throw an exeption
Activity activity = getActivity();
themeId = activity.getPackageManager().getActivityInfo(activity.getComponentName(), 0).theme;
TypedArray ta = activity.obtainStyledAttributes(themeId, attrs);
int color = ta.getColor(0, Color.BLACK); //I set Black as the default color
txtView.setTextColor(color);
ta.recycle();
} catch (NameNotFoundException e) {
e.printStackTrace();
}
I think I found a simpler solution which worked with the existing attrs, here it in case someone is looking for the same, any simpler ones? Thanks!
public static int getColorByThemeAttr(Context context, int attr, int defaultColor) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
boolean got = theme.resolveAttribute(attr, typedValue, true);
return got ? typedValue.data : defaultColor;
}

Get specific drawable from state list drawable

I have a state list drawable, and i want to get a specific drawable from the state list drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:kplus="http://schemas.android.com/apk/res-auto">
<item kplus:key_type_space_alt="true" android:state_pressed="true" android:drawable="#drawable/space_1_pressed" />
<item kplus:key_type_space_alt="true" android:drawable="#drawable/space_1_normal" />
<!-- TopNav keys. -->
<item kplus:key_type_topnav="true" android:state_pressed="true" android:drawable="#drawable/tab_down" />
<item kplus:key_type_topnav="true" android:state_selected="true" android:drawable="#drawable/tab_down" />
<item kplus:key_type_topnav="true" android:drawable="#drawable/tab_normal" />
<!-- TopRow keys. -->
<item kplus:key_type_toprow="true" android:state_pressed="true" android:drawable="#drawable/numeric_presseed" />
<item kplus:key_type_toprow="true" android:drawable="#drawable/numeric_normal" />
</selector>
I select the correct drawable state for each key, something like this:
if (keyIsNumbers) {
if (KPlusInputMethodService.sNumbersState == 2) {
drawableState = mDrawableStatesProvider.KEY_STATE_TOPNAV_CHECKED;
}
}
Now the states are defined like this:
KEY_STATE_TOPNAV_NORMAL = new int[] {keyTypeTopNavAttrId};
KEY_STATE_TOPNAV_PRESSED = new int[] {keyTypeTopNavAttrId, android.R.attr.state_pressed};
KEY_STATE_TOPNAV_CHECKED = new int[] {keyTypeTopNavAttrId, android.R.attr.state_selected};
Now my question is how to extract the correct drawable for each state ? I need to get the 9patch padding of the drawable, because if the state have different padding on 9patch it will get the padding only for the top drawable, and i want to set the padding manually for each key (drawable.getPadding(rect)).
There is no public API to get the drawable from the state.
There are some methods in StateListDrawable but they are #hide with the comment "pending API council".
You can invoke them by reflection... but it's at your own risk !!!. (it may change in future releases)
Those methods are :
getStateDrawableIndex
getStateDrawable
Here is how to proceed (exceptions omitted) :
int[] currentState = view.getDrawableState();
StateListDrawable stateListDrawable = (StateListDrawable)view.getBackground();
Method getStateDrawableIndex = StateListDrawable.class.getMethod("getStateDrawableIndex", int[].class);
Method getStateDrawable = StateListDrawable.class.getMethod("getStateDrawable", int.class);
int index = (int) getStateDrawableIndex.invoke(stateListDrawable,currentState);
Drawable drawable = (Drawable) getStateDrawable.invoke(stateListDrawable,index);
now it is the way to get specific drawable without reflection:
StateListDrawable pressedDrawable = (StateListDrawable) this.mPressed;
final int state = android.R.attr.state_pressed;
final int index = pressedDrawable.findStateDrawableIndex(new int[]{state});
if (index >= 0) {
Drawable drawable = pressedDrawable.getStateDrawable(index);
if (drawable instanceof GradientDrawable) {
((GradientDrawable) drawable).setCornerRadius(mRoundConerRadius);
}
}

Changing text color in action bar? [duplicate]

This question already has answers here:
ActionBar text color
(28 answers)
Closed 9 years ago.
I can't seem to figure out how to change the color of the actionbar to white instead of the default black.
<resources>
<style name="ActionBarOrange" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#color/orange</item>
</style>
</resources>
Any help?
In this article you will find how to customize ActionBar and more things
http://developer.android.com/guide/topics/ui/actionbar.html#AdvancedStyles
Just use HTML:
String htmlColor;
Color orange = getResources().getColor(R.color.orange);
int alpha = Color.alpha(orange);
if (alpha == 0)
htmlColor = String.format(Locale.US, "#%06X", (0xFFFFFF & orange));
else {
int red = Color.red(orange);
int green = Color.green(orange);
int blue = Color.blue(orange);
if (Build.VERSION >= 18) //KITKAT first provided CSS3 support
htmlColor = String.format(Locale.US, "rgba(%d,%d,%d,%d)", red, green, blue, alpha);
else
htmlColor = String.format(Locale.US, "#%06X", (0xFFFFFF & Color.argb(0, red, green, blue)));
}
getActionBar()/* or getSupportActionBar() */.setTitle(Html.fromHtml("<font color=\"" + hexColor + "\">" + getString(R.string.app_name) + "</font>"));

Obtaining themed attributes from built in Android styles

Given a Context that has been themed with AppTheme (shown below), is it possible to programmatically obtain the color #ff11cc00 without referencing R.style.MyButtonStyle, R.style.AppTheme, or android.R.style.Theme_Light?
The objective is to obtain the button text color that was set by the theme without being bound to a particular theme or app-declared resource. Using android.R.style.Widget_Button and android.R.attr.textColor is okay.
<style name="AppTheme" parent="Theme.Light">
<item name="android:buttonStyle">#style/MyButtonStyle</item>
</style>
<style name="MyButtonStyle" parent="android:style/Widget.Button">
<item name="android:textColor">#ff11cc00</item>
</style>
Try the following:
TypedValue outValue = new TypedValue();
Theme theme = context.getTheme();
theme.resolveAttribute(android.R.attr.buttonStyle, outValue , true);
int[] attributes = new int[1];
attributes[0] = android.R.attr.textColor;
TypedArray styledAttributes = theme.obtainStyledAttributes(outValue.resourceId, attributes);
int color = styledAttributes.getColor(0, 0);
I can think of this round about way, may be someone else will know better:
int theme ;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD) {
theme = android.R.style.Theme;
} else {
theme = android.R.style.Theme_DeviceDefault;
}
ContextThemeWrapper wrapper = new ContextThemeWrapper(context, theme);
Button button = new Button(wrapper);
ColorStateList colorStateList = button.getTextColors();
colorStateList.getColorForState(button.getDrawableState(), R.color.default_color);

Two different styles in a single textview with different gravity and height

I am looking for a textview which looks like . I have used below code for the implementation of this..
SpannableString text;
text = new SpannableString(bal_dollars.getText());
int index = bal_dollars.getText().toString().indexOf(".");
text.setSpan(new TextAppearanceSpan(getApplicationContext(),
R.style.HeroGraphicBalanceSmallFont), index, bal_dollars
.getText().toString().length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
bal_dollars.setText(text, TextView.BufferType.SPANNABLE);
here is the style used in the span..
<style name="HeroGraphicBalanceSmallFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">wrap_content</item>
<item name="android:textSize">50sp</item>
<item name="android:singleLine">true</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_gravity">top|right</item>
<item name="android:paddingBottom">30dp</item>
<item name="android:gravity">top|right</item>
<item name="android:textColor">#color/status_text</item>
<item name="customFont">fonts/HeroicCondensedMedium.ttf</item>
</style>
everything looks fine except the spacing of targeted span of text i.e decimal part gravity.. . I tried all the xml attributes. Please help
I cracked it. I am posting this as it might be helpful for others and thanks to #kcoppock for the hint.
I have extended MetricAffectingSpan class which affects character-level text formatting in a way that changes the width or height of characters extend this class.
public class CustomCharacterSpan extends MetricAffectingSpan {
double ratio = 0.5;
public CustomCharacterSpan() {
}
public CustomCharacterSpan(double ratio) {
this.ratio = ratio;
}
#Override
public void updateDrawState(TextPaint paint) {
paint.baselineShift += (int) (paint.ascent() * ratio);
}
#Override
public void updateMeasureState(TextPaint paint) {
paint.baselineShift += (int) (paint.ascent() * ratio);
}}
use this to update the text in your views..
String string = tv.getText().toString();
SpannableString text = new SpannableString(tv.getText());
int index = tv.getText().toString().indexOf(".");
text.setSpan(new CustomCharacterSpan(), index, string.length(),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);//u can use ur own ratio using another constructor
tv.setText(text, TextView.BufferType.SPANNABLE);

Categories

Resources