Adding an ImageView at a random position in a LinearLayout - java

I want to add image views dynamically to a full screen linear layout. I have written some code but some image views are either partially or totally going outside the screen. Some times image views are well within the phone screen.
How can I ensure that they remain totally inside the screen.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final int widthArea = displayMetrics.widthPixels;
final int heightArea = displayMetrics.heightPixels;
final LinearLayout home = (LinearLayout) findViewById(R.id.llHome);
ImageView imgView = new ImageView(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200, 200);
imgView.setLayoutParams(lp);
imgView.setImageResource(R.drawable.img_1);
imgView.setX(r.nextInt(widthArea));
imgView.setY(r.nextInt(heightArea));
home.addView(imgView);

You should take into account the dimension of the image itself. Settings X and Y place its starting point within the screen but it might leak on a side.
First, load your image as a Drawable, then include drawable.getWidth() (and maybe even height, that depends on your layout) to your X position.
imgView.setX(r.nextInt(widthArea - drawable.getWidth()) + drawable.getWidth());

Related

How to get usable size screen size in android studio?

So I want to make an app compatible with multiple screen sizes, however the positioning of some buttons is done programmatically and it has to align with the background (timetable app, task aligned with proper day, start time and end time on a grid display). Hence, I need to get the screen dimensions.
Currently I'm testing this in Nokia 8.1 with height 2246px and width 1080px, however what I need is the actual usable height not the total height (minus the bar at the top and the bottom). After manually testing the usable height it seems to be 1887px, but I have not found a way to retrieve this number.
I tried these codes so far:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels; //2246px
int displayWidth = displayMetrics.widthPixels; //1080px
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels; //2034px
int displayWidth = displayMetrics.widthPixels; //1080px
WindowManager windowManager = (WindowManager) mainActivity.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y; //2034px
int width = size.x; //1080px
I thought maybe navigation bar is the problem
Resources resources = mainContext.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
int navigation_bar_height = resources.getDimensionPixelSize(resourceId); //126px
2034 - 126 = 1908px != 1887px (which is close but still to big, it goes off screen)
Furthermore, when I tried to make a new hardware profile with the phone dimensions (2246 x 1080), using 1887px goes off screen on the xml Design display, even though it shows up exactly right on my phone...
So yeah I'm lost, no idea how to get the real size or why the profile doesn't match. Please help.

How to convert dp into % of the screen (weight)?

I have multiple layouts. By all I have hight=0 and and weigth=something. But now I want to add a layout that will allways be 60px. All works perfectly but now I need to know how much weight is 60px because I neet it for something else I am working on. So is it possible to get screen dimensions in dp. NOTE: I need to know the dp for each section. image
How exact can this be?
you can get the screen width and height like this:
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
then you just divide the height you get by the number of layouts you want to display
EDIT: another option would be(if you know the number of layouts at compile time) to give each layout the same layout_weight value f.e. layout_weight=1

in a scroll view i need to set image height as activity height of the device

i need to set image height as activity height of the device programatically
So i need to retrieve activity size of the device, this size varies from device to device
i tried this
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
but this gives the entire size(including navigationbar and everthing) of the phone screen but i need the activity size
use a method like this:
public static Point getAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
to get the usable screen size
If you want visible activity height excluding toolbar. Calculate tool/action bar height and set your image view layout_height as
required height = activity height-toolbar height

JAVA: apply width and height to fragment

I'm new to JAVA (Android development) and want to apply a width and a height to a fragment. I've pointed out that this is not possible, but what is the alternative? Wrap it in an extra layout, ... ?
In the oncreate method (of the java file that instantiates the fragment) this is what I have. It creates the fragment that fills the whole screen of my device.
RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout);
Fragment playerFrag = new PlayerFragment();
getFragmentManager().beginTransaction().add(rl.getId(), playerFrag).commit();
This is what i tried to apply a width and height:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
ViewGroup.LayoutParams params = playerFrag.getView().getLayoutParams();
params.width = displaymetrics.widthPixels/2;
params.height = displaymetrics.heightPixels/2;
playerFrag.getView().setLayoutParams(params);
Any suggestions on this issue, tips?

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