I am displaying integer data from an SQLite database using a SimpleCursorAdaptor.
Everything shows up but the alignment is all wrong:
The dialog looks like this:
<?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">
<ListView android:id="#+id/lvwScores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btnOK "
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK" android:layout_below="#id/lvwScores">
</Button>
</RelativeLayout>
With the row xml file:
<?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">
<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0,1,2,3">
<TableRow >
<TextView android:id="#+id/tvwPlayer1Score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/>
<TextView android:id="#+id/tvwPlayer2Score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/>
<TextView android:id="#+id/tvwPlayer3Score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/>
<TextView android:id="#+id/tvwPlayer4Score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/>
</TableRow>
</TableLayout>
</RelativeLayout>
TableLayout is a bad choice. It's inherent fluidity will cause the columns to vary in width based on the content inside of them (although the stretching does minimize some of this), which you have no control over (see below). Also, the namespace declaration only needs to be on the root element of the XML, not each one ;)
Simplify your row layout drastically by using this instead:
<?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">
<TextView android:id="#+id/tvwPlayer1Score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
<TextView android:id="#+id/tvwPlayer2Score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
<TextView android:id="#+id/tvwPlayer3Score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
<TextView android:id="#+id/tvwPlayer4Score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"/>
</LinearLayout>
The combination of layout_width="fill_parent" and layout_weight="1" on each element tells the system to lay out all four elements, equally spaced (since they have the same weight sum) to fill the row. I almost always use nested LinearLayout in place of TableLayout whenever possible (that's all TableLayout really is anyway).
Another thing from the row XML you posted: it's not a good idea to set the root element of a list item's layout with layout_height=fill_parent like you have in the RelativeLayout tag. Depending on where this layout get's drawn, the layout manager might actually listen to you and one row might end up taking the entire window!
NOTE ABOUT TABLELAYOUT PARAMS:
If you insist on sticking with TableLayout, know that you can (and should) omit all the layout_width and layout_height attributes from every child of TableLayout and TableRow. Those two widgets ignore what you say and set the values of their children to MATCH_PARENT and WRAP_CONTENT (respectively), so adding them to your code will only serve to confuse you if you think they're supposed to take effect.
Hope that Helps!
you should specify the android:gravity attribute:
<TextView android:gravity="right" />
more about this: Android TextView
Update:
I've modified a bit the row.xml layout.
changed the TextViews' width to
fill_parent (they are stretched
anyway, so it shouldn't do any harm),
and
added some attributes to the
TableRow tag
So it looks like:
[...]
<TableRow android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="match_parent">
<TextView android:id="#+id/tvwPlayer1Score"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="right" />
<TextView android:id="#+id/tvwPlayer2Score"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="right" />
<TextView android:id="#+id/tvwPlayer3Score"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="right" />
<TextView android:id="#+id/tvwPlayer4Score"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="right" android:layout_margin="2dp" />
</TableRow>
[...]
And the output looks right now:
Please let me know if this helped (still very embarrassed...)
Related
I would like to obtain this layout for an Android app for mobile phones:
Icon - Object1
List with entries related to Object1
Icon - Object2
List with entries related to Object2
So far I have used the following layout tree (edited graphically with the editor in Android Studio):
Root-LinearLayout
Vertical LinearLayout
Horizontal LinearLayout with icon and text
ListView
Vertical LinearLayout
Horizontal LinearLayout with icon and text
ListView
May be this is not the best way to organize such layout (may be I should use lists with header, but suggestions very welcome), however it can be a good case for understanding deeper how ListView works.
This is the graphical layout generated:
the blue row corresponds to the first LinearLayout. As you can see from the second screenshot that follows, the second list goes all the way down to Hell, bringing me with her. Is there any way to make the lists respect the wrap_content+ weight behaviour?
The XML code follows. I have tried several combos (both reasonable and unreasonable) of layout:weights but none works. I also tried to set the min-width of the first LinearLayout (the hidden one), but nothing changes.
Could you please help me?
<?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:weightSum="2">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView15"
android:src="#drawable/abc_ic_menu_share_mtrl_alpha" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Object1"
android:id="#+id/textView24"
android:textSize="26dp"
android:paddingLeft="10dp" />
</LinearLayout>
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView2"
android:layout_weight="1" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView16"
android:src="#drawable/abc_ic_commit_search_api_mtrl_alpha" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Object2"
android:id="#+id/textView25"
android:textSize="26dp"
android:paddingLeft="10dp" />
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_weight="1" />
</LinearLayout>
It should work if you put your ListViews inside of the child LinearLayouts which hold the LinearLayout that has the TextView and ImageView. You also should be using "0dp" for the height when using weight with a vertical layout.
Something like this, I believe, should work
<?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:weightSum="2">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center_vertical"
android:minHeight="50dp"
android:layout_weight=".2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView15"
android:src="#drawable/abc_ic_menu_share_mtrl_alpha" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Object1"
android:id="#+id/textView24"
android:textSize="26dp"
android:paddingLeft="10dp" />
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="#+id/listView2"
android:layout_weight=".8" />
</LinearLayout>
Note the other changes: I gave the inner-LinearLayout an arbitrary weight of ".2" then the ListView a weight of ".8". And, of course, set the height to "0dp". You may need to play with those weights a bit but I think doing something like that for both first child LinearLayouts should get you close.
That may get your current layout to work but using headers and/or an ExpandableListView may be a better option.
I have a LinearLayout with a custom button and then another LinearLayout with two TextViews, which is always partly cut off on the edge of the screen. I have tried tons of different combinations of fill_parents, match_parents, and wrap_contents, as well as trying to add layout_weights, but nothing solved it, nor did any answer to any similar questions on this site. Here is my XML code, thanks for any help!
<?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="horizontal" >
<com.goldtrimdevelopment.sites.google.com.interesthub.SquareButton
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="#+id/list_article_like_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#drawable/custom_like_button"
android:contentDescription="#string/like_button"
android:padding="5dip" />
</com.goldtrimdevelopment.sites.google.com.interesthub.SquareButton>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textSize="16sp" />
<TextView
android:id="#+id/article_link"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:singleLine="true"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
I figured from your xml file that you want the layout to be a custom button and to its right 2 textviews which are vertically linear, while the buttons and the textviews are horizontall linear. Try the following values in your layout. I think it must solve the issue.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" or "wrap content"
android:orientation="horizontal" >
and the next linear layout as
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
and for both textviews set
android:layout_width="150dp"
try it and let me know.
I have a ListView which uses a custom adapter to display rows of data. Each row contains two TextViews; one is left-justified and one is right-justified.
Ideally what I want is for each TextView to take up at most half of the width of the row. I've accomplished this using the following row xml layout:
<?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" >
<TextView
android:id="#+id/description"
style="#style/ProductSpecNameFont"
android:layout_width="fill_parent"
android:layout_gravity="left"
android:layout_weight="1"
android:padding="#dimen/margin_sides"
android:text="Display Size" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TextView
android:id="#+id/value"
style="#style/ProductSpecValueFont"
android:layout_alignParentRight="true"
android:padding="#dimen/margin_sides"
android:text="3.5 Inches" />
</RelativeLayout>
This "almost" works perfectly. The problem is that the rows which contain text on the right-side TextView that takes up multiple lines are not right-justified. The single-line rows are right-justified as I would like. Here is a screenshot of what I'm talking about:
Why aren't the multiple-line TextViews right-justified, and how can I make them so?
Try setting the gravity of the TextView. It looks like what's happening is the TextView is aligned to the right, but the text itself is left justified.
android:gravity="right"
Try using android:weightSum on your LinearLayout. Like this:
<?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:weightSum="1.0">
<TextView
android:id="#+id/description"
style="#style/ProductSpecNameFont"
android:layout_width="fill_parent"
android:layout_gravity="left"
android:layout_weight="0.5"
android:padding="#dimen/margin_sides"
android:text="Display Size" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5" >
<TextView
android:id="#+id/value"
style="#style/ProductSpecValueFont"
android:layout_alignParentRight="true"
android:padding="#dimen/margin_sides"
android:text="3.5 Inches" />
</RelativeLayout>
I am trying to create a UI using relative layout where in the UI should look as such:
In the top of the screen i have a TextView
followed by another TextView
followed by scroll view where in i have to display the text in the
scrollable format
in the end i have a imageview.
How can i achieve this.
I have tried using the combination of Relative and Linear Layout but the elements gets overlapped. Anyone have any suggestions on how to proceed?
<?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="#drawable/background_main" android:id="#+id/g_description">
<TextView android:id="#+id/titleBar" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#drawable/header"
android:layout_alignParentTop="true" android:gravity="center"
android:text="#string/aboutus" android:textAppearance="? android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView android:id="#+id/wd_name"
android:layout_width="fill_parent" android:layout_below="#id/titleBar"
android:layout_height="wrap_content" android:textStyle="bold"
android:textColor="#FFFFFF" android:textSize="18dip"
android:background="#drawable/background_main" android:gravity="center" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrolltext" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#id/wd_name"
android:padding="5dip">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wd_description"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="14dip" android:textColor="#FFFFFF">
</TextView>
</LinearLayout>
</ScrollView>
<ImageView android:id="#+id/bottomBar" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:src="#drawable/bottom_bar"
android:layout_alignParentBottom="true" />
you can achieve it like this:
Take relative layout and put textview in it.
Take another textview and add this to it android:layout_below="id of the 1st textview".
Take scrollview and add this to it android:layout_below="id of the 2nd textview".
Take linear layout inside scrollview.
Put textview inside linear layout.
Take imageview and add this to it android:layout_below="id of the scrollview".
Now your layout is ready and if you get any error then let me know with your code....
If you have a series of elements that you want to flow down the screen, then it sounds like you just need a LinearLayout.
It goes as below. Placing a TextView inside scrollview will make it scrollable. Reset is selfexplanary.
<?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">
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Below TOP TextView" android:id="#+id/TextView01" android:layout_alignParentTop="true"></TextView>
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="TOP TextView" android:id="#+id/TextView02" android:layout_below="#id/TextView01"></TextView>
<ScrollView android:layout_height="wrap_content" android:layout_width="fill_parent" android:fillViewport="true" android:layout_below="#id/TextView02">
<TextView android:id="#+id/textView1" android:layout_height="wrap_content" android:text="Scrollable textViewafkasjf;laksjflaskjf;lasjf;alsfjal;sfj;aslfj;aslfja;slfj;aslkfj;asldfj;aslkfjasl;dfj;a asfj aslfj asldfjk as;lfjk asfjka;sldf jl;asj df;asdfjasfj aslfj asljf asjfl;asfasj fasj f;asj flas jf;lasjf;asjkf;las f;asj df;as jf;lasjf ;asjf;asjf;asjf;asjf;asjf;jasf;asfja s;dfa;sf asf jf asf a; sldfja;slfj;asldfjk" android:layout_width="fill_parent"></TextView>
</ScrollView>
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:id="#+id/imageView1" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_width="fill_parent"></ImageView>
</RelativeLayout>
It works fine with setting android:orientation="vertical" for each text view.
the following Android layout is a custom row for my listactivity. The second textview in the tablerow does not stretch to fit the column like I want it to. Note that there is only one tablerow because I took the others out for brevity.
<?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="wrap_content"
android:padding="6dip">
<ImageView
android:id="#+id/DocTypeIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:layout_gravity="center_vertical" />
<TableLayout
android:id="#+id/tableLayout1"
android:layout_toRightOf="#id/DocTypeIcon"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:stretchColumns="1">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pages"
android:textStyle="bold"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/DynamicTextHere"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</TableRow>
<!-- Other rows stripped for brevity -->
</TableLayout>
</RelativeLayout>
When I put stretchColumns="1" in, I see nothing but a couple of artifacts on the screen.
When stretchColumns is not there the layout only goes to a fixed width. I believe it might be the RelativeLayout screwing me up. However, that is more of a drastic change than I want. Can I do something small to make this row fit to my screen (both portrait and landscape)?
Can you try: android:stretchColumns="*"