What is the size of ActionBar in pixels? - android

I need to know the exact size of ActionBar in pixels so to apply correct background image.

To retrieve the height of the ActionBar in XML, just use
?android:attr/actionBarSize
or if you're an ActionBarSherlock or AppCompat user, use this
?attr/actionBarSize
If you need this value at runtime, use this
final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
If you need to understand where this is defined:
The attribute name itself is defined in the platform's /res/values/attrs.xml
The platform's themes.xml picks this attribute and assigns a value to it.
The value assigned in step 2 depends on different device sizes, which are defined in various dimens.xml files in the platform, ie. core/res/res/values-sw600dp/dimens.xml

From the de-compiled sources of Android 3.2's framework-res.apk, res/values/styles.xml contains:
<style name="Theme.Holo">
<!-- ... -->
<item name="actionBarSize">56.0dip</item>
<!-- ... -->
</style>
3.0 and 3.1 seem to be the same (at least from AOSP)...

To get the actual height of the Actionbar, you have to resolve the attribute actionBarSize at runtime.
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);

One of the Honeycomb samples refers to ?android:attr/actionBarSize

I needed to do replicate these heights properly in a pre-ICS compatibility app and dug into the framework core source. Both answers above are sort of correct.
It basically boils down to using qualifiers. The height is defined by the dimension "action_bar_default_height"
It is defined to 48dip for default. But for -land it is 40dip and for sw600dp it is 56dip.

If you're using the compatibility ActionBar from the recent v7 appcompat support package, you can get the height using
#dimen/abc_action_bar_default_height
Documentation

With the new v7 support library (21.0.0) the name in R.dimen has changed to #dimen/abc_action_bar_default_height_material.
When upgrading from a previous version of the support lib you should therefore use that value as the actionbar's height

If you are using ActionBarSherlock, you can get the height with
#dimen/abs__action_bar_default_height

#AZ13's answer is good, but as per the Android design guidelines, the ActionBar should be at least 48dp high.

Accepted answer in Kotlin :
val Context.actionBarSize
get() = theme.obtainStyledAttributes(intArrayOf(android.R.attr.actionBarSize))
.let { attrs -> attrs.getDimension(0, 0F).toInt().also { attrs.recycle() } }
Usage :
val size = actionBarSize // Inside Activity
val size = requireContext().actionBarSize // Inside Fragment
val size = anyView.context.actionBarSize // Inside RecyclerView ViewHolder

public int getActionBarHeight() {
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv,
true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(
tv.data, getResources().getDisplayMetrics());
} else {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
getResources().getDisplayMetrics());
}
return actionBarHeight;
}

The Class Summary is usually a good place to start. I think the getHeight() method should suffice.
EDIT:
If you need the width, it should be the width of the screen (right?) and that can be gathered like this.

On my Galaxy S4 having > 441dpi > 1080 x 1920 >
Getting Actionbar height with getResources().getDimensionPixelSize I got 144 pixels.
Using formula px = dp x (dpi/160), I was using 441dpi, whereas my device lies
in the category 480dpi. so putting that confirms the result.

I did in this way for myself, this helper method should come in handy for someone:
private static final int[] RES_IDS_ACTION_BAR_SIZE = {R.attr.actionBarSize};
/**
* Calculates the Action Bar height in pixels.
*/
public static int calculateActionBarSize(Context context) {
if (context == null) {
return 0;
}
Resources.Theme curTheme = context.getTheme();
if (curTheme == null) {
return 0;
}
TypedArray att = curTheme.obtainStyledAttributes(RES_IDS_ACTION_BAR_SIZE);
if (att == null) {
return 0;
}
float size = att.getDimension(0, 0);
att.recycle();
return (int) size;
}

Related

How to display progress drawable animation in Chip Material Component?

According to the official Design docs for Action Chips, we are supposed to be able to add a progress state to chips. Sadly, the Development docs don't mention this at all. Has anyone managed to figure out how to achieve the effect shown here?
It is possible to implement this behavior using the chipIcon attribute and androidx.swiperefreshlayout.widget.CircularProgressDrawable.
xml
app:chipIconWithProgress="#{viewModel.taskIsStarting ? null : #drawable/ic_play_arrow}"
and BindingAdapter
#BindingAdapter("chipIconWithProgress")
fun Chip.setChipIconWithProgress(item: Drawable?) {
chipIcon = item
?: CircularProgressDrawable(context!!).apply {
setStyle(CircularProgressDrawable.DEFAULT)
start()
}
}
You can use the Material Components Library and the Chip component.
In your layout use:
<com.google.android.material.chip.Chip
android:id="#+id/chip"
style="#style/Widget.MaterialComponents.Chip.Action"
app:iconStartPadding="2dp"
../>
Then use as chipIcon a ProgressIndicator provided by the library:
ProgressIndicatorSpec progressIndicatorSpec = new ProgressIndicatorSpec();
progressIndicatorSpec.loadFromAttributes(
this,
null,
R.style.Widget_MaterialComponents_ProgressIndicator_Circular_Indeterminate);
progressIndicatorSpec.circularInset = 1; // Inset.
progressIndicatorSpec.circularRadius =
(int) dpToPx(this, 8); // Circular radius is 8 dp.
IndeterminateDrawable progressIndicatorDrawable =
new IndeterminateDrawable(
this,
progressIndicatorSpec,
new CircularDrawingDelegate(),
new CircularIndeterminateAnimatorDelegate());
Chip chip = findViewById(R.id.chip);
chip.setChipIcon(progressIndicatorDrawable);
with this util method:
public static float dpToPx(#NonNull Context context, #Dimension(unit = Dimension.DP) int dp) {
Resources r = context.getResources();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}
You can customize the colors and all other attributes:
progressIndicatorSpec.indicatorColors = getResources().getIntArray(R.array.progress_colors);
progressIndicatorSpec.growMode = GROW_MODE_OUTGOING;
Note: this requires at least the version 1.3.0-alpha02.

