Multiple TextViews in the same List Item - android

I'm using a ListView with an array adapter to display multiple list items. I'd like to have multiple TextView in the list item as per the picture
I'm currently using a linear layout which doesn't seem to be suitable... Would a relative layout help me achieve this?
Example code or tutorial link would be cool
Cheers
ListView 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="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewTest"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
ArrayAdapter xml
<?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" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="89dp"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:layout_weight="0.34"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="0.97"
android:gravity="center"
android:text="#+id/label"
android:textSize="30dp" >
</TextView>
</LinearLayout>
I'm trying to add another textView under the existing textView using graphical layout tab in eclipse but my noobieness is getting in the way

This link uses Multiple Texts in a Single Listview...
http://publicstaticdroidmain.com/2011/12/building-a-custom-multi-line-listview-in-android/
Use the Code as follow..
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Custom ListView Example" />
<ListView
android:id="#+id/srListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
and,
<?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:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#C20000"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/cityState"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

Follow the Link. It will Help you..
android Multiple selection ListView & Textview
And
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Main.xml for List action
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="785dp"
android:choiceMode="multipleChoice" >
</ListView>
</LinearLayout>
And Row.xml as,..
<?xml version="1.0" encoding="utf-8"?>
<TableRow>
<TextView
android:id="#+id/StudNo"
android:layout_width="50px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5px" />
</TableRow>
<TableRow>
<TextView
android:id="#+id/StudNo"
android:layout_width="50px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5px" />
</TableRow>
</TableLayout>
and try with Customized Codes..

The linked picture has only two TextViews one below another. In case your using only 2 TextViews then a vertical LinearLayout should solve the purpose. However, if you intent to use more than two Views inside your ListItem then I would recommend you to use Relative Layout as it is way more flexible in holding multiple child-views, when it comes to aligning them with respect to one another.
You can follow a very simple tutorial provided by Google here:
Relative Layout

Make xml layout for list item which contains two textview
**listItem.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
///*now in set this layout in your listview using Adapter as following*/
mlistview.setAdapter(new ArrayAdapter<String>(this,
R.layout.spinneritem, StringArray) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
if (v == null) {
if (DiscountActivity.this != null) {
LayoutInflater vi = (LayoutInflater) DiscountActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listItem, null);
}
}
TextView txtview1 = (TextView) v.findViewById(R.id.textview1);
TextView txtview2 = (TextView) v.findViewById(R.id.textview2);
txtview1.setText("textview1");
txtview2.setText("textview2");
v.setTag(campaign);
return v;
}
});

Related

