How I align two spinners evenly?(%50-%50) - android

I mean I have one line and I want to put 2 spinner in there with horizontal. I want to put them evenly. But I dont set it.When I set it 3,3 inc screens , it is problem with other screens because I use dp . What do I use? Lineerlayout or relative? Anad How I set them?
Thanks.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="309dp"
android:layout_height="wrap_content" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
THIS IS IMG TO SHOW MY PROBLEM : http://img4host.net/viewer.php?img=220942354fe421eb92ec5

use weight sum in Linear layout .....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
</LinearLayout>

You can use the below code to get the width dynamically independent to devices,
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.heightPixels;
int width=metrics.widthPixels;
Now split the width into two, width=width/2; and set the width to your spinners in your Activity dynamically.
spinner.setLayoutParams(new LayoutParams(width,LayoutParams.WRAP_CONTENT));

Use android:layout_weight="float value here" inside your spinners. That should do the trick.
See this for explanation.

Related

set Two text box in list view one with fix size and other with dynamic size

hi I am creating ListView with the two TextView in android.
The one contain the fix size and the other will occupied the remaining space of the screen.
The first which is fix size will display on the right side of the screen and other TextView will display data on the left of the screen.
How Can i do this?
isn't that ok?
<TextView
android:layout_height="wrap_content"
android:layout_width="480dp" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
Create a LinearLayout with horizontal orientation, and match_parent width.
Put both TextViews in it. For the first on, use android:layout_width and specify the value in dp if you want the buttons physical size to be the same on every screen, or in sp if you want it to scale according to screen resolution.
For the second TextView, use android:layout_width="match_parent".
Try this code...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="70dp" //Set the dimension
android:layout_height="wrap_content"
android:text="Text1" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text2" />
</LinearLayout>
Try this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv1"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:text="dfafa"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="dfafa"
android:layout_toRightOf="#+id/tv1"/>
</RelativeLayout>

How to make buttons to take space and increase size of as much as they want with respect to the resolution?

I have a layout in which buttons placed on a screen, i want that if the screen has more space then buttons should be three per row, whatever the space they get, also their sizes should be adjusted with screen and i want maximum three buttons per row in the centre of the screen. Right now i have used linear layout for each row of two buttons, please guide me how to make adjustable buttons so if the screen size is big, it should display three buttons max, if size is very small it should display only two buttons per row with small button sizes and as explained on default screen it should display three buttons per row.
See my code example here
<LinearLayout android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:paddingTop="25dip"
android:layout_width="fill_parent">
<Button
android:id="#+id/Car"
android:layout_width="110dip"
android:layout_height="110dip"
android:layout_marginLeft="10dip"
android:text="#string/car" />
<Button
android:id="#+id/Taxi"
android:layout_width="110dip"
android:layout_height="110dip"
android:layout_marginLeft="10dip"
android:text="#string/taxi" />
</LinearLayout>
You can use layout_weight to set the width of the button automatically according to the width of LinearLayout.
<LinearLayout
android:id="#+id/wrapper"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:paddingTop="25dip"
android:layout_width="match_parent">
<Button
android:id="#+id/Car"
android:layout_width="0dp"
android:layout_height="110dip"
android:layout_marginLeft="10dip"
android:layout_weight="1"
android:text="#string/car" />
<Button
android:id="#+id/Taxi"
android:layout_width="0dp"
android:layout_height="110dip"
android:layout_marginLeft="10dip"
android:layout_weight="1"
android:text="#string/taxi" />
</LinearLayout>
However, I don't think you can set the LinearLayout to have three buttons depends on the width by XML. You can only achieve it programmatically.
if(getCurrentScreenWidth() > SPECIFIED_WIDTH_FOR_3_BUTTONS){
LinearLayout layout = (LinearLayout)findViewById("wrapper");
Button button = createNewButton();
layout.addView(button)
}
BUT the best practices will be to load different layout according to screen as described in android document
This is how I would do this.
For each of your buttons set width like:
<Button
android:id="#+id/Car"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="110dip"
android:layout_marginLeft="10dip"
android:text="#string/car" />
If you set the width to 0dp and the weight to 1 for each of your buttons then the buttons will expand to fill the space.
Using fix value for height and width may causes the problem with different screen. use below xml code to align button as per your requirement.
<?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="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/Car"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/car" />
<Button
android:id="#+id/Taxi"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/taxi" />
<Button
android:id="#+id/bus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/bus" />
</LinearLayout>

Large text coming wrapped in a Textview

