layout_below item inside included layout - android

I'm trying to figure out a way to align an item in a layout in respect to an item within an included layout.
Here's a visual representation:
And here is example code that I'm looking for (which obviously doesn't work):
Main.xml
<?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" >
<include
android:id="#+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/info" />
//This would be the dark grey box
<RelativeLayout
android:layout_below="#id/item1">
</RelativeLayout>
</RelativeLayout>
included.xml
<?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="50dp">
<ImageView
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingLeft="100dp"/>
</RelativeLayout>
I'm assuming the best way to accomplish this would be through dynamically positioning the dark grey box in code, but I have no idea where to start. Any help would be awesome.

Can you make this below your include? Something like this (instead item1 use info):
<?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" >
<include
android:id="#+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/info" />
//This would be the dark grey box
<RelativeLayout
android:layout_below="#id/info">
</RelativeLayout>
</RelativeLayout>

Related

ListView Selector on Specific Element Within ListView View

I'm having a problem where I want each subchild of a view for a listview to have it's own custom selector. There are a few solutions already but none of them seem to work at all for me. Here's my xml for the listview and individual view. Does anyone know specifically what I need to do? Right now If I can get show_group_actions and action_calendar to have separate selectors working I'd be quite happy...
<?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="wrap_content"
android:descendantFocusability="afterDescendants"
android:orientation="vertical">
<LinearLayout
android:id="#+id/action_show_group_actions"
style="#style/AppTheme.NavigationDrawer.Row"
android:paddingRight="0dp"
>
<TextView
android:id="#+id/group_name"
style="#style/AppTheme.NavigationDrawer.Row.TextView"
android:text="#string/label_my_groups"
/>
<RelativeLayout
style="#style/AppTheme.NavigationDrawer.Group.OpenClose"
>
<me.magneto.groups.views.IconView
android:id="#+id/action_show_group_actions_arrow"
style="#style/AppTheme.NavigationDrawer.Group.OpenClose.Arrow"
android:text="#string/icon_chevron_down"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/action_calendar"
style="#style/AppTheme.NavigationDrawer.Group.Row"
>
<me.magneto.groups.views.IconView
style="#style/AppTheme.NavigationDrawer.Group.IconView"
android:text="#string/icon_calendar"
/>
<TextView
style="#style/AppTheme.NavigationDrawer.Row.TextView"
android:text="#string/label_calendar"/>
</LinearLayout>
</LinearLayout>
<ListView
android:id="#+id/navigation_drawer_list_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#null"
android:background="#color/navigation_primary_background"
/>

Android layout_gravity

I can't understand why the following code places my TextView in the top left corner of my screen. Based on my understanding of layout_gravity, it seems that it should place my TextView at the bottom of my screen instead.
Can anyone please shed some light on this for me? This Linear Layout, despite seeming very simplistic, has caused me numerous headaches. How can something that seems so simple, be such a royal pain to grasp?
<?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:orientation="vertical"
android:background="#FF0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_gravity="bottom"
android:background="#FFF"
/>
</LinearLayout>
A LinearLayout set to vertical orientation will ignore all layout_gravity parameters that refers to vertical alignment. So for example "bottom" wont work but "center-horizontal" will work. (The vertical alignment is always done so that each view in the layout i placed under the other views in the layout.)
A relative layout might be the way to go or maybe you can use this code that probably is pretty close to what you want.
<?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:orientation="vertical"
android:background="#FF0000"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:gravity="bottom"
android:layout_weight="1.0"
android:background="#FFF" />
</LinearLayout>
Edit This blog explains things further.
<?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="#FF0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_alignParentBottom="true"
android:background="#FFF"
/>
</RelativeLayout>
you can use relative layout were you can align bottom easily
If that is the simplicity of your layout, you shouldn't wrap it in a LinearLayout at all.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#FFF"
android:text="HELLO HELLO" />
If you need to wrap it though, remove the vertical orientation:
<?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="#FF0000" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#FFF"
android:text="HELLO HELLO" />
</LinearLayout>
I think this code do what you want:
<?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:orientation="vertical"
android:gravity="bottom"
android:background="#FF0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:background="#FFF"
/>
</LinearLayout>
And if you want the textview on center, you can use:
<?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:orientation="vertical"
android:gravity="bottom"
android:background="#FF0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_gravity="center"
android:background="#FFF"
/>
</LinearLayout>
Hope this help you.