Textview`s LinkMovementMethod blocks listitem touch event

I have some problem with Listview. Listview has list items that consist of imageview and textview. Textview contains clickable links. When I set LinkMovementMethod to textviews, listitems doesn`t receive onclick event. I still cannot find any right solution=(. Can anyone explane how to solve it or give an example of using textview with clickable links inside listitem? Thanks.
Here is textview creation in adapter:
TextView text = (TextView) ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.post_content_text, null);
SpannableString sString = new SpannableString(d.getText());
//I have my own class UrlSpan that contain fields startPosition, endPosition and urlLink
for (UrlSpan us : ((TextData) d).getUrls())
{
URLSpan urlSpan = new URLSpan(us.getUrl());
sString.setSpan(urlSpan, us.getStart(), us.getEnd(), SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
}
text.setText(sString);
text.setMovementMethod(LinkMovementMethod.getInstance());
view.addView(text);
and list item is:
<?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:visibility="visible" >
<LinearLayout
style="?PostListRow"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="65dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/user_image"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_margin="3dp"
android:clickable="true"
android:src="#drawable/ic_no_photo" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_margin="0dp"
android:orientation="vertical" >
<TextView
android:id="#+id/user_title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:clickable="true"
android:textSize="#dimen/title_in_list" />
<TextView
android:id="#+id/post_datetime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textSize="#dimen/text_size_small" />
</LinearLayout>
<TextView
android:id="#+id/post_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="123" />
</LinearLayout>
<!-- it is a view where textview with links is to be added to -->
<LinearLayout
android:id="#+id/post_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="6" >
</LinearLayout>
<TextView
android:id="#+id/post_view"
style="?PostText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="gone" />
<LinearLayout
android:id="#+id/topic_edit_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
try to add this attribute
android:descendantFocusability="beforeDescendants"
to your listview
from the documentation:
it defines the relationship between the ViewGroup and its descendants when looking for a View to take focus. it seems to me that when you have some active views like buttons or edittext and etc in your listview you have to define descendantFocusability

Android - can't get scroll view to scroll the list correctly

I have this pretty basic view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/question_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your question: "
/>
<TextView
android:id="#+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<EditText
android:id="#+id/question_text"
android:layout_height="wrap_content"
android:hint="#string/question_comment_hint"
android:inputType="textMultiLine"
android:lines="4"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="#+id/submit"
android:layout_height="wrap_content"
android:text="#string/submit"
android:onClick="sendFeedback"
android:layout_width="fill_parent">
</Button>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/please_wait"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please wait while the discussion loads..."
/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="20px" >
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
What I was hoping to do with it is to allow the user to scroll through the entire list of comments. As of now, when there are more comments then space on the screen, the screen does not scroll down.
Any idea what I should change to enable scrolling of the comments?
Thanks!
I would do away with the scrollview completely - based on what I THINK you're try to accomplish, since the listview will handle scrolling on its own. Like such:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/question_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your question: "
/>
<TextView
android:id="#+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<EditText
android:id="#+id/question_text"
android:layout_height="wrap_content"
android:hint="#string/question_comment_hint"
android:inputType="textMultiLine"
android:lines="4"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="#+id/submit"
android:layout_height="wrap_content"
android:text="#string/submit"
android:onClick="sendFeedback"
android:layout_width="fill_parent">
</Button>
<TextView
android:id="#+id/please_wait"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please wait while the discussion loads..."
/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="20px" >
</ListView>
The difference now, is that the TextView with id "please_wait" will not scroll with the list. If you need it to scroll with the list, I would recommend using a dynamically built TableLayout in java for your list, then add that and your textview to a ScrollView. The TableLayout doesn't scroll on its own, and thats why its a better solution than using a ListView inside a ScrollView. If you want to do it this way, let me know and I will post some example code.
Seems like you're trying to add a header above your comments in the ListView. As coder noted, you shouldn't put a ListView in a ScrollView. You should look at adding a header view directly to your ListView (see addHeaderView).
basicly you can do like this:
get rid of scrollview and loading textview.
set a simple adaptor to your listview like this:
fList = (ListView)findViewById(R.id.lstview);
fList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"Please wait while the discussion loads..."}));
Than get your comments and set to your listview.
list.setAdapter(adapter);
You should not have a ListView inside a ScrollView.
inflate a LinearLayout(vertical) 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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/question_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your question: " />
<TextView
android:id="#+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
<EditText
android:id="#+id/question_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/question_comment_hint"
android:inputType="textMultiLine"
android:lines="4" >
</EditText>
<Button
android:id="#+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="sendFeedback"
android:text="#string/submit" >
</Button>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/please_wait"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please wait while the discussion loads..." />
<LinearLayout
android:id="#+id/comments_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="invisible" >
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
In your code:
LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.comments_layout);
To populate the LinearLayout:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i = 0; i < comments.size(); i++){
View childView = inflater.inflate(R.layout.comment_list_item, commentsLayout, false);
TextView comment = (TextView) childView.findViewById(R.id.comment);
comment.setText("Something");
categoriesLayout.addView(childView, i);
}
Something like that, hope this helps.

Horizontal bar in Android list activity

In my Android project, I want a horizontal bar in my list activity. How can I add this through layout?
You can add headerView in your ListView.
Add a layout in the XML as header layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/header_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:text="#string/header_text_1"
android:id="#+id/footer_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip">
</TextView>
</LinearLayout>
</LinearLayout>
Also in code:
View headerView = ((LayoutInflater)ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.header_layout, null, false);
ListView.addHeaderView(headerView);
Is this maybe what you're looking for?
http://jnastase.alner.net/archive/2011/01/08/ldquomaster-pagesrdquo-in-android.aspx

