I want to add a title bar in the list view.
I have added a text view inside the listview which can act as title:
Layout File :
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/layout_rounded"
android:layout_marginRight="10px"
android:layout_marginLeft="10px"
android:layout_marginTop="30px"
android:layout_below="#id/linlaypay"
android:orientation="vertical"
android:padding="10dip" >
<ListView
android:id="#+id/rectran"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout >
Here is the code used to add the title listview dynamically:
listView = (ListView) findViewById(R.id.rectran);
TextView textView = new TextView(getApplicationContext());
textView.setText("header view");
listView.addHeaderView(textView);
The problem is ,an emty text view isadded to the listview without the text "header view".
Why?Am I missing anything?
I don't believe you can just add a view like this. The view being passed into listView needs to be inflated.
create a header.xml layout file with the text view in it with the text set.. and then run the code below.
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(header, null, false);
If I were you, I would add a title to the ActionBar, assuming that your ListView is the whole Activity. If it isn't the whole Activity or it doesn't take up the whole screen, you could add the title to the layout rather than dynamically adding it.
http://developer.android.com/reference/android/app/ActionBar.html
Check out the Android Developers documentation on the ActionBar
My activity holds 3 small listviews and I wanted to give title to each of the view by adding textview.
However the text in the Textview was not visible because the size of the text was too small.
setting the text size of the textview resolved the issue.
Thanks !!
Related
I am currently having some formatting issues with Android's ListView. After looking through several questions on SO I did not find any proper solutions.
I have a ListView (used for navigation) including one or more views (actual navigation items). What I want to achieve is - the ListView uses up the whole vertical space (heigth set to match_parent) and the cells are centered vertically in this ListView.
Simply setting the gravity to center_vertical for the ListView does not do the trick. I currently can only achieve this look if I wrap the ListView in another view (which has set the gravity to center_vertical) and change the ListView's height to wrap_content. But this seems not to be the perfect solution as the resulting measuring operations (the adapter’s getView method is called multiple times for the same position) have a performance impact, even if applying a proper holder concept for the navigation items. Is there any solution to this issue?
ListView (gravity does not work):
<ListView android:id="#+id/lvNavigation"
android:layout_width="#dimen/navWidth"
android:layout_height="match_parent"
android:gravity="center_vertical" />
ListView with workaround (bad performance):
<LinearLayout android:layout_width="#dimen/navWidth"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical">
<ListView android:id="#+id/lvNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Navigation item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_weight="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="#dimen/horizontalMargin"
android:paddingRight="#dimen/horizontalMargin">
...
</LinearLayout>
Listview is probably a bad thing to use for this purpose. I think what you should do is use a recycler view with a custom Layout Manager
In particular you probably want to override onLayoutChildren
I did a quick search and could not find a library that does this for you already.
Alternatively, if you have elements where you can predict the height you can add a header view that is blank and set the height so that the list items will appear centered.
So something like
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv, false);
header.setLayoutParams(new AbsListView.LayoutParams(<width>, <height>));
lv.addHeaderView(header, null, false);
I have a listview inside of a viewpager. I set an empty view on the listview. If this listview starts empty, when i add something to it then right or left swipes on the listview won't switch between tabs. If the listview starts with something (not empty) when i set the adapter on it, then i can swipe. If i remove the setEmptyView on the listview, it always works whether it starts with data or without it.
How can i set an empty view and still get the listview to swipe between tabs?
This is how i set the empty view:
mListview.setEmptyView(findViewById(R.id.empty_view));
the empty view is a textview directly below the listview in a linearlayout
the empty view xml:
empty_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/empty_list"
android:textColor="#color/lighter_gray"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/no_item"
android:textStyle="italic">
</TextView>
I would suggest removing your empty view from the current layout file, and move it into its own dedicated layout file. Then inflate this new layout in your class when you want to use it as an empty view. The end result would be something like this (not compiled, coding from memory):
final View v = LayoutInflater.from(context).inflate(R.layout.empty_view, null);
mListview.setEmptyView(v);
or maybe
final View v = LayoutInflater.from(context).inflate(R.layout.empty_view, mListview, false);
mListview.setEmptyView(v);
I am making an android app for diabetic patients. In that app I want to show the food item with their quantity for every meal for diet control. It should look somewhat like below:
RICE 100gm
POTATO 50gm
FISH 60gm
Those info will be obtained from my database dynamically. However I am facing problem arranging them. I want the food items to be aligned left and quantity aligned right of the screen. As below (dots denote the screen )
|-------------------------------------------------------|
|RICE 100gm |
|POTATO 50gm |
|FISH 60gm |
|-------------------------------------------------------|
To do that I have declared a relative Layout in xml file. The code looks as below :
<?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="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>
All I need to do now is to create textview and add them to this relative layout dynamically. But I am confused how to do that. Any hint please...
The final output should look something like below :
I would agree with #Szymon that the best way to do this is with a ListView. However, if you don't want to do that, here's how I would do it:
Create a separate layout for each item you want to display. Make it a RelativeLayout, and place all the TextViews there in accordance to how you want the items to be displayed (alignParentRight / alignParentLeft).
Dynamically create views by inflating this new layout, assign the text of the TextViews by using findViewById() on the views you inflate during runtime
Add the views to the LinearLayout
(EDIT: How to do Step 2:)
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.my_item_layout, null);
TextView tv1 = (TextView)view.findViewById(R.id.tv1);
tv1.setText(myText);
I want to insert a TextView into a LinearLayout that's in a different XML file that is being used as layout for a ListView. But it seems that I can't access the LinearLayout that's in the other XML file.
I run a loop that gets category titles, and for each title I wanna create a TextView with the title.
How I insert the TextView
LinearLayout Layout = (LinearLayout) findViewById(R.id.itemDesign); //When debugging I can see "Layout" is just null. itemDesign is also not in main.xml.
TextView title = new TextView(this);
title.setText(CatName);
Layout.addView(title);
XML used by ListView "single_list_item.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/itemDesign"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Name Label -->
<TextView android:id="#+id/name_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textColor="#43bd00"/>
<!-- Description Label -->
<TextView android:id="#+id/email_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"/>
</LinearLayout>
It sounds like you need to inflate your view. In getView() of adapter class
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.your_layout, parent, false);
Then to access your TextViews
TextView firstTV = (TextView) rowView.findViewById(R.id.some_id);
All about LayoutInflater
I guess, you are not setting the activity content view before using findViewById():
setContentView(R.layout.myLayout);
After that initialize your text view, and add width, height params to that. Then there should be no problems in adding that text view to the layout.
Maybe you should try invalidating the layout after you add the TextView:
Layout.addView(title);
Layout.invalidate();
I'm still not entirely sure what you want to achieve, maybe you could give us some more information regarding what you want to accomplish?
If you want to make a new ListView item based on an xml file, you need to inflate it first. Then you can call findViewById() on the resulting view to get the children.
LayoutInflater inf = LayoutInflater.from(getContext());
LinearLayout listItemLayout = (LinearLayout)inf.inflate(R.layout.single_list_item, null);
TextView nameLabel = (TextView) listItemLayout.findViewById(R.id.name_label);
I am using addHeaderView to add a view item to the top of a ListView. I also have a TextView to display a message saying there are no items in the list.
Here is the layout:
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/list_empty"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
And the Java code:
final ListView listView = getListView();
final View view = getLayoutInflater().inflate(R.layout.list_item_add,
listView, false);
listView.addHeaderView(view, null, true);
When there are items in the ListView then the header is shown but if I delete all items in the list (except the header view) then the header view disappears.
I would like the header view to be visible in the list view whether there are items in the list or not.
Thanks,
Remove the #android:id/empty view from your layout, or override/subclass your adapter to return false from isEmpty()
From my experience (SDK version 10):
Overriding isEmpty() in the adapter makes it work.
Then, it is optional to remove the #android:id/empty view.