Trying to make my entire layout scrollable

I have designed my layout and on smaller phone screens not everything is visible, so I'd like to make it scrollable, could somebody help me with how to go about this? Below is the basic structure of my layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/allowanceroot"
android:background="#drawable/background"
>
--TextView--
--EditText--
--TextView--
--EditText--
--TextView--
--EditText--
</LinearLayout>
Do I just contain my TextViews and EditTexts in a LinearLayout, and then contain that in a ScrollView? Also is there anything I need to do programmatically?
Try wrapping it in a scrollview
https://stackoverflow.com/a/4202608/661079
There is nothing else you need to do
Try like this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView />
<TextView />
</LinearLayout>
</ScrollView>

layout_height always wrapping content

it's just a simple question:
I have a layout file which is presented as an item inside of a ListView. I set the height of the RelativeLayout to 100dp, but it does not work, it's always wrapping the content, no matter which layout I use. What can I do to set a fixed value to the layout height without having it wrapping?
Thanks for the help.
<?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="100dp"> <!-- this attribute seems to be ignored -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="#dimen/progressbar_small"
android:layout_height="#dimen/progressbar_small"
android:layout_gravity="center_vertical" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="#string/loading"
android:layout_gravity="center_vertical"
style="#style/ListItemSubtext" />
</LinearLayout>
</RelativeLayout>
Do like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="100dip">
I think in a list view all items are of the same height. In your case you could add the progress bar in the list footer view. Add a dummy height adjuster right before or after ListItemSubtext. Try using TableLayout instead of Relative and Linear Layout.
A dummy adjuster is like this.
<View android:layout_width="wrap_content" android:layout_height="100dip"/>
Please note you do not need xmlns in every element.
Try just using 100px. Works for me.
i do below changes in your xml..
<?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="100dp"> <!-- this attribute taking a fixed value as u said -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="20dip"
android:layout_height="20dip"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="#string/loading"
android:layout_gravity="center_vertical"
style="#style/ListItemSubtext" />
</LinearLayout>
If this doesn't work for you, say what exactly you want to do.

My ListView backgrounds go black during scrolling - how to fix?

Ive specified white backgrounds for pretty much all widgets and it works, except when scrolling. The background container goes black during scrolling resulting in annoying flickering.
You will need to use the setCacheColorHint() method of ListView.
Example: list.setCacheColorHint(yourBackColor);
Explanation of the issue and solution here
ScrollingCache="false" in your activity's *.xml file for the list object should do the trick, too.
I fixed this problem by making the background color of the cells the same color as the ListView, so:
<?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:orientation="vertical"
android:background="#FFFFFF">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"> <---- Background color of ListView
</ListView>
</LinearLayout>
and then the row:
<?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:padding="8dip"
android:background="#FFFFFF"> <--- Background color of the cell
<LinearLayout
android:id="#+id/projects_cover_row_image_layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:background="#444444"
android:layout_centerInParent="true"
android:layout_marginRight="8dip">
<ImageView
android:id="#+id/projects_cover_row_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="#string/projects_image_content"/>
</LinearLayout>
...
</RelativeLayout>
that completely got rid of the problem for me and seems like the best solution
Just put background on the top and no highlight or other bad effects simple and no need of hint or cache
in listview layout
android:background="#000000" for black background or android:background="#FFFFFF" for white
<?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:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/testscheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:layout_weight="1"
android:layout_gravity="left|center"
android:gravity="left"
android:textColor="#color/ics"
android:textSize="14sp"
/>
</LinearLayout>

Categories

Resources