Set Width And Height For Phone And Tablet - android

i am developing webview android app for phone and tablet, and i am using tablelayout, this is my front page
i want to set width and height that will fit to screen when comes to tablet or big screen, how can i ? this is my xml
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/button"
android:layout_weight=".50"
android:layout_height="150dp"
android:background="#drawable/hindi"
/>
<Button
android:id="#+id/button1"
android:layout_weight=".50"
android:layout_height="150dp"
android:background="#drawable/punjabi"
/>
</TableRow>

Do you mean you want different layouts that will fill the screen for tablets and other big screens?
Example:
tablets = 1 row has 3 buttons
big screens = 1 row has 5 buttons
I think the best way to do this is to create different XML layouts for each screen size. You can have 1 layout for phones (ex. 2 buttons per row) and 1 layout for tablets (ex. 3-4 buttons per row). You can then adjust the layout separately. I suggest you read the How to Support Multiple Screens section of the Android developer docs for more info, since I can't see you're entire XML.
Also, if you're table and TableRow fills-up the entire width of the screen, I suggest using match_parent instead of wrap_content.

Related

How to make layout for diffrent screen sizes using Relative Layout for Android

I have coded the whole app and designed all the layouts in Relative Layout, but now I want to make it compatible with all the screen sizes.
I would recommend using the percent relative layout
It offers the following attributes which you can use to set dimensions accordingly.
layout_widthPercent
layout_heightPercent
layout_marginPercent
layout_marginLeftPercent
etc.
In most cases Android will automatically resize your layout to fit the screen. However, if you want to ensure that elements will be placed the way you define them to be then use different layouts for different layouts for different screen size. The configuration qualifiers you can use to provide size-specific resources are small, normal, large, and xlarge. For example, layouts for an extra-large screen should go in layout-xlarge/. Android documentation
Try assigning weights to UI components. Works like charm, not matter what screen size is
example
<LinearLayout
android:weightSum="2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:text="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="1"
android:text="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

How it works that XML show all on display?

How it works, that android show me all boxes on display, whatever I have a 4" Display or a 7" Display? It's okay, that android show the boxes a little bit smalls, but I must have all the boxes on display.
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:background="#drawable/box_ressourcen">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/box"
android:id="#+id/box1"
android:showDividers="beginning"
android:layout_gravity="top"
android:layout_alignParentStart="true"
android:layout_alignParentTop="false"
android:layout_below="#+id/imageView"
android:layout_marginTop="-20dp"
android:layout_alignParentEnd="false"
android:gravity="center">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/myimage1"
android:src="#drawable/inventar"/>
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentStart="false"
android:src="#drawable/inventar_ressourcen"
android:layout_marginTop="-25dp" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/box"
android:id="#+id/box2"
android:showDividers="beginning"
android:layout_gravity="top"
android:layout_alignTop="#+id/box1"
android:layout_toEndOf="#+id/box1"
android:gravity="center">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/inventar" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/box"
android:id="#+id/box3"
android:showDividers="beginning"
android:layout_gravity="top"
android:layout_alignTop="#+id/box2"
android:layout_toEndOf="#+id/box2"
android:gravity="center">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/imageView3"
android:src="#drawable/inventar" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/box"
android:id="#+id/box4"
android:showDividers="beginning"
android:layout_gravity="top"
android:gravity="center"
android:layout_alignTop="#+id/box3"
android:layout_toEndOf="#+id/box3">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/imageView4"
android:src="#drawable/inventar" />
</LinearLayout>
</RelativeLayout>
And how I can change it, that the boxes was show in a line and break on the end of the line and go to the next line?
Example:
MyBoxes:
B1 B2 B3 B4 B5 B6 B7 B8 B9 B10
Display:
B1 B2 B3 B4
B5 B6 B7 B8
B9 B10
It depends with the sizes of your Views.
Its up to you to decide that you are going to show everything in the screen size or you let it to scroll in the screen.
You can divide your screen using XML , code or using both.
If you want you can go for flexed sizes (width / height) or wrap it and when you wrap it ,it will take the height of its child/children or you can separate layouts with a ratio again using XML ,code or both.
read about weight_sum and layout_weight
also how to give sizes to layouts based on the screen size
What is android:weightSum in android, and how does it work?
https://developer.android.com/training/multiscreen/screensizes.html
https://developer.android.com/guide/practices/screens_support.html
https://developer.android.com/training/multiscreen/screendensities.html
easiest one for you at this stage will be this
https://stackoverflow.com/a/12302811/5188159
also read about how to set sizes based on screen size which will display your XML in the same way in any screen
simple math - allocate my layout height 10% from the screen so the size will keep the ratio in any kind of a screen
as a code snippet this will give you the screen size height and width for the latest visions of android
DisplayMetrics displaymetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
now you can set a size problematically using the ratios
To learn it you can search ;) i'm not telling you that :) and good luck!
For each size you should make a different layout, in order to do that you create another layout with the same name that the one you already have but you add a qualifier like this :
You click on size and choose the one you want. Do this for each size (sall, normal, large, X-Large) and it should be ok.
From the way you speak it is clear that you miss the theorical part.
Basically you have to do different layout for the major configurations you could need, in your case one basic one like you did it and then if I well understand one for smaller screens, in your case 4 inches. To understand how you should name them i would warmly recommend you to go trought he documentation for inches is considered a normal screen that are at least 470dp x 320dp
In particular I suggest you this passage, where is explained also why is not best practice to call the four inches screen as normal
Provide different layouts for different screen sizes
By default, Android resizes your application layout to fit the current
device screen. In most cases, this works fine. In other cases, your UI
might not look as good and might need adjustments for different screen
sizes. For example, on a larger screen, you might want to adjust the
position and size of some elements to take advantage of the additional
screen space, or on a smaller screen, you might need to adjust sizes
so that everything can fit on the screen.
The configuration qualifiers you can use to provide size-specific
resources are small, normal, large, and xlarge. For example, layouts
for an extra-large screen should go in layout-xlarge/.
Beginning with Android 3.2 (API level 13), the above size groups are
deprecated and you should instead use the swdp configuration
qualifier to define the smallest available width required by your
layout resources. For example, if your multi-pane tablet layout
requires at least 600dp of screen width, you should place it in
layout-sw600dp/. Using the new techniques for declaring layout
resources is discussed further in the section about Declaring Tablet
Layouts for Android 3.2.
This is a basic skill every Android developer should know, so I would highly recommend you to learn how it works.

