Decision: ListView or ScrollView - android

I need an Android Activity, which should show a field like a headline with an image and several dynamic generated items (1 to 100 I think) below it.
If I would not want the headsection to scroll, I would use a LinearLayout and put the headsection layout in it. Below this, I would add a ListView for scrolling the items, but I want the headsection to be scrolled to, as a top of the list.
Should I just put the stuff in a ScrollView or is there a better idea?

should show a field like a headline with an image and several dynamic generated items (1 to 100 I think) - use a ListView as it can recycle views (efficiency reason). Also, it's easier to change and maintain a list adapter than a complex UI structure.
If I would not want the headsection to scroll, I would use a LinearLayout and put the headsection layout in it. - Why not use a RelativeLayout that has the header on top and tha list occupies the rest of the height. This way you have the expected result.
but I want the headsection to be scrolled to, as a top of the list - then set the list header, or use different views in your listview and make your first item look different. More on this topic - search android listview different views in Google.
Either way to put it - use a ListView!

Have a ScrollView with a LinearLayout as it's child, and put your stuff into the LinearLayout. Remember ScrollViews can only have one child layout. That should work fine. Something like this:
ScrollView android:id="#+id/ScrollView02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/aboutFormLinearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<TextView android:id="#+id/aboutFormVersionDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:singleLine="true"
android:text ="Version: "
android:textSize="15sp"
android:textColor="#000000"/>
<TextView android:id="#+id/aboutFormVersion"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:singleLine="false"
android:textSize="20sp"
android:textColor = "#000000"
android:layout_marginBottom ="20sp"/>
<TextView android:id="#+id/aboutFormCompanyDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:singleLine="true"
android:textSize="15sp"
android:text ="Company: "
android:textColor="#000000"/>
<TextView android:id="#+id/aboutFormCompany"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:singleLine="false"
android:textSize="20sp"
android:textColor = "#000000"
android:layout_marginBottom ="20sp"
/>
</LinearLayout>

Related

How to change the property of first content of List View in Android

I have created an expandable listView where I am showing text side by side more likely I wanna show a table of 2 columns there. For that, I have used the expandable list view and so far it looks like this(The image below).
But I want to change the color of the first line of this list. something like this(image below).
As for code, I have used two text views in a horizontal Linear Layout and with a custom adapter I am showing the data in the view. here's the XML.
<?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:orientation="horizontal">
<TextView
android:id="#+id/itemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:textSize="16sp"
android:text="item"/>
<TextView
android:id="#+id/itemText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:textSize="16sp"
android:text="Item2"/>
</LinearLayout>
Can anyone suggest what should I do to achieve this?
In your adapter if position = 0
itemText.setBackgroundColor(int colour);
Also you should use RecyclerView instead of ListView

set the Columns of a TableRow in a TableLayout

well basicly I'm getting data from a database and I want to adapt this data in a kind of "DataGridView" on any other lenguaje, Basicly I have an GridView in a Main layout define like this:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gastoGridView"
android:stretchMode="columnWidth"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="1"
android:clipChildren="true"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip" />
other hand I have a second xml layout file than define every item(row) for this gridView, It is my item_gridview xml file:
<?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">
<TableLayout android:id="#+id/TableLayout01"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TableRow android:id="#+id/TableRow01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<TextView android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fechaAtt"
android:gravity="left"
android:layout_gravity="left" />
<TextView android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/descpAtt"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal" />
<TextView android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/saldoAtt"
android:layout_gravity="right" />
</TableRow>
</TableLayout>
Ok, basicly I'm getting 3 attributes from database, I already developed the adapter to this gridview, so I show the information on it, but not in the way I want, I'll try to explain you, Currently it's being show like this
| Date | Description | Price |
2014-11-17 any description it have 50.85$
I'm trying to divide my tablerow (width:fill_parent) on 3 sections(columns) I'm not sure if It's possible, because I'm not very involved on this subject, but I want to divide this tablerow on those 3 section, I want a small section on the left side of the row which will be my date, a large section in the center_horizontal wich will be my Description, and another left section wich will be my price, I'm not sure if you guys get my point but I want some like this.
| Date | Description | Price |
2014-11-17 Get the description Centered Here 50.85$
I've tried to use the layout_span and layout_column on every TextView, but I get a Null Pointer error which I don't understand, maybe I'm doing that in a wrong way.
Could you guys help me to get this style? I've been reading about it a lot, It's a kind of difficult because Android do not support an DataGridView tool as others lenguages do.
Thanks you beforehand, I really need it
Your difficulty stems from the fact that your trying to bring your concept of the DataGridView into Android which is problematic. What you really want to do use a ListView with a proper Adapter and Loader (use a Loader if possible).
Now, with a ListView what happens is it creates View for every row returned from the Cursor using the Adapter to create (inflate) this view and populate it (binding). This is useful since you can now think about each row as a set of three items and lay them out appropriately. I recommend just using the regular LinearLayout with the appropriate layout_weight set for your layout. You'll have to remember to set the LinearLayout to horizontal.
Edit:
For clarification. With LinearLayout you can specify in the layout.xml file the android:layout_weight parameter. This allows you to set 'relative' sizes (width or height depending on horizontal or vertical LinearLayout). Once you do this the android:layout_width is ignored but you should set it to 0dp. An Example:
<LinearLayout
android:id="#+id/row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="#+id/description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="#+id/price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
Now you have three TextView in a horizontal LinearLayout each with a weight set to 1. That would make them all equal size. You can adjust the weight values to change their relative sizes to each other and the parent width.