Checkbox state problem in ListView

Hello with regard to the known issue of unwanted checkboxes being checked while scrolling through a listview, can anyone please give me an idea how to use a checkedTextView properly so that i can maintain check state properly when scrolling through the list. I thought the listview takes care of maintaining the checkbox state when using a checkedtextView but that does not seem to be the case. or do i have to use the default Id for the checkedTextView. I can't seem to find an example that uses a cursorAdapter or a SimpleCursorAdapter for this case. Thank you
i have tried using this, but am not really sure what to do with the set tag position. here is a small piece of code:
//bindView() method in SimpleCursorAdapter
//viewHolder holder;
//Cursor c;
holder.checkedText = (CheckedTextView)view.findViewById(R.id.view_checked);
holder.checkedText.setTag(c.getPosition());
holder.checkedText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
v.getTag();
((CheckedTextView) v).toggle();
}
});
my xml standard layout:
<?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">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="multipleChoice"/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_notes"/>
</LinearLayout>
my custom layout:
<?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="horizontal"
android:padding="6dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/viewNameText"
android:id="#+id/viewNameId">
</TextView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:padding="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start Date"
android:layout_gravity="right"
android:id="#+id/DateId">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start Time"
android:layout_gravity="right"
android:id="#+id/TimeId">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
>
<CheckedTextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
</LinearLayout>
</LinearLayout>
I thought the listview takes care of maintaining the checkbox state when using a checkedtextView but that does not seem to be the case.
It should, if your ListView is android:choiceMode="multiple".
do i have to use the default Id for the checkedTextView
You might. The only times I have implemented a multiple choice ListView, I have used the standard row layout rather than a custom one.

Android: Adding static header to the top of a ListActivity

