Toolbar height from top for all android verison - android

i want to show one ui below toolbar but problem is say my toolbar size is 50dp and if i say my layout margin from top 50 then for some android version its working . i think above api 22 we also need status_bar_height which includes in toollbar height .
what is the best way to get toolbar height so that i can show view above that toolbar . i am showing one overlay screen with transparent
int statusBar = getStatusBarHeight();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(0, (statusBar + 50), 0, 0);
MainRel.setLayoutParams(layoutParams);
MainRel.setVisibility(View.VISIBLE);
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}

Use this generic code. Override the OnWindowFocusChanged
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int toolbarHeight = toolbar.getHeight();
int statusBar = getStatusBarHeight();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(0, (statusBar + toolbarHeight), 0, 0);//you can use the toolbar height you got here.
MainRel.setLayoutParams(layoutParams);
MainRel.setVisibility(View.VISIBLE);
}

Related

Cutout of application by notch

My application is cut off from the top side by the notch
Is there any specific solution to overcome this problem.
I am using the emulator Pixel 3 XL API 27
image for illustrstion
What should I do any code is there to increase the height of the top bar or status bar or should I increase the height of the title bar, but the title bar is still cut off
Any suggestion or code explanation
Try below solution
It is get size of notch as per device and set margin top to view So,it's fit in all device.
#Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
this.getWindow().setStatusBarColor(Color.TRANSPARENT);
super.onCreate(savedInstanceState);
setContentView(MainActivity.this, R.layout.activity_main);
/*------------ Check display cutout size and give top margin to toolbar -------------*/
setMarginTopAccordingDisplayCutout(context, yourTopMainView, convertDpToPixel(34), 0);
/*-----------------------------------------------------------------------------------*/
...
//your Code
}
private void setMarginTopAccordingDisplayCutout(Context context, View view, int extraTopWithoutCutout, int extraTopWithCutout) {
int statusBarHeight = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
}
if (statusBarHeight > convertDpToPixel(24)) {
final ViewGroup.MarginLayoutParams[] layoutParams = new ViewGroup.MarginLayoutParams[1];
layoutParams[0] = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
layoutParams[0].topMargin = statusBarHeight + extraTopWithCutout;
view.setLayoutParams(layoutParams[0]);
//topbarlp.setMargins(0, statusBarHeight, 0, 0);
//Set above layout params to your layout which was getting cut because of notch
///topbar.setLayoutParams(topbarlp)
Log.e(TAG, "onCreate statusBarHeight :: " + statusBarHeight);
} else {
final ViewGroup.MarginLayoutParams[] layoutParams = new ViewGroup.MarginLayoutParams[1];
layoutParams[0] = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
layoutParams[0].topMargin = extraTopWithoutCutout;
view.setLayoutParams(layoutParams[0]);
}
}
private int convertDpToPixel(float dp) {
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return Math.round(px);
}
I hope this can help you!
Thank You.

Set Top Padding affects Bottom Margin of a View

This is bottom margin:
#Override
protected void directionDownScrolling(View recyclerView) {
MarginLayoutParams params = (MarginLayoutParams) recyclerView.getLayoutParams();
params.setMargins(0, 0, 0,
(int) recyclerView.getContext().getResources().getDimension(R.dimen.dimen_recycler_view_spacing));
mHandler.postDelayed(() -> recyclerView.setLayoutParams(params), 250);
}
And Top Padding :
#Override
protected void directionDownScrolling(View recyclerView) {
// Calculate ActionBar height
TypedValue tv = new TypedValue();
int actionBarHeight = recyclerView.getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true) ?
TypedValue.complexToDimensionPixelSize(tv.data, recyclerView.getContext().getResources().getDisplayMetrics()) :
(int) recyclerView.getContext().getResources().getDimension(R.dimen.dimen_recycler_view_spacing);
recyclerView.setPadding(0, actionBarHeight, 0, 0);
}
As you see top padding applies without no delay, but I expect bottom margin appears after 250ms,
but as soon as top padding applies, bottom margin appears as well. Why and how to fix it?
You're getting the layout parameters from recyclerView:
MarginLayoutParams params = (MarginLayoutParams) recyclerView.getLayoutParams();
Then you setup the margins directly on it:
params.setMargins(0, 0, 0,
(int) recyclerView.getContext().getResources().getDimension(R.dimen.dimen_recycler_view_spacing));
So the delayed message doesn't do anything:
mHandler.postDelayed(() -> recyclerView.setLayoutParams(params), 250);
This would make a difference if you'd set the layout params to some other instance. Instead call setMargins delayed:
#Override
protected void directionDownScrolling(View recyclerView) {
MarginLayoutParams params = (MarginLayoutParams) recyclerView.getLayoutParams();
int marginBottom = (int) recyclerView.getContext().getResources().getDimension(R.dimen.dimen_recycler_view_spacing));
mHandler.postDelayed(() -> {
params.setMargins(0, 0, 0, marginBottom);
recyclerView.requestLayout();
}, 250);
}

