listView inside of a linear layout with something else - android

i was just wondering if it was possible to have in one layout xml file, maybe a vertical linear layout at the top have a horizontal linearLayout and then below that (inside of the vertical layout) have a list view that takes up the rest of the screen?
I couldnt find any information specifically on this, and i'd like to know if its possible before i attempt it, thanks!

Give the ListView layout_height="0dp" and layout_weight="1". It will take up the remaining space not used buy the sibling LinearLayout.
<LinearLayout
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:orientation:norizontal >
<!-- other views --->
</LinearLayout>
<ListView
android:layout_width:match_parent
android:layout_height:0dp
android:layout_weight:1 />
</LinearLayout>

Related

LinearLayout with percent empty space

I am working on linear layout for my simple android application. I wanna make the portion of two views dynamically change based on the size ( I want to have, for a row for left to right, the first 20% is empty, and all the content is inside the rest of 80%) . For this approach, i chosen the weight for different view. I created an nested linear layout for this approach. For example, the layout hierarchy is something like this.
<linearLayout> //parent layout
<linearLayout //child 1 layout
android:layout_weight="1">
//so that this view occupy 20% of the space regardless the width of device. I intensionally wanna keep this view empty.
</linearLayout>
<linearLayout //child 2 layout
android:layout_weight="4">
//so that this view occupy 80% of the space regardless the width of device. and
//inside this view I have whatever view I wanna add on it.
<EditText>
<ImageView>
</linearLayout>
</linearLayout>
With this approach, the Lint in Android Studio tell me the following warnings:
This is a Nested Layout. Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.
the child 1 layout is useless: This LinearLayout view is useless (no children, no background, no id, no style)
Can anyone address me the right layout to use in order to have the layout dynamically change based on the size of devices? How should I correctly set up the empty space for a linear layout case?
This is a possible solution using weights:
<LinearLayout
android:layout_width="match_parent"
android:gravity="end"
android:weightSum="1">
<!-- Your content here: -->
<View
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_gravity="end" />
</LinearLayout>
Have a look at PercentRelativeLayout.
Note: You need the Percent library to use it.

Most fitting View/Layout for placing several text- and image views inside, XML for Android

So I have this ScrollView inside which I have a RelativeLayout on one of my views on my Android app. Inside the RelativeLayout I already have a TableLayout that I'm using, and above it I want; 2 different text views (one header and one longer text, and I want the header to be placed on top of the longer bit of text) aswell as an ImageView that I want to be placed to the right of the 2 TextViews, and I want all 3 views to be placed on a differently colored background than the other stuff on the ScrollView such as the TableLayout for example.
I tried putting another RelativeLayout inside the ScrollView but it tells me ScrollView can only host one direct child, so that didn't really pan out. What would the most fitting way to accomplish this? Because I want this 3-view-background-thingie to scroll with the TableLayout and all the other stuff on the view.
As always, thankful for any answers or tips!
(My design kinda looks like this at the moment, schematically;)
<Container (that doesn't scroll)/>
<ScrollView
<RelativeLayout
*Alot of stuff here, such as a TableLayout for example
</RelativeLayout>
</ScrollView>
Put everything in your RelativeLayout
<ScrollView
<RelativeLayout
<LinearLayout>
<TextView>
</TextView>
<TextView>
</TextView>
<ImageView>
</ImageView>
</LinearLayout>
<TableLayout>
</TableLayout>
</RelativeLayout>
</ScrollView>
This way, the only child is the RelativeLayout, the others being children of the RelativeLayout.
Hope this helps!

How to float/overlap a view in android?

I have many activities with a scrollview inside a tablelayout. However, it is necessary a small design change, so I have to put a black transparent view over the whole screen from the top to the bottom. Is it possible to do it in the tablelayout or the scrollview?
RelativeLayout allows for easy overlapping of views. You'll have to adjust the existing views in your app because it doesn't do anything automatically.
EDIT:
A quick way to do this would be to take your existing view (the ScrollView) that is already organized and put it in a top-level RelativeLayout. Then, all you have to do is add new view inside the RelativeLayout with the width and height both set to MATCH_PARENT. The result should be the black transparent view will be visible over the ScrollView.
I normally use FrameLayout to achieve any kind of 'layering' of views.
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
//your existing layout
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#33000000" />
</FrameLayout>
As DeeV said, you can probably use RelativeLayout in a similar way, but you might have to set additional attributes on its children to achieve this.

How to create android activity that shows at specified screen position?

My current layout displays activity that is not full screen (that's OK).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="100dip"
android:layout_height="200dip" >
<TextView android:layout_height="wrap_content" android:id="#+id/textView1"
android:layout_width="fill_parent" android:text="#string/hello"></TextView>
I also added android:theme="#android:style/Theme.Translucent" to manifest for my activity.
My 200x100dip activity now shows in the upper left corner. How can i specify position of my linear layout (or my activity)?
You can use either FrameLayout or RelativeLayout as outer most layout for this. Ant then use absolute position in dp or android:layout_centerInParent or similar.
I believe your Activity´s outmost layout element (LinearLayout) will be placed in a FrameLayout that is the parent given from Android. I suggest you let your outmost layout match_parent/fill_parent in layout_height and _width and then center the content inside it with gravity="center" on your outmost layout. By letting the outmost layout being transparent and not catch click element it will appear as layout in the middle where elements behind is visible. If Im correct guessing that's what you want to achieve here.
put that layout in another absolute layout in which you use android:layout_width="fill_parent" and android:layout_height="fill_parent" the other thing you can do is to use this: http://www.droiddraw.org/
you can move your elements around manually with that and it will give you the XML code that is used to do that. I found it very useful in laying out XML in android.

Android: adding components to RelativeLayout

in My screen, i want to have Hearder(specifically image) on top, and List, and at bottom i want to have small imagebuttons (like youtube,facebook).
I'm using RelativeLayout.. first two are displaying correctly, last one not display at all on the screen, can anyone help here.
my layout file looks like this
<RelativeLayout>
<image></image>
<ListView></ListView>
<RelativeLayout>
<ImageButton></ImageButton>
</RelativeLayout>
</RelativeLayout>
In a RelativeLayout it matters in which order you add the child views (since the latter can be bound to the ones added previously).
I think this is the problem in your layout, you have a fill_parent/match_parent high view already inside (the ListView), and there is no room for the footer.
You should change the order of your views inside the RelativeLayout:
first you should add the header view
(and bind it to the top: android:align_parent_top="true"), then
the footer with buttons (and bind it
to the bottom: android:align_parent_bottom="true"), and
the ListView which should fill up
the empty space will go in as the
thirds view, with
android:layout_below="header_view"
and
adroid:layout_above="footer_view"`
Your layout would then look like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
<image android:id="#+id/header"
android:layout_alignParentTop="true"></image>
<RelativeLayout android:id="#+id/footer"
android:layout_alignParentBottom="true">
<ImageButton></ImageButton>
</RelativeLayout>
<ListView
android:layout_below="#id/header"
android:layout_above="#id/footer"></ListView>
</RelativeLayout>

Categories

Resources