Set AppBarLayout size Programmatically on Android - java

My AppBarLayout hold a Toolbar and TabLayout. When view is anchored, height is set to 0 setLayoutParams(0). On another state, the view is collapsed, I need to restore appbar height. I tried setLayoutParams(heightDp) where
float heightDp = getResources().getDisplayMetrics().heightPixels / 6;
but the height is not accurate.
private void setLayoutParams(int paramsHeight) {
CoordinatorLayout.LayoutParams lsp = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
lsp.height = paramsHeight;
mAppBarLayout.setLayoutParams(lsp);
}
How can I determine exact AppBar height programatically.

You should get it from default resources with TypedArray:
int mActionBarSize;
TypedArray attrs= getContext().getTheme().obtainStyledAttributes(new int[] { android.R.attr.actionBarSize });
mActionBarSize = (int) attrs.getDimension(0, 0);
You must recycle TypedArray
attrs.recycle();
This way you will get the value according to your screen.

Related

How to set margin for ImageView in pixels in android java?

I have a set of ImageViews whose BackgroundResource and LayoutParams will be defined in a separate xml file. The height, width, marginleft and margintop are given in pixels. The values are in respect to a 1024x600 pixel resolution screen. The code i'm using
ImageView ImgV= (ImageView)findViewById(R.id.ImgV_1);
RelativeLayout.LayoutParams the_dimens = new RelativeLayout.LayoutParams(50,50);
the_dimens.setMargins(800,50,0,0);
ImgV.setBackgroundResource(R.drawable.bulbon);
ImgV.setLayoutParams(the_dimens);
While the height and width seem to be rightly displayed in pixels, the margins are not. So how can I set the margin values to apply in pixels rather than whatever default units the below code takes.
the_dimens.setMargins(800,50,0,0);
The app is only for a 1024x600 device, so i'm not worried about resolution independent design.
Thanks.
check this
int dpValue = 5; // margin in dips
float d = context.getResources().getDisplayMetrics().density;
int margin = (int)(dpValue * d); // margin in pixels
You can do in this way:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);

Change AppBarLayout height programmatically in Android

I'm trying to implement Flexible Space with image pattern, using this tutorial.
Everything works fine.
Notice the height definition of the AppBarLayout which is 192dp.
I'd like to make the height 1/3 of the screen instead, to match this google example for the pattern here.
Here's the code in the activity's onCreate (the layout xml is exactly the same as in the tutorial):
AppBarLayout appbar = (AppBarLayout)findViewById(R.id.appbar);
float density = getResources().getDisplayMetrics().density;
float heightDp = getResources().getDisplayMetrics().heightPixels / density;
appbar.setLayoutParams(new CoordinatorLayout.LayoutParams(LayoutParams.MATCH_PARENT, Math.round(heightDp / 3)));
But for some reason, the result is not what I'm expecting. I can't see the app bar at all with this code. (without the code, the height shows as expected but it's from XML and can't be set dynamically).
Do this instead:
AppBarLayout appbar = (AppBarLayout) findViewById(R.id.appbar);
float heightDp = getResources().getDisplayMetrics().heightPixels / 3;
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams();
lp.height = (int)heightDp;
In your original code I think that you calculation for 1/3 of the screen was wrong, but you still should have seen something. It could be that the LayoutParams.MATCH_PARENT in the setLP() wasn't imported correctly. Always declare the view type first, i.e. CoordinatorLayout.LayoutParams just to make sure. Otherwise it can be easy to use a Framelayout.LayoutParams, for instance.
Some methods for changing the AppBarLayout height programatically with dividing, percent or by weight of the screen height:
private AppBarLayout appbar;
/**
* #return AppBarLayout
*/
#Nullable
protected AppBarLayout getAppBar() {
if (appbar == null) appbar = (AppBarLayout) findViewById(R.id.appbar);
return appbar;
}
/**
* #param divide Set AppBar height to screen height divided by 2->5
*/
protected void setAppBarLayoutHeightOfScreenDivide(#IntRange(from = 2, to = 5) int divide) {
setAppBarLayoutHeightOfScreenPercent(100 / divide);
}
/**
* #param percent Set AppBar height to 20->50% of screen height
*/
protected void setAppBarLayoutHeightOfScreenPercent(#IntRange(from = 20, to = 50) int percent) {
setAppBarLayoutHeightOfScreenWeight(percent / 100F);
}
/**
* #param weight Set AppBar height to 0.2->0.5 weight of screen height
*/
protected void setAppBarLayoutHeightOfScreenWeight(#FloatRange(from = 0.2F, to = 0.5F) float weight) {
if (getAppBar() != null) {
ViewGroup.LayoutParams params = getAppBar().getLayoutParams();
params.height = Math.round(getResources().getDisplayMetrics().heightPixels * weight);
getAppBar().setLayoutParams(params);
}
}
If you want to follow the material design guidelines the height should be equal to the default height plus content increment(s)
https://www.google.com/design/spec/layout/structure.html#structure-app-bar