How to get heights of a transparent softkeys bar and transparent status bar?

I have an activity containing a RecyclerView and both transparent status bar (the one containing wifi signal, etc...) and softkeys bar. Of course the number of items in the RecyclerView is undefined and i need the first one to have the standard top margin plus the status bar height, and the last one the standard bottom margin plus the height of the softkeys bar. This is needed because otherwise i'll get the first and the last items partially covered by the bars when the scroll is at the top or at the bottom respectively.
Here a sample code of how i'm handling the margins programmatically:
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
topSpace = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
bottomSpace = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
standardSpace = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
topSpace.setMargins( (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8 + 24, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics));
bottomSpace.setMargins( (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8 + 56, metrics) +
getSoftkeysHeight(activity));
standardSpace.setMargins((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics));
I'm trying to use those answers but they seems not helping me because the drawable surface of the screen is indeed the whole screen...
You can use RecyclerView.ItemDecoration to modify the margins of the specific items.
Create a class that extends RecyclerView.ItemDecoration :
public static class SimpleItemDecorator extends RecyclerView.ItemDecoration {
private int regularMargin;
private int lastPosition;
private int statusBarHeight;
private int navigationBarHeight;
public SimpleItemDecorator(Activity activity, int recyclerViewItemsCount) {
// get the regular margin
regularMargin = activity.getResources().getDimensionPixelSize(R.dimen.regularMargin);
// determine the last position
lastPosition = recyclerViewItemsCount - 1;
// get the height of the status bar
final Rect rectangle = new Rect();
final Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
statusBarHeight = rectangle.top;
// get the height of the navigation bar
final int redId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
navigationBarHeight = (redId > 0) ? resources.getDimensionPixelSize(resId) : 0;
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
// modify the margins
final int position = parent.getChildAdapterPosition(view);
if (position == 0) {
outRect.top = statusBarHeight + regularMargin;
} else if (position == lastPosition) {
outRect.bottom = navigationBar + regularMargin;
}
}
}
Add the created ItemDecorator to the RecyclerView :
final int recyclerViewItemsCount = 6;
final SimpleItemDecorator itemDecorator = new SimpleItemDecorator(this, recyclerViewItemsCount);
recyclerView.addItemDecorator(itemDecorator);

Changing WRAP_CONTENT to a set number using LinearLayout.LayoutParams

I have set up some LinearLayout.LayoutParams to set up some buttons just using java. But I want to change it from WRAP_CONTENT to a height and width of my choice.
//LinearLayout.LayoutParams top;
//this is declared at the top as it is used by different bits of the code.
top = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
top.topMargin = 100;
top.leftMargin = 120;
top = new LinearLayout.LayoutParams
(90, 80);
// whatever u like
this is from documentation:
public LayoutParams(int width, int height) {
super(width, height);
weight = 0;
}
According to ViewGroup.MarginLayoutParams.html#setMargins you have to use this:
LinearLayout layout = (LinearLayout)findViewById(R.id.yourLinear_Layout);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(120, 100, 0, 0); //Here your custom margin (int left, int top, int right, int bottom)
layout.setLayoutParams(params);
Have you tried?
top.getLayoutParams().height = 256;
top.getLayoutParams().width= 256;
top.requestLayout();
try like this..
LinearLayout layout = (LinearLayout)findViewById(R.id.numberPadLayout);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = 100;
params.width = 100;
layout.setLayoutParams(params);

How to set margin of ImageView using code, not xml