Reference different raw resource depending on screen orientation

I would like to define raw resources in my dimens.xml file like I define margins, padding for different screen orientations.
I've tried this:
<item name="my_res" type="raw" format="string">R.raw.test</item>
But that doesn't seem to be working.
When I try to fetch id of that resource, it is not correct:
TypedValue out = new TypedValue();
getResources().getValue(R.raw.my_res, out, true);
int resId = out.resourceId;
Any suggestions how to han
Use Activity.getResources().getConfiguration().orientation for obtaining ORIENTATION_PORTRAIT and ORIENTATION_LANDSCAPE values indicating current orientation. Based on this, get appropiate raw resource.
TypedValue out = new TypedValue();
int resId;
if(Activity.getResources().getConfiguration().orientation == 1) { //1 for Portrait and 2 for Landscape
getResources().getValue(R.raw.my_res, out, true);
resId = out.resourceId;
} else {
getResources().getValue(R.raw.my_res_alternate, out, true);
resId = out.resourceId;
}
Do any necesary change, but this is the main idea.

actionBarSize different in xml and code

I have a view with height defined as:
android:layout_height="?attr/actionBarSize"
Later on this view will have to be resized to a larger format and eventually back to the original, so in the onCreateView of the fragment I do:
final TypedArray styledAttributes = getActivity().getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
int actionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle()
Which, as far as I understand, should be the exact same value as i initially got in the XML.
This is however not the case, the value from the XML is 112 while in code it returns 96.
Anyone any clue why this is or what I am doing wrong?
doc for getDimension(int,int) says
Retrieve a dimensional unit attribute at index. Unit
conversions are based on the current DisplayMetrics
associated with the resources this TypedArray object
came from.
I think may be the displaymetrics associated with that does not work.
So try
float pixelvalue = styledAttributes.getDimensionPixelSize(0, 0);
float dpValue =pixelvalue/ getResources().getDisplayMetrics().density;
In case use ActionBarSherlock/AppCompat use:
?attr/actionBarSize
Why use?:
final int actionBarSize
why dont use?
actionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
this last option give you the value at runtime.

Setting textSize programmatically

textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.result_font));
The following code works, but the R.dimen.result_font is taken as a much bigger value than it really is. Its maybe about 18sp-22sp or 24sp according to the screen size ... But the size set here is at least about 50sp. Can someone please recommend something ?
You have to change it to TypedValue.COMPLEX_UNIT_PX because getDimension(id) returns a dimen value from resources and implicitly converted to px.
Java:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.result_font));
Kotlin:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
resources.getDimension(R.dimen.result_font))
Requirement
Suppose we want to set textView Size programmatically from a resource file.
Dimension resource file (res/values/dimens.xml)
<resources>
<dimen name="result_font">16sp</dimen>
</resources>
Solution
First get dimen value from resource file into a variable "textSizeInSp".
int textSizeInSp = (int) getResources().getDimension(R.dimen.result_font);
Next convert 16 sp value into equal pixels.
for that create a method.
public static float convertSpToPixels(float sp, Context context) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
}
Let's set TextSize,
textView.setTextSize(convertSpToPixels(textSizeInSp , getApplicationContext()));
All together,
int textSizeInSp = (int) getResources().getDimension(R.dimen.result_font);
textView.setTextSize(convertSpToPixels(textSizeInSp , getApplicationContext()));

Label Text size according to the screen size in a chart engine pie chart in android