How get height of the a view with gone visibility and height defined as wrap_content in xml?

I have a ListView with visibility=gone and height=wrap_content, and i need its height to can make an animation of expand.
I already tried it:
view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int height = view.getMeasuredHeight();
and
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int height = view.getMeasuredHeight();
and
v.measure(View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED));
int height = view.getMeasuredHeight();
But it returned me a lesser value.
I am trying to do this after onResume.
The only way that i found was setting the width of the my view from width of a visible header view, then the below code returned me the right value.
int widthSpec = MeasureSpec.makeMeasureSpec(headerView.getWidth(), MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
v.measure(widthSpec, heightSpec);
int height = v.getMeasuredHeight();
view.post(new Runnable() {
#Override
public void run() {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int height = view.getMeasuredHeight();
}
});
This worked for me. The method "post" makes sure the view is already added to the screen.
There is only one efficient way to do that if you want to animate height of a view whose visibility is gone
First add this in xml of hidden view
android:animateLayoutChanges="true"
then thats how you can set it to animate
yourView.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
Android will animate any layout change on that specific view
When an object is gone, it no longer is part of the layout. Maybe you mean to set your object to invisible, in which case you'll probably get a sensical value
This works for me
view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int height = view.getMeasuredHeight();

How to get height in pixels of LinearLayout with layout_weight

this is my first post. I am learning to code in java for android apps.
I am using a LinearLayout "variable named: ll" with a predefined layout_weight (which I want to change whenever I want in the future) and height and width set to MATCH_PARENT. I want to get the height of this LinearLayout in pixels. I fill this using java (ll.addView(variableForAnObject);).
I am trying to get its height using ll.getLayoutParams().height; but I am getting -1. Because I believe this is the constant for MATCH_PARENT. Setting the height to 0dp makes the ll invisible and gives height as 0.
Please tell me if there's a way to get its height. I think I can do this by getting screen height using DisplayMetrics and making nearly impossible calculations (for me, at least) but this will be too complex.
try with ll.getHeight() should work
if not, it's because android has not yet calculed its bounds, then:
//inside of onCreate method should work
ll.post(new Runnable() {
#Override
public void run() {
//maybe also works height = ll.getLayoutParams().height;
height = ll.getHeight();
}
});
It is -1 because its dimensions have not yet been set, whenever you set layout it takes some time to compute the actual values including height, width etc.
Use OnLayoutListener to listen when it has finished with the layout.
layout.onLayoutChange (View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//Extract values here
height = view.getHeight();
width = view.getWidth()
}
See this for more about the OnLayoutListener
Define your LinearLayout in your class and use getHeight() and getWidth(). Example:
LinearLayout yourLayout = (LinearLayout) findViewById (R.id.yourLL);
double height = yourLayout.getHeight();
double width = yourLayout.getWidth();
Hope it helps! :)
Give the id field in your layout xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/<ID>>
Use this id to get reference in java class and find height and width:
LinearLayout linearLayout= (LinearLayout) findViewById (R.id.<ID>);
double height = linearLayout.getHeight();
double width = linearLayout.getWidth();

Textview Resize text When a screen is bigger than other

I Would like to resize a textview, because some text are to small in tablets but in cellphone are normal.
Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading);
TextView txt = (TextView) findViewById(R.id.text);
txt.setTextAppearance(this, android.R.attr.textAppearanceLarge);
You can get the screen size
If you want the the display dimensions in pixels you can use getSize:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
For the use case you're describing however a margin/padding in the layout seems more appropriate.
Now accordingly you can adjust your textView, by adding weight or something
Typically you would do this all with XML. Then you can create a layout for larger screens. You could for example, create a layout folder layout-sw600dp which would be used for screens with a smallest dimension of at least 600dp.
Supporting Multiple Screens

Categories

Resources