Buttons positioning in different layouts - android

I am making a bus schedule app. I have a lot of buttons in the main activity. My phone has a HD screen, so I made the buttons for that screen resolution.
How can I make the positions of the buttons to fit every screen resolutions?

If you are creating the buttons through xml layouts, then size them using dip (density independent pixels) as opposed to px (normal pixels). If you are pulling these images from resources, then you will have to have resources for all screen resolutions placed within the corresponding folders within the project structure (hdpi, mdpi, ldpi, etc.) - this would consume quite a bit of memory though.

You can use GridView...It will allow you lots of button to equally distributed along the screen.
You can follow these tutorials...
Android GridView Layout Tutorial
Android GridView example
Android Custom GridView Example
Android Custom GridView with Images and Text

You can use GridView for that purpose.

If you do not want to use GridView you can use linear with vertical orientation as main layout and linear layouts that contain buttons with "horizontal" orientation as rows.
To make buttons fit same space on different resolutions you can use layout_weigth attribute.
Something like that
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<!-- yo can define as much buttons as you want -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<!-- yo can define as much buttons as you want -->
</LinearLayout>
<!-- and so on -->
</LinearLayout>
Defining layout this way may be not good for peformance so use it carefully

You can use grid view with 7 rows
GridView gridView = new GridView(context);
gridView.setNumColumns(7);

Related

adjust EditTexts and buttons with different screen sizes

How can I adjust EditTexts and Buttons to fit any screen size for android ? I'm trying to put some transparent EditTexts and Buttons in certain positions to fit my background image, but when I change screen size every thing changes. Here are my background image and my XML code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/login_page"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="10sp" >
<EditText
android:id="#+id/etLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:layout_marginTop="135sp"
android:background="#android:color/transparent"
android:hint="Login"
android:inputType="textCapWords"
android:padding="8sp" />
<EditText
android:id="#+id/etPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0sp"
android:layout_marginTop="-3sp"
android:background="#android:color/transparent"
android:hint="Password"
android:inputType="textPassword"
android:padding="8sp" />
<Button
android:id="#+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12sp"
android:background="#android:color/transparent"
android:minHeight="40sp"
android:minWidth="500sp"
android:text="Login"
android:textColor="#08b0ef" />
</LinearLayout>
I tried other units like dp and dpi and dip but no one is giving the expected result.
Have you tried using "android:layout_weight" ?
I think what your talking about to set your Layout objects in a certain position relative to the background and once set, stay fixed proportionally so when the background stretches our contracts so do the buttons so they stay in the same position over the background?
If so (I could be way off),
setting the layout_weight attribute inside of each child object of the LinearLayout can let you position everything relative to the screen size so it automatically changes with each screen size. It will take a little trial and error to get the right percentages but should work.
Also consider creating multiple xml layout definitions for the major screen sizes so the OS automatically calls the one it needs for a particular screen with a resource qualifier, that way you know it will display in the right position. For example a xml called activity_main in R.layout is inflated by default but if you also create a activity_main in R.layout-land, this XML will only be inflated if the screen is in landscape mode. So you can set the sizes of your editText and Buttons for multiple screen size.
If you want to create only one file for all layout and trying to make your screen universal, you should try Linearlayout with weight property.
Instead of using sp or px, you should use pd for margin, padding or any other properties in your layout. dp will render differently as per screen resolution.

supporting multiple screens issue

I'm a little confused after reading Designing for Multiple Screens in the Android documentation. There is a tool in Eclipse to preview the layout in different screens. And I can see from there that certain images are out of place and too big or too small. For example in this screenshot the big one is how it should look like and all the others are what it would look like in other screens. As you can see the jar with brain is out of place in all screens screens(except for Galaxy Nexus).
Throughout my application I've used all the best practices:
I have multiple versions of all the images located in drawable-xhdpi, hdpi, mdpi and ldpi, xxhdpi
I've used wrap-content and fill-parent wherever possible
I've used RelativeLayout
I've used dp for margins and paddings
layout xml for this particular layout in the screenshot:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/wall_nobrainjar"
android:orientation="vertical" >
<ImageButton
android:id="#+id/brainjar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="129dp"
android:layout_marginTop="215dp"
android:layout_toLeftOf="#+id/red_paint"
android:background="#drawable/brainjar"
android:onClick="zoomImage" />
<Button
android:id="#+id/riddle_book"
android:layout_width="70dp"
android:layout_height="30dp"
android:layout_alignBottom="#+id/red_paint"
android:layout_alignParentLeft="true"
android:layout_marginLeft="83dp"
android:background="#android:color/transparent"
android:onClick="zoomImage" />
<ImageView
android:id="#+id/zoomed_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+id/brainjar_zoomed"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/brainjar_zoomed_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/brain_grey"
android:visibility="gone" />
<ImageView
android:id="#+id/riddleBook_zoomed_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/riddle_zoomed"
android:visibility="gone" />
</FrameLayout>
<Button
android:id="#+id/red_paint"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/brainjar"
android:layout_marginRight="14dp"
android:background="#android:color/transparent"
android:onClick="zoomImage" />
</RelativeLayout>
What is my problem? the only thing I haven't followed is having multiple versions of layouts (-large, -small,etc). Is that the problem? If it is there a way to solve it without having many layout files. Currently I have only one layout folder, where I've put all my layout files.
Create different folders of name layout-small,
layout-large,
layout-xlarge
in res folder
Copy-paste all ur .xml's from ur layout to thes folder
Open all the xml's & set the margins with using different screen sizes one by one
Then u can run & see the app in different size emulators
It is the simplest way !
Short answer: Don't be lazy.
More detailed answer: Your background is scaled to fit the screen, but your other images are are not, so the ratio between the background and other images are different on different devices, if you look carefully you will see that the brain jar is not only in different places, but with different sizes. To solve this, you simply, or not that simply ;) need to add layouts for different screen sizes, to layout and scale your brain jar probably.