I want to add an unknown number of ImageView views to my layout with margin. In XML, I can use layout_margin like this:
<ImageView android:layout_margin="5dip" android:src="#drawable/image" />
There is ImageView.setPadding(), but no ImageView.setMargin(). I think it's along the lines of ImageView.setLayoutParams(LayoutParams), but not sure what to feed into that.
Does anyone know?
android.view.ViewGroup.MarginLayoutParams has a method setMargins(left, top, right, bottom). Direct subclasses are: FrameLayout.LayoutParams, LinearLayout.LayoutParams and RelativeLayout.LayoutParams.
Using e.g. LinearLayout:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
MarginLayoutParams
This sets the margins in pixels. To scale it use
context.getResources().getDisplayMetrics().density
DisplayMetrics
image = (ImageView) findViewById(R.id.imageID);
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
marginParams.setMargins(left_margin, top_margin, right_margin, bottom_margin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
image.setLayoutParams(layoutParams);
All the above examples will actually REPLACE any params already present for the View, which may not be desired. The below code will just extend the existing params, without replacing them:
ImageView myImage = (ImageView) findViewById(R.id.image_view);
MarginLayoutParams marginParams = (MarginLayoutParams) image.getLayoutParams();
marginParams.setMargins(left, top, right, bottom);
Kevin's code creates redundant MarginLayoutParams object. Simpler version:
ImageView image = (ImageView) findViewById(R.id.main_image);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(image.getLayoutParams());
lp.setMargins(50, 100, 0, 0);
image.setLayoutParams(lp);
If you want to change imageview margin but leave all other margins intact.
Get MarginLayoutParameters of your image view in this case: myImageView
MarginLayoutParams marginParams = (MarginLayoutParams) myImageView.getLayoutParams();
Now just change the margin you want to change but leave the others as they are:
marginParams.setMargins(marginParams.leftMargin,
marginParams.topMargin,
150, //notice only changing right margin
marginParams.bottomMargin);
I use simply this and works great:
ImageView imageView = (ImageView) findViewById(R.id.image_id);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
layoutParams.setMargins(left, top, right, bottom);
imageView.setLayoutParams(layoutParams);
setMargins()'s unit is pixel not dp. If you want to set margin in dp, just inside your values/dimens.xml file create your dimensions like:
<resources>
<dimen name="right">16dp</dimen>
<dimen name="left">16dp</dimen>
</resources>
and access like:
getResources().getDimension(R.dimen.right);
If you use kotlin, this can be simplified by creating an extension function
fun View.setMarginExtensionFunction(left: Int, top: Int, right: Int, bottom: Int) {
val params = layoutParams as ViewGroup.MarginLayoutParams
params.setMargins(left, top, right, bottom)
layoutParams = params
}
Now all you need is a view, and this extension function can be used anywhere.
val imageView = findViewById(R.id.imageView)
imageView.setMarginExtensionFunction(0, 0, 0, 0)
You can use this method, in case you want to specify margins in dp:
private void addMarginsInDp(View view, int leftInDp, int topInDp, int rightInDp, int bottomInDp) {
DisplayMetrics dm = view.getResources().getDisplayMetrics();
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(convertDpToPx(leftInDp, dm), convertDpToPx(topInDp, dm), convertDpToPx(rightInDp, dm), convertDpToPx(bottomInDp, dm));
view.setLayoutParams(lp);
}
private int convertDpToPx(int dp, DisplayMetrics displayMetrics) {
float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics);
return Math.round(pixels);
}
Answer from 2020 year :
dependencies {
implementation "androidx.core:core-ktx:1.2.0"
}
and cal it simply in your code
view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
setMargins(5)
}
create layout dynamically and set its parameter as setmargin() will not work directly on an imageView
ImageView im;
im = (ImageView) findViewById(R.id.your_image_in_XML_by_id);
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(im.getLayoutParams());
layout.setMargins(counter*27, 0, 0, 0);//left,right,top,bottom
im.setLayoutParams(layout);
im.setImageResource(R.drawable.yourimage)
For me this worked:
int imgCarMarginRightPx = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, definedValueInDp, res.getDisplayMetrics());
MarginLayoutParams lp = (MarginLayoutParams) imgCar.getLayoutParams();
lp.setMargins(0,0,imgCarMarginRightPx,0);
imgCar.setLayoutParams(lp);
sample code is here ,its very easy
LayoutParams params1 = (LayoutParams)twoLetter.getLayoutParams();//twoletter-imageview
params1.height = 70;
params1.setMargins(0, 210, 0, 0);//top margin -210 here
twoLetter.setLayoutParams(params1);//setting layout params
twoLetter.setImageResource(R.drawable.oo);
In Kotlin you can write it in more pleasant way
myView.layoutParams = LinearLayout.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT
).apply {
setMargins(12, 12, 12, 12)
}
Using a method similar to this might save you some headaches in some situations.
If you have two passes of programmatical tinkering with margins it is safer to check if there are already some layoutParams set. If there are some margins already one should increase them and not replace them:
public void addMargins(View v, int left, int top, int right, int bottom) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
if (params == null)
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
int oldLeft = params.leftMargin;
int oldTop = params.topMargin;
int oldRight = params.rightMargin;
int oldBottom = params.bottomMargin;
params.setMargins(oldLeft + left, oldTop + top, oldRight + right, oldBottom + bottom);
v.setLayoutParams(params);
}
Here is an example to add 8px Margin on left, top, right, bottom.
ImageView imageView = new ImageView(getApplicationContext());
ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(
ViewGroup.MarginLayoutParams.MATCH_PARENT,
ViewGroup.MarginLayoutParams.WRAP_CONTENT
);
marginLayoutParams.setMargins(8, 8, 8, 8);
imageView.setLayoutParams(marginLayoutParams);
We can create Linear LayoutParams & use resources.getDimensionPixelSize for dp value.
val mContext = parent.context
val mImageView = AppCompatImageView(mContext)
mImageView.setBackgroundResource(R.drawable.payment_method_selector)
val height = mContext.resources.getDimensionPixelSize(R.dimen.payment_logo_height)
val width = mContext.resources.getDimensionPixelSize(R.dimen.payment_logo_width)
val padding = mContext.resources.getDimensionPixelSize(R.dimen.spacing_small_tiny)
val margin = mContext.resources.getDimensionPixelSize(R.dimen.spacing_small)
mImageView.layoutParams = LinearLayout.LayoutParams(width, height).apply {
setMargins(margin, margin, 0, 0)
}
mImageView.setPadding(padding, padding, padding, padding)

Categories

Resources