I am successfully displaying pie chart using achart engine.I want to customize my labels text size according to the screen size.Thanks in advance.
This problem comes down to resolution. achartengine seems to have been designed with raw pixels in mind while display quality and pixel density has increased dramatically over the last couple years. The labels from the achartengine sample are tiny on my HTC One!
Android has introduced scale-independent pixels. The key to using them with achartengine is the TypedValue class. Here's what I did:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float val = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, metrics);
I got 18 from "text size medium" in the link above.
You can then use val anywhere you want to use medium sized text. For example:
renderer.setLabelsTextSize(val);
This a summarised version of what I did:
public static final int TEXT_SIZE_XHDPI = 24;
public static final int TEXT_SIZE_HDPI = 20;
public static final int TEXT_SIZE_MDPI = 18;
public static final int TEXT_SIZE_LDPI = 13;
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
switch (getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_XHIGH:
renderer.setMargins(new int[] { 40, 90, 25, 10 });
renderer.setAxisTitleTextSize(Constants.TEXT_SIZE_XHDPI);
renderer.setChartTitleTextSize(Constants.TEXT_SIZE_XHDPI);
renderer.setLabelsTextSize(Constants.TEXT_SIZE_XHDPI);
renderer.setLegendTextSize(Constants.TEXT_SIZE_XHDPI);
break;
case DisplayMetrics.DENSITY_HIGH:
renderer.setMargins(new int[] { 30, 50, 20, 10 });
renderer.setAxisTitleTextSize(Constants.TEXT_SIZE_HDPI);
renderer.setChartTitleTextSize(Constants.TEXT_SIZE_HDPI);
renderer.setLabelsTextSize(Constants.TEXT_SIZE_HDPI);
renderer.setLegendTextSize(Constants.TEXT_SIZE_HDPI);
break;
default:
renderer.setMargins(new int[] { 30, 50, 20, 10 });
renderer.setAxisTitleTextSize(Constants.TEXT_SIZE_LDPI);
renderer.setChartTitleTextSize(Constants.TEXT_SIZE_LDPI);
renderer.setLabelsTextSize(Constants.TEXT_SIZE_LDPI);
renderer.setLegendTextSize(Constants.TEXT_SIZE_LDPI);
break;
}
So I basically customised the margins and text sizes of the chart depending of the device's screen density. Note: I excluded the medium density case for now.
I wrote two methods called _dp and _sp to convert to dp and sp values.
private DisplayMetrics displayMetrics;
private boolean isPortrait;
int _dp(float pixels) {
return (int)(pixels * displayMetrics.density);
}
float _sp(float pixels) {
return (pixels * displayMetrics.scaledDensity);
}
In your onCreate you need to set the displayMetrics value:
displayMetrics = (this.getResources()).getDisplayMetrics();
Then you just use _sp and _dp to get the sp/dp value. For instance to set a font scaled to 18
renderer.setAxisTitleTextSize(_sp(18));
And to set margins with dp values:
renderer.setMargins(new int[] {_dp(25), _dp(30), _dp(35), _dp(20)});
NOTE: I made one change to this code. It is possible to get a 0 value for these functions -- which is a real problem if you are dividing by the return value. I added a test to return 1 if the return value is < 1. ie: return ((rv < 1) ? 1 : v);
You can set the labels text size this way:
renderer.setLabelsTextSize(size);
You will have to find the best logic for finding the best proportion between the screen size and the labels size.
The best practice is to place dummy TextView into layout to obtain the textSize:
<TextView
android:id="#+id/dummyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="TextView" />
And in your code for example:
TextView testTextView = (TextView) rootView.findViewById(R.id.dummyTextView);
float textSize = testTextView.getTextSize();
renderer.setLabelsTextSize(textSize);
renderer.setLegendTextSize(textSize);
I think the best way to do it is to define sizes in the file dimens.xml in dp and sp like so:
<dimen name="graph_chart_text_size">16sp</dimen>
<dimen name="graph_axis_title_text_size">14sp</dimen>
<dimen name="graph_labels_text_size">12sp</dimen>
<dimen name="graph_labels_padding">5dp</dimen>
<dimen name="graph_legend_text_size">14sp</dimen>
<dimen name="graph_legend_height">40dp</dimen>
<dimen name="graph_point">3dp</dimen>
<dimen name="graph_line">3dp</dimen>
<dimen name="graph_margin_top">10dp</dimen>
<dimen name="graph_margin_left">40dp</dimen>
<dimen name="graph_margin_right">10dp</dimen>
<dimen name="graph_margin_bottom">20dp</dimen>
And then use getDimension and getDimensionPixelOffset to convert to pixels when configuring the renderer, such as:
renderer.setChartTitleTextSize(context.getResources().getDimension(R.dimen.graph_chart_text_size));
renderer.setAxisTitleTextSize(context.getResources().getDimension(R.dimen.graph_axis_title_text_size));
renderer.setLabelsTextSize(context.getResources().getDimension(R.dimen.graph_labels_text_size));
renderer.setLegendTextSize(context.getResources().getDimension(R.dimen.graph_legend_text_size));
renderer.setLegendHeight(context.getResources().getDimensionPixelOffset(R.dimen.graph_legend_height));
renderer.setMargins(new int[]{
context.getResources().getDimensionPixelOffset(R.dimen.graph_margin_top),
context.getResources().getDimensionPixelOffset(R.dimen.graph_margin_left_mood),
context.getResources().getDimensionPixelOffset(R.dimen.graph_margin_bottom),
context.getResources().getDimensionPixelOffset(R.dimen.graph_margin_right)});
}
I testing in phones that range from 800x480 to 1920x1080 and the design is very consistent.
You can try this:
renderer.setLegendTextSize(textSize);

Categories

Resources