What is the correct layout for this structure?

What is the structure of such a layout? The text in the left column aligned to the right, the text of right column aligned to the left. Also rows are separated with a line. Thank you for hints!
You could make a ListView of your own items. That would give you a separator between all rows.
To set your own layout for ListView's item, you have to prepare your layout's xml file and set it like this:
yourListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.layout_file_for_your_item, items));
Item's layout (LinearLayout) could consist of 2 LinearLayouts. In first one (left) you'd have TextView that would be aligned right in parent. Second one (right) would have TextView aligned left in parent and margin-left set to some specific value to make a gap between two layouts.
Blue: ListView
Red: ListView's item (LinearLayout)
Green: First LinearLayout
Yellow: Second LinearLayout
You can try create listview field in your layout (with empty inside, not textviews or images) and then create another layout something like that:
Main 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"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla bla" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla bla 2" />
<!-- listview with populated items -->
<ListView
android:id="#+id/mylist"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Populated items:
<TextView
android:id="#+id/itemleft"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:textStyle="bold"
android:gravity="right|center_vertical"
android:text="Location" />
<TextView
android:id="#+id/itemright"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:text="Los Angeles, US" />
</LinearLayout>
Here is a preview of this settings:
There is of course only one item but it will be populated then. It's just a preview.
(Populated items will be used in listView later) Then try use for example base adapter implementation in you code, check here example: link. I hope you will understand it, however using custom listview wasn't the problem in this title :)

Adding elements to a ListView expands it below other elements using RelativeLayout

In my activity i hava a ListView which has an ArrayAdapter as its adapter, and is updated dynamically. I am using RelativeLayout. At first, the ListView is empty, but I can see that it takes up space on the screen compared to before i added it. As soon as one element is added, it is expanded and shows this element. As soon as I add a second element, I only see the top of the second element and then it goes behind the message text element under. How can I solve this?
<Button
android:id="#+id/btnAddAttachment"
android:layout_marginTop="7dp"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="#id/sprType"
android:text="Add attachment"/>
<!--ATTACHMENTS LIST-->
<ListView
android:id="#+id/lstAttachments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btnAddAttachment">
</ListView>
<!-- MESSAGE TEXT -->
<EditText
android:id="#+id/txtMessage"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="#id/lstAttachments"
android:layout_marginTop="7dp"
android:lines="10"
android:maxLines="10"
android:maxLength="500"
android:hint="Message text"
android:gravity="top|left"/>
What is the android:layout_height value of the RelativeLayout?
Try changing the ListView to have:
android:layout_height="fill_parent"
and do the same for the RelativeLayout that contains it.
You can then align the EditText below it to the bottom of the ListView, and make sure the list view is layout_above that EditText

TableRow in ListView Item

I have a ListView which each row of which has the following
1. TextView
2. Button
The TextViews can contain text of multiple lengths. So the list appears in an awkward way. I thought the way out will be to set each List item to a Table Row with 2 cells(TextView and Button) . Is there a way to do this? Or may be there is a better solution to this?
You could use Relative Layout to display the row. In the Relative Layout put a button on the right with align parent right as true, and then set the TextView to the left of the Button to match parent. Also you could specify the TextView to be single line and ellipsize as true at end. This is just an example
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:singleLine="true"
android:ellipsize="end"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toLeftOf="#+id/button1"
android:text="This is your textView with a very long text. I hope this code serves your problem" />
</RelativeLayout>
You could also use 2 TextViews, that totally depends upon the text that you want to display.
LinearLayout and it's weight attribute. More in official docs http://developer.android.com/guide/topics/ui/layout/linear.html

Categories

Resources