How to display google map in right half of the screen while left half of the screen contain list view

i am working on a app, where i need to display the google map on the right half of the screen of my device. The left half of the screen contain text in list view format. Please help me how to design the UI part or layout.xml.....Thanks in advance..!!!
Use linear layout with horizontal orientation and put both your mapview and listview inside it , now give similar weight to them .May be that will work .for example give layout_weight 1 or 0.5 in both mapview and listview .
Try use Fragments and here are some links to training
Using Fragments in Android
trading for fragments
Maybe you could look into something like this? I've used this format in apps before
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:gravity="center"
android:orientation="vertical">
<MapView />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ListView />
</LinearLayout>

Android Image Button Layout

Could someone please explain how android uses image button sizes? I seem to be getting odd behavior with my buttons.
I have the following code as an example. I have two buttons that sit at the bottom of my layout. These buttons share 50% of the total width as they sit side-by-side.
Within Abode PS, the two images (used for these two buttons) are actually 2" x 38" or 495x94 pixels. This size is of course larger than the available space in the layout.
I am using edge effects on my buttons to give them definition. Android is cutting the edges off my buttons in order to center then in the available layout space.
This particular layout that I am working on will only allow vertical orientation, in case that helps.
Thank you.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<ImageButton
android:id="#+id/ImageButton1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_weight="1"
android:background="#FF000000"
android:src="#drawable/map4" >
</ImageButton>
<ImageButton
android:id="#+id/ImageButton3"
android:layout_width="0dip"
android:layout_marginRight="1dip"
android:layout_weight="1"
android:background="#FF000000"
android:src="#drawable/buy"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip">
</ImageButton>
</LinearLayout>
</RelativeLayout>
Try using an ImageView and android:scaleType:
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
Experiment the values available to see what is the best combination!
Then add a listener to behave like a button...
try use the button and make it with empty text after this set the background .
or :
use the image button and put the source and the background the same image to get button exactly like the image
**you can use the selector to make some beauty for application buttons
Google it it's easy to use ;)

Scale layout items equally on large screens

I've got a 4-item start screen in my app, which looks like the following:
What's important to me there:
- All items do have the same width (not regarding how much text is actually in it)
- Look the same on all devices (small-screen, mdpi, large-screen, etc.)
Im just wondering if there is a easy solution about this problem?
I've tried using 3 LinearLayouts but thats really awkward..
(1 presenting the layout root[vertical] and two which do each contain 2 buttons[horizonal]).
Making this layout ready for multiple screens would require a lot of fixed-width and fixed-margin hacking. Just like "button margin = 30dp on xlarge, 20 on large, 15 on normal,...".
My layout-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:background="#drawable/background"
android:id="#+id/main_root"
android:orientation="vertical"
android:gravity="center" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center" >
<Button
android:id="#+id/btn_learn"
android:text="#string/mainBtn_learn"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
<Button
android:id="#+id/btn_quiz"
android:text="#string/mainBtn_quiz"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center" >
<Button
android:id="#+id/btn_search"
android:text="#string/mainBtn_search"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
<Button
android:id="#+id/btn_more"
android:text="#string/mainBtn_more"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
</LinearLayout>
Is there a view which "auto-scales" these Buttons or still any other easier solution?
Edit:
So, in special, you need something like
button:
android:layout_width="15%" // 15% of screen width / height depending on the orientation
android:layout_marginBottom="10%" // see above
I'm pretty new to Android development but I can show you what worked for me in a similar case. I defined my layout as follows:
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/outputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:editable="false" />
<Spinner
android:id="#+id/outputSpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:prompt="#string/OutputBaseOptionsPrompt" />
</LinearLayout>
I have a horizontal layout with two items. The LinearLayout has a width of "match_parent" so that it is as wide as the screen. Both items in the layout have the following:
android:layout_width="0dp"
android:layout_weight="1"
Since both items have a layout_weight of 1, they will be drawn at the same width. In this case, each item takes up half of the available space. If you change the weight of one of these items to "2" then it will be twice as wide as the item with a weight of "1".
Do you already have xml that makes it work on one screen size? If so post what you have so far.
I would suggest using a RelativeLayout for your root though. You can use the alignCenter attributes to float your children towards the middle. Then you just have to hard code the inner margins (how far apart you want the buttons) rather than the margin from yourself to the wall.
You could also avoid having to hard code the inner margin by making your own button 9 patch images. You can just add a border of transparent pixels in your image to represent the margin. You'll probably still want to supply an image for each density you wish to support though.
The solution is you dont use hardcoded values any where
Put three images with same name in hdpi mdpi and ldpi folders in drawables
an run the code

Categories

Resources