i'm using the following code to position ImageView
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
bg = (ImageView) rootView.findViewById(R.id.rectimage);
bg.getLayoutParams().width = width;
bg.getLayoutParams().height = 500;
MarginLayoutParams params = (MarginLayoutParams)bg.getLayoutParams();
params.setMargins(0, height - 510, 0, 0);
i want to place ImageView always at the bottom of screen but screenHeight - image_height is not working Any ideas?
Try to add
android:layout_alignParentBottom="true"
to your ImageView in your layout xml file
Related
I have an app in which I have a recycled view with custom layout what I want I want to show item according to device height. If the device is large then I have to show items with 1 grid or device height is medium then want to show 2 grid.
code:-
DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
Code of recyclerview
rightPaneAdapter = new SidePanelListAdapter(UserListingMainActivity.this, models, this);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 1);
mOptionList.setLayoutManager(mLayoutManager);
mOptionList.setItemAnimator(new DefaultItemAnimator());
mOptionList.setAdapter(rightPaneAdapter);
Use
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int devicewidth = displaymetrics.widthPixels / 3;
int deviceheight = displaymetrics.heightPixels / 4;
holder.image_view.getLayoutParams().width = devicewidth;
//if you need same height as width you can set devicewidth in holder.image_view.getLayoutParams().height
holder.image_view.getLayoutParams().height = deviceheight;
been struggling with a problem for a while. Basically, I've been wrapping my head around trying to get the most accurate width and height of a bitmap inside of an imageview.
How it is, I have a textview and a imageview in a relativeLayout. The imageview gets a bitmap from the gallery of the phone, then centers it in the imageview and keeps the image ratio when it scales the image up to fit the screen accordingly. My problem is that my textview is supposed to get a random position where that position is on-top of the bitmap. I've been trying to get the correct width and height of the bitmap for some math, but I can't quite get the correct width and height. My text always seem to get outside of the bitmap. Is there someone that could help me with this? It's been days..
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
g.getLayoutParams();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
int dpHeight = (int) (displayMetrics.heightPixels / displayMetrics.density);
int dpWidth = (int) (displayMetrics.widthPixels / displayMetrics.density);
int widthMin = g.getWidth();
int widthMax = dpWidth;
int randomWidth = new Random().nextInt(widthMax - widthMin + 1) + widthMin;
int heightMin = g.getHeight();
int heightMax = dpHeight;
int randomHeight = new Random().nextInt(heightMax - heightMin + 1) + heightMin;
TextView test = (TextView) findViewById(R.id.textView11);
test.setText(String.valueOf(widthMax));
params.leftMargin = randomHeight;
params.topMargin = randomWidth - 40;
I have used this code to make a ImageButton & after that i want to change the size of it at run time. Both the codes the written below,
<ImageButton
android:id="#+id/btn_new"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/untitled3"
/>
public void scaler1() {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
btn_new.setMinimumWidth(width/4);
btn_new.setMinimumHeight(height/6);
}
But it is not working please give any solution of it.
This is the actual code i'm using
public void scaler1() {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
edit_screen.setWidth(width);
edit_screen.setHeight(height / 6);
lpsame = (LayoutParams) btn_0.getLayoutParams();
lpsame.width=width/4;
lpsame.height=height/6;
btn_0.setLayoutParams(lpsame);
LayoutParams lpedit = (LayoutParams) edit_screen.getLayoutParams();
lpedit.width=width;
lpedit.height=height/6;
LayoutParams lpequal = (LayoutParams) btn_0.getLayoutParams();
lpequal.width=width/2;
lpequal.height=height/6;
edit_screen.setLayoutParams(lpedit);
btn_2.setLayoutParams(lpsame);
btn_3.setLayoutParams(lpsame);
btn_4.setLayoutParams(lpsame);
btn_5.setLayoutParams(lpsame);
btn_6.setLayoutParams(lpsame);
btn_7.setLayoutParams(lpsame);
btn_8.setLayoutParams(lpsame);
btn_9.setLayoutParams(lpsame);
btn_add.setLayoutParams(lpsame);
btn_subtrac.setLayoutParams(lpsame);
btn_multiply.setLayoutParams(lpsame);
btn_divide.setLayoutParams(lpsame);
btn_dot.setLayoutParams(lpsame);
btn_percentage.setLayoutParams(lpsame);
btn_CA.setLayoutParams(lpsame);
btn_back.setLayoutParams(lpsame);
btn_equal.setLayoutParams(lpequal);
editParams = new RelativeLayout.LayoutParams(width, height/6);
sameparam= new RelativeLayout.LayoutParams(width/4,height/6);
equalparam= new RelativeLayout.LayoutParams(width/2,height/6);
edit_screen.setLayoutParams(editParams);
}
You can change properties of a View by using LayoutParams class
LayoutParams lp = (LayoutParams) btn_new.getLayoutParams();
lp.width = yourWidth;
lp.height = yourHeight;
btn_new.setLayoutParams(lp);
Update
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(yourWidth, yourHeight);
yourButtons.setLayoutParams(layoutParams);
You have to get an instance of your view, before changing its properties
ImageButton btn_new = (ImageButton) findViewById(R.id.btn_new);
try this:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
LayoutParams layoutParams = image_new.getLayoutParams();
layoutParams.width=width/4;
layoutParams.height=height/6;
image_new.setLayoutParams(layoutParams);
I have a layout with a background image. The image width should be the same as the screen width (match_parent). But it's ugly, the OS don't stretch the picture in height.
I heard about ImageView scaleType, so I have the background image in an ImageView instead of a layout background, but I can't config to be what I want.
How can I set to the layout or ImageView to scale the image (in width) to fit the width of the screen AND scale the height with the same scale?
I want the second screen in the picture:
UPDATE:
I'm trying from code, but I get 0 width + I can't believe this can't be done from xml with an ImageView.
Drawable imageDrawable = getResources().getDrawable(imageResource);
contentContainer.setBackgroundDrawable(imageDrawable);
Rect rect = imageDrawable.getBounds();
int originalWidth = rect.width();
int originalHeight = rect.height();
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = originalHeight * (width/originalWidth);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
contentContainer.setLayoutParams(layoutParams);
contentContainer.post(new Runnable() {
#Override
public void run() {
contentContainer.setScaleY(contentContainer.getScaleX());
Rect rect = contentContainer.getBackground().getBounds();
int originalWidth = rect.width();
int originalHeight = rect.height();
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = originalHeight * (width/originalWidth);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
contentContainer.setLayoutParams(layoutParams);
}
}
Use FrameLayout as parent and then add imageview as child.
Give the width match_parent and height fill_parent to the imageView and another ways is apply the android:layout_weight attribute try this may be it's work.
you can do this at runtime by measuring the width of the device like this:--
super.onCreate(savedInstanceState);
setContentView(<layout-resoure-id);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int imageSize = 0;
if (width > height)
imageSize = height;
else
imageSize = width;
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.getLayoutParams().width = imageSize;
imageView.getLayoutParams().height = imageSize;
imageView.setImageResource(R.drawable.pattu);
your xml:--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"/>
</LinearLayout>
Enjoy...!
Have you tried playing with scaleType in you layout file?
<ImageView
...
android:scaleType="fitStart"
/>
This should scale your image to fit at the top of your layout, stretching or shrinking if necessary. Haven't had a chance to test (and I can't remember the height/width settings--sorry!), but this could be a solution.
ImageView imageView = (ImageView) findViewById(R.id.your_image_id);
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
int imageSize = height>width?width:height;
your configurations go here !
imageView.getLayoutParams().width = imageSize;
imageView.getLayoutParams().height = imageSize + (/* additional height logic*/);
you should put the imageView scaletype as fitXY and do this to stretch the image like the way you want!
<ImageView
...
android:scaleType="fitXY"
/>
I have an activity with
android:theme="#android:style/Theme.Dialog"
to show it as float window. Also I have a line
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
And I need a width/height of this window to show content correctly.
I've tested four options and did not get an answer:
//1
int width = getWindowManager().getDefaultDisplay().getWidth();
//2
int width = getWindow().getDecorView().getWidth();
//3
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
//4
int width = getWindow().getAttributes().width;
Thanks!
use this function:
private int getting_screen_width(Dialog dialog)
{
DisplayMetrics dm = new DisplayMetrics();
dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
String width = ""+dm.widthPixels ;
dm = null;
return (Integer.parseInt(width));
this return the value of the screen width