Too much padding around Button's Text

I have a fragment that takes up the whole screen, with Buttons and a SeekBar which scale to fit it, as well as fixed size TextViews. I use linear horizontal and vertical layouts with weights to achieve this.
The problem is I can't get the button text large enough without it making the buttons expand in size. For some reason, any text size greater than about 35sp makes the button expand, no matter how big the button is. This screen shot shows the button sizes have plenty of space for the text:
Ideally I would like the "<" and ">" characters to fill the buttons. (I was going to programmatically change the font size according to the button size, e.g. for different screen sizes) but haven't tried since I can't even get the static layout to work.
Edit: I would like to avoid images, since if I had 15 buttons, and 8 buckets, that would be 120 images I need!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/VerticalLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp" >
<!-- ........ -->
<TextView
android:id="#+id/trackTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2" >
<Button
android:id="#+id/trackPreviousButton"
style="android:buttonBarStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/button_track_previous"
android:textSize="35sp" />
<Button
android:id="#+id/trackNextButton"
style="android:buttonBarStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/button_track_next"
android:textSize="35sp" />
</LinearLayout>
<SeekBar
android:id="#+id/seekBar"
style="#style/tallerBarStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<!-- ........ -->
</LinearLayout>
I have tried adding the following line to Buttons, but it only makes a small difference, if any:
android:padding="0dp"
Advice on getting the font height to fill the buttons without padding is my primary question. (But if the problem of dynamically sizing the text to fill the buttons for different screen sizes can be solved at the same time, that would be brilliant).
Edit: it turns out that using larger font sizes affects the effect of weighting for the height of the linear layouts, which is why there seemed to be padding - larger font size increased the button size, not because of the padding (which was 0) but because of the weighting
Button is not the right widget for your purpose. Use an ImageButton (or even an ImageView) instead.
I was going to programmatically change the font size according to the button size, e.g. for different screen sizes
Your current approach will land you in a lot of problems regarding proper sizing of your UI components. Given the plethora of android devices out there, screen size is just one aspect of the problem. You will also be dealing with varying screen densities. Best approach would be to put size/density buckets (drawable-mdpi/hdpi/xhdpi) to use. Help android in working for you.
Use drawables to indicate next and previous. If you're worried about the drawables being too small for tablet screens, create appropriate drawable resources/folders:
// Phones - 4 to 7 in
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
// Tablets - 7 to 10 in
drawable-large-mdpi
drawable-large-hdpi
// Tablets - 10 in
drawable-xlarge-mdpi
This list may not be exhaustive. Consider doing some research before finalizing your size/density buckets.
Output:
# drawable size 32dp:
# drawable size 64dp
Now it becomes quite straightforward - finalize drawable size by visual inspection on a phone, on a 7 inch tablet, and on a 10 inch tablet. Then use density scales to create and store appropriately sized drawable in the folders I mentioned above. Let android deal with what bucket to pick from.
The problem is by default buttons include a minHeight attribute. I had the same problem and solved it with just a single line of code in my XML file:
android:minHeight="0dp"
There is a quick and easy solution to your problem!
Auto-sizing text in Android is fiendishly difficult in my experience, especially when padding is involved. I would advise that instead of using an angle bracket character, you use a drawable - there are plenty of arrow icons available online - and an ImageButton. For example:
<ImageButton
android:id="#+id/trackNextButton"
style="android:buttonBarStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/left_arrow"
android:scaleType="fitXY"
android:padding="0dp"
android:textSize="35sp" />
By using different ScaleTypes you can alter the stretching of the image. Even better, the screen sizes problem is solved because you can add different drawables for different densities.
Use minWidth="0" or "1" to reduce the horizontal padding on a text Button.

