I am working on the layout of the android app but i have found that all buttons are fixed in a column in Eclipse atuomatically. What should i do to make it to the desired position like the app shown in the link?? thanks
http://www.dcemu.co.uk/vbulletin/threads/335622-Android-oscilloscope
You could use RelativeLayout instead of LinearLayout in the main.xml file.
In this layout, you can place the components at any place in the layout, but the components are placed relatively to one another.
You can use a mixture of layout to make your view look sound. Also try using the Layout Orientation i.e. either Vertical or horizontal,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
You could get 2 columns of buttons by having a vertical LinearLayout containing several horizontal LinearLayouts, each containing 2 buttons. Give the buttons equal weights to space them evenly, and some margin to make them look less cluttered.
E.g.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button 2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button 3" />
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button 4" />
</LinearLayout>
</LinearLayout>
Or if the columns were going to be very long, you could use a horizontal linear layout containing 2 vertical linear layouts, and add the buttons to those.
Or you could use a TableLayout with 2 columns and have TableRows containing the buttons (I generally find table layouts harder to work with, perhaps that's just me).
I find it easier to write the xml in Eclipse rather than fiddling round with the graphical editor, then just switch over to the graphical editor every now and again to check it displays how you want. Look around online for a few example layouts and you'll soon get the idea.
To emulate that layout exactly, start with a RelativeLayout as mihail suggests, and use that to position your other layouts (such as your linear layout with buttons) and views.
Related
I have a layout contain one image and 3 text field
I've tried to align the image to right and text field to left but I've failed
I've used
android:layout_gravity="right" for image and left to text but it did not work also I've used end and start in gravity with no success
this is the layout code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:orientation="vertical"
android:background="#drawable/card_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/listthumb"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:contentDescription="Rss video thumbnail"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/listtitle"
style="#style/listTitle"
android:maxLines="3"/>
</LinearLayout>
<TextView
android:id="#+id/shortdescription"
android:layout_width="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/listpubdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11dp"/>
</LinearLayout>
</FrameLayout>
Try to use a <RelativeLayout> instead of a <LinearLayout>
With the RelativeLayout you could place a widget depending on the position of another widget
Here the Relative Layout description
Hope this will help, I have not had time to test....
One linear layout should have vertical orientation and contain the 3 text fields.
One linear layout should have horizontal orientation and contain both the above linear layout and the image.
To push two views to the edges of the screen, you can also give each a left/right margin and then put a blank view with weight = 1 in between them.
Please read a bit more on how layouts work on Android and the different types available to you. A LinearLayout will stack the containing Views either Horizontally or Vertically one after the other. A FrameLayout is simply a container and the items within have to position themselves. RelativeLayout allow you to position your views with a relative reference to other views (in your case, you can position your ImageView, and then your 3 TextViews relative to where the ImageView is).
If you can use LinearLayout instead of RelativeLayout, you should do so, as RelativeLayout is always slower, due to having to perform two passes prior to rendering as it needs to measure each view and then also perform the layouts based on that. You might be looking for something like (pseudo-code):
<LinearLayout orientation=horizontal>
<LinearLayout orientation=vertical>
<TextView />
<TextView />
<TextView />
</LinearLayout>
<ImageView />
</LinearLayout>
You have not described your question well . Check below code if it works .
You just forgot to add orientation in linear layout containing one text view and a Image view .
Add Orientation to Your Linear Layout.
I am having some spacing trouble when building part of my UI programmatically in Android 4.0. I am trying to add stylized buttons to a stylized LinearLayout. To space the buttons equally, each one is wrapped in a LinearLayout with a weight of 1. I started with a layout defined in XML (somewhat of a proof of concept,) which renders like I expect:
<LinearLayout android:id="#+id/dialog_footer"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:background="#drawable/dialog_footer">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center">
<Button android:id="#+id/cancel"
style="#style/Button"
android:layout_width="130dp"
android:layout_height="38dp"
android:text="Cancel" />
</LinearLayout>
<!-- Another LinearLayout with a nested Button like the one above -->
</LinearLayout>
To add buttons programmatically, I removed the inner LinearLayouts and put them in their own layout file that I can inflate and add to the outer LinearLayout in Java. It is nearly identical.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center" >
<Button android:id="#+id/button"
style="#style/Button"
android:layout_width="130dp"
android:layout_height="38dp" />
</LinearLayout>
And this is roughly how I'm adding buttons in code:
LinearLayout dialogFooter = (LinearLayout)dialogView.findViewById(R.id.dialog_footer);
LinearLayout wrappedButton = (LinearLayout)getLayoutInflater().inflate(R.layout.dialog_button_wrapped, null);
Button button = (Button)wrappedButton.findViewById(R.id.button);
button.setText(R.string.button_one_text);
// button.setOnClickListener(...);
dialogFooter.addView(wrappedButton);
The buttons appear but now they are grouped together and shifted to the left. Is there something Android does when it parses a Layout that I would need to do myself if I'm adding to the dialog_footer? Since weights come into play here, I thought that calling setWeightSum() on the container I'm adding to (dialog_footer) might be necessary but that didn't help. Does anyone have any ideas what might be causing the difference between the XML and Java approaches?
I believe this is your problem:
LinearLayout wrappedButton = (LinearLayout)getLayoutInflater().inflate(R.layout.dialog_button_wrapped, null);
The null should be replaced with the parent view , so that it will get the layoutParams you want to set for it .
Another thing is about the weight you set - you should set the width/height to 0px so that the weight won't cause the layout process work in a weird/inefficient way .
BTW , you can remove the inner layout (that has the button) and use a single button instead. just set the layout_gravity there to center_horizontal .
I'm feeling difficult to implement , two widgets(say spinner) in linear layout one next to the other. I mean layout height of both spinner is wrap content, but width should be first half for the first spinner, second half of the screen to the second spinner. In linear layout they coming one down the other. I tried in Relative Layout, but as i gave width as wrap_content both are coming one next to each other but lot of space is remaining right to second spinner. I have seen in few apps this working out, but im not getting it.
Use layout_weight. This will force the two spinners to take up half of the space each.
<LinearLayout
android:orientation="horizontal"
... >
<Spinner
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
... />
<Spinner
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
... />
</LinearLayout>
I had the EXACT same problem one day. I've also tread a lot of different tricks to get it working. I ended putting both spinners in their own layout. Was weird but it worked.
relative layout is intended for putting views related to each other . it has nothing to do with their sizes.
for linearLayout , set the height/width (depending on orientation of the layout) of both views to 0px and the weight to 1 . this will make each take half the space.
<?xml version="1.0" encoding="utf-8"?>
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/testArray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spinner2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="#array/testArray" />
<Spinner
android:id="#+id/spinner3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
I'm new to android programming but from how much I have understood of the layouts from the documentation, RelativeLayout is mostly used when you need the views based on some rules and the FrameLayout when you want to overlap views.
But unfortunately for the following program I get the work of FrameLayout done by using RelativeLayout. I got my work done but for understanding, Am I missing something in the difference?
Also, how did the buttons come over my image? (Even the other image is overlapping.)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/ic_launcher"
/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_alignLeft="#id/imageView1"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1.0" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="Login" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="Register" />
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="Try application" />
</LinearLayout>
</RelativeLayout>
The RelativeLayout can use :
android:layout_toEndOf="#id/some_view"
android:layout_toStartOf="#id/some_view"
android:layout_above="#id/some_view"
android:layout_below="#id/some_view"
to make sure views lineup correctly in relation to each other. FrameLayout is very similar except it's only using gravity to put display its views (with no relation).
I would also suggest you to take a look at the ConstraintLayout component. ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). It's similar to RelativeLayout in that all views are laid out according to relationships between sibling views and the parent layout, but it's more flexible than RelativeLayout and easier to use with Android Studio's Layout Editor.
RelativeLayout based on relation of views. It is a layout manager that helps you arrange your UI elements based on some rule. You can specify things like: align this to parents left edge, place this to the left/right of this elements etc.
FrameLayout allows placements along Z-axis. That is you can stack your view elements one above the other.
RelativeLayout - As the name suggest in this viewgroup, view are placed relative to each other. Most used property of relativelayout are used are
android:layout_toLeftOf="#id/some_view1"
android:layout_toRightOf="#id/some_view2"
android:layout_above="#id/some_view3"
android:layout_below="#id/some_view4"
android:layout_toendof="#id/some_view5"
android:layout_tostartof="#id/some_view6"
View are placeed relative to each other. It is really helpful while developing complex designed.
FrameLayout - It behaves as a single object view are not placed relative to each but as per to the FrameLayout. FrameLayout takes the size of biggest child view.
android:gravity="center_horizontal|center_vertical|bottom"
Using above property child views position is modified.
I have a complex layout situation in which an horizontal LinearLayout holds two other LinearLayouts. The content for those layouts is dynamic, can be any kind of views, and is generated at runtime from different independent sources.
I want to display both of them as long as there is enough space, and limit them to 50% of the available space each otherwise. So I want those child LinearLayouts to have layout_width="wrap_content" when there is enough space, and layout_weight="0.5" when there isn't. This means that the space distribution could be 10-90, 25-75, 60-40; it would only be 50-50 when there isn't enough space to show the entire content of both views. So far I haven't find a way to do this from XML, so I'm doing it from code. My question is can I achieve what I want using only XML attributes? Will a different kind of layout be able to do it?
Here is my current layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="2dp" >
<LinearLayout
android:id="#+id/title_frame"
android:layout_width="wrap_content"
android:layout_height="48dp"/>
<LinearLayout
android:id="#+id/options_frame"
android:layout_width="wrap_content"
android:layout_height="48dp"/>
</LinearLayout>
Try this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="2dp" >
<LinearLayout
android:id="#+id/title_frame"
android:layout_width="wrap_content"
android:layout_height="48dp" android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:id="#+id/options_frame"
android:layout_width="wrap_content"
android:layout_height="48dp" android:layout_weight="1">
</LinearLayout>
</LinearLayout>
I tried it with textviews and this should work according to ure requirements.
It appears this cannot be achieved using only XML attributes.
Change layout_width="wrap_content" to layout_width="fill_parent" as you are using weight concept .