Currently I have a class that is extending the ListActivity class. I need to be able to add a few static buttons above the list that are always visible. I've attempted to grab the ListView using getListView() from within the class. Then I used addHeaderView(View) to add a small layout to the top of the screen.
Header.xml
<?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" >
<Button
android:id="#+id/testButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Income"
android:textSize="15dip"
android:layout_weight="1" />
</LinearLayout>
Before I set the adapter I do:
ListView lv = getListView();
lv.addHeaderView(findViewById(R.layout.header));
This results in nothing happening to the ListView except for it being populated from my database. No buttons appear above it.
Another approach I tried as adding padding to the top of the ListView. When I did this it successfully moved down, however, if I added any above it, it pushed the ListView over. No matter what I do it seems as though I cannot put a few buttons above the ListView when I used the ListActivity.
synic, I tried your suggestion previously. I tried it again just for the sake of sanity, and the button did not display. Below is the layout file for the activity and the code I've implemented in the oncreate().
//My listactivity I am trying to add the header to
public class AuditActivity extends ListActivity {
Budget budget;
#Override
public void onCreate(Bundle savedInstanceState) {
Cursor test;
super.onCreate(savedInstanceState);
setContentView(R.layout.audit);
ListView lv = getListView();
LayoutInflater infalter = getLayoutInflater();
ViewGroup header = (ViewGroup) infalter.inflate(R.layout.header, lv, false);
lv.addHeaderView(header);
budget = new Budget(this);
/*
try {
test = budget.getTransactions();
showEvents(test);
} finally {
}
*/
// switchTabSpecial();
}
Layout.xml for activity:
<?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">
<ListView android:id="#android:id/list" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/empty" />
</LinearLayout>
findViewById() only works to find subviews of the object View. It will not work on a layout id.
You'll have to use layout inflater to convert the xml to it's corresponding View components. Something like this:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
I'm not sure why your code wasn't just throwing an error. findViewById() was probably just returning null, and so no header was added to your list.
Here is the simpliest sollution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background">
<include layout="#layout/actionbar"/>
<ListView
android:id="#+id/tasklist_TaskListView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textColor="#color/baseFont"/>
<include layout="#layout/bottombar"/>
</LinearLayout>
or
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/tasklist_TaskListView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textColor="#color/baseFont"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
instead of button you can add another horizontal linear layout
After some research I was able to figure out that mixing TableLayout and LinearLayout within my ListActivity XML document I was able to add a header to the document. Below is my XML document if anyone is interested to see it. While synic's approach is probably the right approach after work with his solution for sometime I was unable to make it function the way I wanted it.
AuditTab.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audittab);
getListView().setEmptyView(findViewById(R.id.empty));
}
audittab.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/btnFromDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="1" />
<Button
android:id="#+id/btnToDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_toRightOf="#+id/btnFromDate"
android:layout_weight="1" />
<Button
android:id="#+id/btnQuery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Query"
android:layout_toRightOf="#+id/btnToDate"
android:layout_weight="1" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#android:id/list"
android:layout_width="300dip"
android:layout_height="330dip"
android:scrollbars="none" />
<TextView
android:id="#+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:text="- Please select a date range and press query." />
</LinearLayout>
</TableRow>
</TableLayout>
AuditItem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:padding="10sp">
<TextView
android:id="#+id/transactionDateLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date: " />
<TextView
android:id="#+id/transactionDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/transactionDateLabel" />
<TextView
android:id="#+id/transactionTypeLabel"
android:layout_below="#id/transactionDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type: " />
<TextView
android:id="#+id/transactionType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_below="#id/transactionDate"
android:layout_toRightOf="#id/transactionTypeLabel" />
<TextView
android:id="#+id/transactionAmountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="Amount: "
android:layout_below="#id/transactionDate"
android:layout_toRightOf="#id/transactionType" />
<TextView
android:id="#+id/transactionAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/transactionDate"
android:layout_toRightOf="#id/transactionAmountLabel" />
<TextView
android:id="#+id/transactionCategoryLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category: "
android:layout_below="#id/transactionAmountLabel" />
<TextView
android:id="#+id/transactionCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/transactionAmountLabel"
android:layout_toRightOf="#id/transactionCategoryLabel"
/>
<TextView
android:id="#+id/transactionToAccountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To Account: "
android:layout_below="#id/transactionCategoryLabel" />
<TextView
android:id="#+id/transactionToAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/transactionCategoryLabel"
android:layout_toRightOf="#id/transactionToAccountLabel" />
<TextView
android:id="#+id/transactionFromAccountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From Account: "
android:layout_below="#id/transactionToAccountLabel" />
<TextView
android:id="#+id/transactionFromAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/transactionToAccountLabel"
android:layout_toRightOf="#id/transactionFromAccountLabel" />
<TextView
android:id="#+id/transactionNoteLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Note: "
android:layout_below="#id/transactionFromAccountLabel" />
<TextView
android:id="#+id/transactionNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/transactionFromAccountLabel"
android:layout_toRightOf="#id/transactionNoteLabel" />
<Button
android:id="#+id/editTransactionBtn"
android:layout_width="wrap_content"
android:layout_height="40sp"
android:visibility="gone"
android:text="Edit"
android:layout_below="#id/transactionNoteLabel"/>
<Button
android:id="#+id/deleteTransactionBtn"
android:layout_width="wrap_content"
android:layout_height="40sp"
android:text="Delete"
android:layout_below="#+id/transactionNoteLabel"
android:visibility="gone"
android:layout_toRightOf="#+id/editTransactionBtn"
android:ellipsize="end"/>
</RelativeLayout>
The ListView answer above is useful but scrolls with the list and doesn't keep the header graphics at the top. The best solution I've found is to set a custom title to the activity. Here is what my constructor looks like:
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.your_listview_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.your_header);
...
Where your_listview_layout.xml configures a ListView, and your_header.xml contains whatever custom header layout that you like. Just note that the three lines above must be called in exactly that order to not cause run-time problems.
The tutorial that helped me was http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/ and you can find many related pages on Stack Overflow by searching for the term "setFeatureInt"
Adding a static header is easy, just make a separate relative view that has the alignParentTop (or bottom, right or left) attribute set to true.

Categories

Resources