Stretching an image relative to screen size

I have a layout that features six buttons. All of them have the size of 72x72dip, so they appear to be larger on small screens and vice versa.
How can I tackle this problem? I want the buttons to be relatively the same size (eg 10% of the screen's width), but I don't know how to do it.
This is how my layout looks:
The layout's sourcecode can be found here.
1 - Did you fix all you 'image views' to layout_width & height of 72dip ?
2 - Normally 72dip is the same thing on every screen size... But you still can have 3(or 4 if you support XHDPI) resolution of your images...
2 - Add 4 différent fonder for you Drawable :
2.1 drawable
2.2 drawable-ldpi
2.3 drawable-mdpi
2.4 drawable-hdpi
Consider the following ratio for your images :
ldpi is 3
mdpi is 4
hdpi is 6
So make sure to make HDPI images first, so you can isealy resize other images applying ration 1.5 and 2 (just divide by 2 for LDPI for example).
3.By the way, why did you just did it all by yourself ? Seems you are on android 4.0, why don't you just use the new "DashBoard" class from Android ?
I usually do this by setting the width and height to wrap_content and then tweaking the layout_weight on the ImageButton widgets so that each button takes up the same amount of space. I usually do scaleType="center_inside" to make them line up nicely. Then pad around the edges of the TableView with margins as necessary.
Few quick observations:
a. You do not need TableLayout, just use LinearLayout
b. You do not need separate ImageView and TextView, you can use Button and image to it using
android:drawableTop="#drawable/"
c. Never use absolute values in width/height of any views, so use wrap_content.
Sample Layout should be like this:
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<!-- Row 1 -->
<LinearLayout
android:layout_height="0dp"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:layout_width="0"
android:layout_weight="1.0"
android:text="Preferences"
android:drawableTop="#drawable/Preferences" />
<Button
android:layout_height="wrap_content"
android:layout_width="0"
android:layout_weight="1.0"
android:text="Preferences"
android:drawableTop="#drawable/Preferences" />
</LinearLayout>
.
.
.
</LinearLayout>
</LinearLayout>

Single Layout for all screen sizes

I have read many documentation on supporting multiple screen sizes but still confused.
I have read like if specified in dp it will get automatically scaled.
In my sample i just have a single layout. there i have placed an edit text.
and given width , suppose 170 dp. suppose width wise it is half the screen in a 5 inch phone emulator.
What i was expecting is, if i show it on an tablet 7inch or 10 inch , there also it will be half the width of the screen (i.e it will get automatically scaled). But it didnt. in a 10 inch tablet it is only very small size.
what should i do to achieve this only through layout xml ( only single xml )
Dont set a Static width for the Textview as android will automatically scale it the the correct size using
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Basically what this does is makes the TextView the full size of the width of the device, and wraps the Height only to the needed height of the text.
YOu could also do warp_content for width to if you wanted. This is the best way. If you set it statically, you will need to create different layouts using XML.
Learn to use layout weights, you can use them like a percentage of 100.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />
<View
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="50" />
</LinearLayout>
Here the textview takes up 50% of the screen width and the other view is a kind of invisible spacer taking up the other 50% of the screen width.
Which looks something like this (Eclipse XML preview):
As you can see the textview wraps when it gets to 50% of the screen. Working on all sizes of screen.

Categories

Resources