Control tablet / phone layout on 3.2+ - android

3.2 introduced a great new way to create layouts... by using layout-swXXXdp in the res folder.
The problem is it controls what layout is used by the SmallestWidth in dp. This is great, but if you have a tablet that's 1024x600 and a tablet that's 1024x768, there is an issue. My app is landscape only. It is a gridview with a certain number of items in a row. The problem is that I need the gridview to adjust how many items are in a row based on the height resolution. All screens that are 1024 pixels should have 5 items in a gridview row, while all tablets that are 1280 pixels should have 6 items in a gridview row. It should not be based on width because then the gridview gets a little squished. How can I do this? (base my layout choise on the height, not width)

Please try this
Get Height and Width of the Display
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Check the height after declaration of GridView and set Number of columns accordingly
For example
GridView gridView = (GridView)findViewById(R.id.app_gridview);
if(height <= 1024){
gridView.setNumColumns(5);
}else {
gridView.setNumColumns(6);
}
I think this will solve the problem

Related

Android studio layout resource for screens with height bigger than width

In android studio I know that, for example if I will create a folder with name layout-sw600dp, the layout files inside that folder will be used for screens with minimum of 600dp width. I want to know is there a way to name a folder, which will be used for screens with height bigger than width? And if there is no way, how to create behavior like that(i.e. separate layouts for screens with height bigger than width and with width bigger than height)?
You can measure device's height and width with this code:
View view = getLayoutInflater().inflate(R.layout.your_activity, null);
//For example, linear layout
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.yourlinearlayout);
linearLayout.measure(0,0);
int width = linearLayout.getMeasuredWidth();
int height = linearLayout.getMeasuredHeight();
if(width > height) {
// your code here
}
You can use the folder name layout-port - indicating that the device is in portrait mode (taller than wide) or layout-land for landscape mode (wider than tall).
See here for more information on the available options.
There are some screen sizes with width longer than height (tablets for example).
you can check the screen aspect in xml using the "-long" "-notlong" suffix in your layout folder.
This is based purely on the aspect ratio of the screen (a "long" screen is wider). This isn't related to the screen orientation.
>long: Long screens, such as WQVGA, WVGA, FWVGA
>notlong: Not long screens, such as QVGA, HVGA, and VGA
https://developer.android.com/guide/topics/resources/providing-resources

Android Studio: Flexible Gridlayout

I started to create an app with the gridlayout. After I wrote the XML-knote of the gridlayout I placed within the gridlayout several buttons.
But if I change the Android Device from Nexus N to Nexus S the Layout starts to look like this:
and the same problem with the size goes on with Nexus XL
What should I do to keep the layout size of Nexus N to all the other devices?
DisplayMetrics displayMetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height = displayMetrics.heightPixels /NumberOfButtons;
width=displayMetrics.widthPixels/3;
You need to set the button height dynamaically at the runtime
Through above code you can get height and width of screen.

how can i know the actual size of an imageView in my device?

I have an ImageView with matchParent property in width.
how can i know its runtime width on my device (using eclipse, without adding code programmatically)?
how can I know the conversion ratio between dpi to pxls in my device?
I have an ImageView ... how can i know its runtime width on my device
You can get the width when the ImageView is measured, i.e. later than the runtime. However you can get the screen's width and height like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
And if for example your ImageView takes up a specific portion of your screen you can measure it using the screen's width and height.
how can I know the conversion ratio between dpi to pxls in my device?
To convert say 20 pixels to DP do (adding to the above code):
int dp = 20 / metrics.density:
Extra: Here's a dpi to pixel calculator
If you don't want to use the TreeObserverListener way, you can try to get the View to measure itself and give you what the dimensions would be if it were calculated. In my experience, this has worked in most cases but not all; especially if the View is in some special dynamic layout or the ordering of the hierarchy prevents the dimensions from being calculated in the correct order. You might find luck in refering to this popular answer.

auto resizing textView with different density devices android

At the top of my app, I have a title which should be shown in the middle, and a button on the right. As the textViews length is behind my control, I sometimes have my title crossing the button due to long length of the content of it.
After following this, I somehow tend to solve the problem. My device was HTC desire. Unfortunately, if I check with Galaxy SIII, it doesn't do the trick.
I am wondering how I can manage this in terms of different devices with different densities.
My controls inside the relative layout
You can also check the device screen density by this--
WindowManager wm = (WindowManager) _context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
And can manage accordingly whats your apps needed..
just use weightsum in your xml and make width of all the views as fill parent ..... this makes auto resizing of your textview
you can maintain layouts according to their DPI`s
replicate the same XML data in XHDPI (As S3 falls in XHDPI) and test it similarly replicate the XML data in HDPI
but
keep in mind the following Thing Pixel Ratio of the layOut as
following
in LDPI its 1:0.75
in MDPI its 1:1
in HDPI its 1:1.5
in XHDPI its 1:2
Display display = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
float density = dm.density;
int screenWidth = display.getWidth();
With this code above, you'll have your screen density as float.. So you can use it to calculate your textView's width like:
int newWidth = (int) (density * 100);
which 100 is here based size.
Or you can have a ratio according to your screenWdith.
int newWidth = screenWidth / 2;

GridView looking similar in every device

I need to know how to set the LayoutParam according to the resolution of the mobile phones..
Im using a GridView where I'm using GridView.LayoutParams(45, 45).. Now if I'm using mobile with small screen size is ok.. But if I test with the big screen device like HTC Desire HD then its looking so small.. How to make everything as similar?
Does using android:numColumns or setNumColumns(int numColumns) not work for your needs?
http://developer.android.com/reference/android/widget/GridView.html#setNumColumns(int)
Divide your display's width in 3 parts (for 3 columns or 4 for 4 columns....)
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
imageView.setLayoutParams(new GridView.LayoutParams(width/3 , width/3));

Categories

Resources