I am trying to create a custom tabbed layout with a viewflipper. Therefore, I need two buttons side-by-side at the top of the screen. I have this. However, I am trying to get the viewFlipper content below these two buttons. Here is my current XML (which does not show the textviews)
<LinearLayout
android:id="#+id/linearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FAFAFA"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/linearLayout02"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/button1" android:text="button 1" android:layout_height="wrap_content" android:layout_width="0dip" layout_weight = ".5"/>
<Button android:id="#+id/button2" android:text="button 2" android:layout_height="wrap_content" android:layout_width="0dip" layout_weight = ".5"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/relativeLayout01"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_below="#id/linearLayout02">
<ViewFlipper
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/viewFlipper01">
<include
android:id="#+id/one"
layout="#layout/view_one" />
<include
android:id="#+id/two"
layout="#layout/view_two" />
</ViewFlipper>
</RelativeLayout>
</LinearLayout>
Your LinearLayout that contains the buttons has a layout_height="fill_parent". You need to set that to wrap_content and also specify the orientation="vertical" in the parent LinearLayout. You'll also need to specify a layout_weight for the view that you want to stretch to fill.
Because linearLayout01 LinearLayout has its layout_height set to fill_parent, android is going to make it take up the reset of the screen. The content below that will not be visible at all because it is off the screen.
<LinearLayout
android:id="#+id/linearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/linearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/button01" android:layout_height="wrap_content" android:text="Button 1" android:layout_width="0dip" android:layout_weight="1"></Button>
<Button android:id="#+id/button02" android:layout_height="wrap_content" android:text="Button 2" android:layout_width="0dip" android:layout_weight="1"></Button>
</LinearLayout>
<RelativeLayout
android:id="#+id/relativeLayout01"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ViewFlipper
android:id="#+id/flipper01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/textview01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
/>
<TextView
android:id="#+id/textview02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text2"
/>
</ViewFlipper>
</RelativeLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="#+id/button01"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_width="0dip"
android:layout_weight="1" />
<Button android:id="#+id/button02"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_width="0dip"
android:layout_weight="1" />
<ViewFlipper
android:id="#+id/flipper01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/relativeLayout01"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- Screen 1: Wherever view you want to display on the first screen -->
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout02"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- Screen 2: Wherever view you want to display on the second screen -->
</RelativeLayout>
</ViewFlipper></LinearLayout>
Generally, you have to ViewFlipper that contains the two or more layout that you want to display when, for example, click a buttom and whatever you want to see in all of screen, write outside of ViewPager tag.
Related
I need to bring the whole content of linearlayout1 below linearlayout2 in java code. How do I do that?
<LinearLayout 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"
android:id="#+id/parent"
android:orientation="vertical"
tools:context="com.example.examplepro.MainActivity" >
1--------> <LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/layout1"
android:layout_weight="50" >
<Button
android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="1"
android:layout_weight="1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="1"
android:layout_weight="1"/>
</LinearLayout>
2------> <LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/layout2"
android:layout_weight="50">
<Button
android:id="#+id/btn3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="2"
android:layout_weight="1"
/>
<Button
android:id="#+id/btn4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="2"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
You can try removing the linear layout 1 first, and add it again.
As the parent layout is of vertical orientation, it should add Linearlayout 1 below LinearLayout 2.
mainLayout.removeView(ll1);
mainLayout.addView(ll1);
Hope this helps.
I want to make a layout in xml for a Fragment class. This layout have to be match_parent width and height.
Picture 1
In picture 1, the layout has four items added programatically...
Picture 2
In picture 2 is the same layout but with 6 items.
none of these layouts examples are scrollable, so if i add 8 items, all items has to fit to
the screen size.
how can i do this?
You can uses ScrollView.
if you use GridView now, ScrollView is contain GridView.
Or
You use over scroll mode if ifContentScrolls mode.
You can do it simply: refer to the LinearLayout by id, and add children programatically. If you want its children to have the same height, set their height to 0px and their weight to the same, 1 for instance. Since its children will be Linearlayouts as well, you can add the items manu1, menu2, etc to them without problem. I don't know you exact usage, but it's a start.
You can do this using layout_weight attribute in LinearLayout as below...
<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:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="Menu 1" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:text="Menu 2" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:text="Menu 1" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="Menu 2" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="Menu 3" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:text="Menu 4" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:text="Menu 5" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="Menu 6" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="Menu 7" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:text="Menu 8" />
</LinearLayout>
</LinearLayout>
How to you make 4 fixed size buttons in an horizontal LinearLayout fill the space of that layer horizontally, letting the Android put the needed space betetween those buttons to fill that space ?
Are you searching for something like this?
<?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="#FFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
If I understood correctly, you want to make those 4 buttons take equal space in a horizontal LinearLayout, if so, then add: android:layout_weight="1" in the XML layout, for each 4 buttons.
How to display the layout with a ListView at top and mutliple Buttons at the bottom (as in pic)
Below is the code that I tried (Problem is the buttons are overlapping the list which is somewhat visible behind the buttons & the 3 buttons are not equal is size though I used the weight sum):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="3" >
<Button
android:id="#+id/bDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Done" />
<Button
android:id="#+id/bCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:id="#+id/bSelAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Select All" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
What I did is basically do a 2 linear layouts, 1 for the list and 1 for the button. Also setting the weight = 1 and the width = 0 will make them equal size
This code worked for me:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_above="#+id/footerlayout"
android:id="#+id/listviewlayout">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</LinearLayout>
<LinearLayout android:id="#+id/footerlayout"
android:layout_marginTop="3dip"
android:layout_height="45dip"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/bDone"
android:text="Done"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1">
</Button>
<Button
android:id="#+id/bCancel"
android:text="Cancel"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
>
</Button>
<Button
android:id="#+id/bSelAll"
android:text="Select All"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
>
</Button>
</LinearLayout>
</RelativeLayout>
Add to LinearLayout id property and add this to ListView
android:layout_above="#id/linear_layout_id"
To make buttons equal size change in their properties this
android:layout_width="wrap_content"
to this
android:layout_width="match_parent"
I want an activity with below layout:
top button
middle(scroll view)
bottom button
I want the bottom button to attach to the bottom of the activity view, and the middle part occupies the rest of the space.
How I can write the layout xml file?
Below doesn't work:
<?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">
<LinearLayout android:focusable="true"
android:focusableInTouchMode="true" android:layout_width="0px"
android:layout_height="0px" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:weightSum="1" android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:weightSum="1">
<EditText android:id="#+id/edit_search"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:lines="1" android:hint="发微博内容" android:layout_weight="0.9" />
<Button android:layout_height="wrap_content" android:text="Search"
android:layout_width="wrap_content" android:id="#+id/search_btn"
android:layout_weight="0.1"></Button>
</LinearLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/TextView01"
android:paddingTop="5dip" />
<ListView android:layout_width="fill_parent"
android:id="#+id/lv01" android:layout_height="390dp">
</ListView>
<Button android:id="#+id/create_new_btn"
android:layout_height="wrap_content" android:text="我要评论" android:layout_gravity="bottom"
android:layout_width="fill_parent"></Button>
</LinearLayout>
</LinearLayout>
For that case, just take a RelativeLayout, its better for this case and you don't need to take any sub-layout.
Just try this once and post your XML layout here for the further improvement.
From your xml layout code, i can assume, you just want to have:
EditText+Button=>Top,
TextView+ListView=>Middle,
Create New button=>Bottom,
is this the case?
Keep it simple, go with LinearLayout, and play with the weights:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0">
</Button>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<!-- Stuff in scroll view -->
</ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0">
</Button>
</LinearLayout>
#ParashMayani is right..
Here is the implementation :
<LinearLayout android:focusable="true"
android:focusableInTouchMode="true" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/relLayout" android:orientation="horizontal" >
<EditText android:id="#+id/edit_search"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:lines="1" android:hint="Hello" android:layout_weight="0.7"/>
<Button android:layout_height="wrap_content" android:text="Search"
android:layout_weight="0.3" android:layout_width="wrap_content" android:id="#+id/search_btn" />
</LinearLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/TextView01"
android:paddingTop="5dip" android:layout_below="#id/relLayout"/>
<Button android:id="#+id/create_new_btn"
android:layout_height="wrap_content" android:text="我要评论" android:layout_gravity="bottom"
android:layout_width="fill_parent" android:layout_alignParentBottom="true"/>
<ListView android:layout_width="fill_parent"
android:id="#+id/lv01" android:layout_height="0dp" android:layout_above="#id/create_new_btn" android:layout_below="#id/TextView01" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="#+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></Button>
<Button android:id="#+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true"></Button>
<ScrollView android:id="#+id/scrollView1" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_below="#+id/button1" android:layout_above="#+id/button2">
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent"></LinearLayout>
</ScrollView>
</RelativeLayout>
I think something like that would work. The important part in that layout is the android:layout_below and android:layout_above. You can replace the ScrollView with other layouts such as ListView and it will remain below the first button and above the second regardless of how big it gets. Likewise, the buttons have the attributes android:layout_alignParentTop="true" and android:layout_alignParentBottom="true" which will keep them to the top and the bottom of the RelativeView respectively.