This is what is expected to come-----
The first text view:
Fox Pro
Drag
Main View Item
This is what is coming-----
And the other text view:
Fox Pro
Drag
Main View Item
So if the text is long enough to certain limit, it is coming wrapped in a next line.
Please note that the problem is only coming in the higher ppi 7inch device(~213 ppi).
The same thing is coming fine in lower ppi 7inch device(~160 ppi).
I tried to do the following programatically,
setting the width to full match_parent-------
DisplayMetrics dm = new DisplayMetrics();
// Display device dpi value of Y in pixels
int screenDPIy = (int)dm.ydpi;
if(screenDPIy > 180)
{
entrytype.setTextSize(14);
entrydate.setTextSize(12);
entrytype.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
entrydate.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
else
{
entrytype.setTextSize(16);
entrydate.setTextSize(14);
}
No Success.
My Layout xml:
<?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="vertical" >
<LinearLayout
android:id="#+id/mapentrieslist"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="#drawable/selector"
android:gravity="left|center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:layout_weight="0.9"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.47"
android:orientation="vertical" >
<TextView
android:id="#+id/entrytype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fox Pro"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/vehicle_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:visibility="gone"/>
<TextView
android:id="#+id/entrydate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#989898"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
You have a number of options:
Add a marquee that automatically scrolls : textView marquee
Place the TextView inside of a Horizontal ScrollView instead of a LinearLayout
Set the TextView to put an ellipses at the end : Detect whether TextView in ListView is ellipsized
The first two preserve the text while the third removes the extra characters.

Proper way to divide into 4 pieces an android layout

what I want to achieve is, divide the rows layout described in picture below. what should I do to achieve dividing the row into 3 exactly same size and 1 unknown size? X are same size and i dont know and dont want to specify if its not necessary...
EDIT: buttons are on the left , center, and right.
Use a LinearLayout inside a RelativeLayout. Put 3 items inside the LinearLayout and give them the same weight. Put the unknown item to the right of the LinearLayout with the help of RelativeLayout.
Left elements will align themselves according to the right-one's width.
Here's the code: https://gist.github.com/3772838
And here 2 screenshots with different sized right most elements:
http://goo.gl/Nezmn
http://goo.gl/XbQwL
Kolay gelsin =)
You can use android:layout_weight to distribute extra space proportionally. You want the three left buttons to absorb all the extra width, so the right (fourth) button should have the default weight of 0. Since you also want them to have the same width, the easiest is to assign them a width of 0dp and give them all the same weight:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Does your extreme left size has a minimum width ?
If so, you should use a LinearLayout with horizontal orientation.
It could contains 2 LinearLayout, one which contains 3 Views (your Buttons) with 0 width and with 1 weight each and the other LinearLayout has a minimumWidth set.
Instead of the marginRight, you could specify a width for the first layout.
Ted Hopp get it right ;)
<?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:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/rlayoutParent">
<RelativeLayout
android:layout_width="wrap_content"
android:id="#+id/rlayoutButtons" android:layout_height="wrap_content">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" android:layout_toRightOf="#+id/button1"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" android:layout_toRightOf="#+id/button2"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rlayoutOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/rlayoutButtons" android:gravity="right">
<Button
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
</RelativeLayout>
you cab use the layout_weight attribute. give all the x layout the same weight and the question mark a diffrent weight until the screen will devide as u like

Android layout, how to position two images and an seekbar

What I want to achieve
I'm trying to make a layout equal to the next link:
http://www.flickr.com/photos/59647462#N08/7038207573/
What I have
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/minVolume"
android:src="#drawable/ic_volume_min" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/maxVolume"
android:src="#drawable/ic_volume_max" />
</LinearLayout>
the result I'm getting with this code that the seekbar extends over the right icon.
I would like to have it scaled appropriately on different screenszes
If its possible without using the weight attribute, i would love that solution
any advice is appreciated
Use android:layout_weight="0.9" in SeekBar. or
Without using layout_weight
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="#string/minVolume"
android:src="#drawable/ic_volume_min" />
<ImageView
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/maxVolume"
android:src="#drawable/ic_volume_max"
android:layout_alignParentRight="true" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/left"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/right" />
</RelativeLayout>
make a relative layout and make the left image anchored to the left side (ALIGN_PARENT_LEFT), the right image anchored to the right side (ALIGN_PARENT_RIGHT) and the seekbar between them with android:layout_toRightOf and android:layout_toLeftOf.
For more info see the docs: http://developer.android.com/reference/android/widget/RelativeLayout.html
If you set layout_width to 0 for all the three widgets and weight to 1 for the seekbar that should work. I don't know how to do that without weight (and why?)
Use the windowManager to get the size and then adjust using operators like '/' etc,
Display display = getWindowManager().getDefaultDisplay();
int height = display.getHeight();
int width = display.getWidth();
Although i believe newer API has come into force.

Categories

Resources