Android: addHeader to ListView - android

I'm trying to add a header to my listView but I see only black screen when activity launches
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincoverflow);
final ListView itemslist = (ListView)findViewById(R.id.itemslist);
LayoutInflater inflater = getLayoutInflater();
ViewGroup mTop = (ViewGroup)inflater.inflate(R.layout.main_header, itemslist, false);
itemslist.addHeaderView(mTop, null, false);
//some actions with header and list items
//example: coverflow_item_title is located in header(R.layout.main_header)
title = (TextView)findViewById(R.id.coverflow_item_title); //checked - is not null
CoverFlow coverFlow = (CoverFlow) findViewById(R.id.coverflow);
ImageAdapter coverImageAdapter = new ImageAdapter(this);
coverFlow.setAdapter(new ImageAdapter(this));
coverImageAdapter.createImages();
coverFlow.setAdapter(coverImageAdapter);
coverFlow.setOnItemSelectedListener(new CoverAdapterView.OnItemSelectedListener(){
...
adapter = new LazyAdapter(MainCoverflowActivity.this, tools, tools_images);
itemslist.setAdapter(adapter);
}
itemslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
...
}
}
maincoverflow.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"
android:orientation="vertical" >
<ListView
android:id="#+id/itemslist"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
main_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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/coverflow_item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<com.thumbsupstudio.mobitour.CoverFlow
android:id="#+id/coverflow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/shadowbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/shadow_box" />
</LinearLayout>
You may notice that I'm using Coverflow widget (from here http://www.inter-fuser.com/2010/01/android-coverflow-widget.html) - it works fine.
In general, I'm changing listView content when Coverflow item is changed. It works fine, but I need Coverflow to scroll with listView, so I decided to attach it to header. After that, there's no listView and Coverflow on the screen, everything is black.

try this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/headerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<TextView
android:id="#+id/coverflow_item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<com.thumbsupstudio.mobitour.CoverFlow
android:id="#+id/coverflow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/shadowbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/shadow_box" />
</RelativeLayout>
<ListView
android:id="#+id/itemslist"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>

I found a solution. I don't fully understand WHY but:
NOT CORRECT
init listView
inflate header and add it to the list
set list adapter in Coverflow onItemSelected handler (it launch automatically with item position=0)
CORRECT
init listView
inflate header and add it to the list
set any adapter to listView (!!!)
set listView adapter in Coverflow onItemSelected handler (it launch automatically with item position=0)
after activity launches listView has adapter content which was set in Coverflow onItemSelected handler. What for is first adapter assignment - I don't understand.

Related

How to inflate the icon image in my layout?

I am trying to add a small home icon image to top of my list view.Below you can see the code of my adapter. It works perfectly for setting dynamic text but it fails when i want to add an image.
I do not get any runtime error, just the images aren't loaded.
The images referenced are 35x35 png file.
How to implement this? Im new to android. please guide me.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewList = Nexttopic.this.getLayoutInflater().inflate(R.layout.activity_nexttopic, null);
dialogMarketList = new Dialog(Nexttopic.this);
dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogMarketList.setContentView(viewList);
dialogMarketList.show();
lvForDialog = (ListView) viewList.findViewById(R.id.List_view);
lvForDialog.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
ArrayAdapter<String> adapter = (new ArrayAdapter<String>(Nexttopic.this, R.layout.row_topic, R.id.child_row,tnamelist));
ImageView home = (ImageView) findViewById(R.id.hme);
lvForDialog.setAdapter(adapter);
row_topic xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<ImageView android:id="#+id/hme"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_above="#+id/child_row"
android:src="#drawable/homex"/>
<TextView
android:id="#+id/child_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="#drawable/bg"/>
</RelativeLayout>
activity_nexttopic
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Nexttopic" >
<ListView
android1:id="#+id/List_view"
android1:layout_width="match_parent"
android1:layout_height="250dp"
android1:layout_centerHorizontal="true"
android1:layout_centerVertical="true"
android:background="#drawable/bg">
</ListView>
</RelativeLayout>

How do I get a TextView to show up on top of a ListView?

I'm trying to display a TextView on top of a ListView from within a ListActivity, but the text is not showing up.
LayoutInflater inflater = (LayoutInflater)getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View layout = inflater.inflate( R.layout.main, null );
TextView myTextView = (TextView)layout.findViewById( R.id.myTextView );
myTextView.setVisibility( View.VISIBLE );
myTextView.setText( R.string.emptyList );
setContentView( layout );
Here is the main layout:
<?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/myTextView"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
I want an "Empty list" notice to go at the top of the screen in case the list view is empty.
Why dont you use setEmptyView for your ListView and do your notification that way.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="300dip"
android:id="#+id/list_view" />
<TextView
android:id="#+id/empty_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List view is empty"
android:visibility="gone" />
</LinearLayout>
And call in your code.
ListView listView = (ListView) findViewById( R.id.list_view );
listView.setEmptyView( findViewById( R.id.empty_list_view ) );
listView.setAdapter(yourAdapter);
Check these for more info.
Correct use of setEmtpyView in AdapterView
http://www.technotalkative.com/android-listview-setemptyview/
You could use a FrameLayout instead of a LinearLayout, that way both the TextView and the ListView will share the same layout space (although not strictly necessary).
Then you can set the ListView empty view to myTextView, and it will take care of showing or hiding the empty view automatically for you.
I'm not sure what you mean regarding the TextView. Are you trying to place something above the list when the activity loads? In that case adding it above the list in the LinearLayout will do just fine.
To get the "empty list" notice you are referring to, make sure activity you are using extends ListActivity, not just Activity, and that the respective views are titled "list" and "empty."
<?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"
>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/empty_user_list"/>
</LinearLayout>

Why does empty view in layout file shows up on screen even though listview has items?

I have a Listview to show some item and i have also used an empty view in case my adapter has no item for listview to show.Problem is when I go this this activity first it shows the empty view on the screen for a second and then loads the item and show them in listview.
My Activity onCreate looks like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite_exams);
list = (ListView) findViewById(R.id.favoriteExamsList);
View empty = findViewById(R.id.favoriteExamsListEmptyView);
list.setEmptyView(empty);
new readingFavFileTask().execute(UtilityFuctions.FAV_EXAMS_FILE_NAME);
}
My layout file is:
<?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/favoriteExamsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<LinearLayout
android:id="#+id/favoriteExamsListEmptyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="#+id/favoriteExamsEmptyViewMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="You do not have any favorite Exams.Please add Exams using below button."
android:textSize="20dp" />
<Button
android:id="#+id/favoriteExamsAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onClickAddExams"
android:text="Add Exams"
>
</Button>
</LinearLayout>
</LinearLayout>
How I Do make sure that empty view never shows up even for a second if we have data in our adapter.Should I attach the empty view to list later after checking the adapter or is thr any other way to do this?
I found the answer. I just need to apply Empty View list.setEmptyView(empty) in my AsyncTask onPostExecute() method after setting my adapter to list and it works perfectly.

ListView inside Fragment android

Im having trouble with implementing list view inside a fragment.
the xml code 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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/jo_logo" />
</LinearLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</ListView>
</LinearLayout>
simple ImageView and afterwards the list view.
I've already tried to implement:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(),
android.R.layout.simple_list_item_1, viewList);
setListAdapter(adapter);
where viewList is View[], on the onCreateView function.
seems that it's not working that way inside fragment.
thing is i need that the first view will be ImageView and second one is ListView, and all inside the Fragment.
please help, thank in advance, udi
In your layout xml change android:id="#+id/listView1" to android:id="#android:id/list" otherwise the ListActivity / ListFragment won't find your ListView.

Set background image for listview with mutiple column

I have to setup the background image for listview.Not setting image for each row.have to setup the background image for whole screen & select the radio button when list view load. that means first option should be selected
This is my code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sales_routes); // **Added edited**
ArrayList<Object> routeList = getWmRoute();
ArrayList<String> routhPath = new ArrayList<String>();
for(int i = 0; i<routeList.size();i++){
routhPath.add(((WMRoute) routeList.get(i)).getDescription());
}
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_two_column, R.id.FROM_CELL, routhPath));
ListView list=getListView();
}
and my xml file is (list_two_column.xml)
<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!--android:background="#drawable/wallpaper" -->
<RadioButton android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<TextView android:id="#+id/FROM_CELL"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Added Newly - sales_routes.xml
<?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">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginRight="6dip"
android:dividerHeight="2dip"
android:background="#drawable/wallpaper" />
</LinearLayout>
currently its display list, the problem is image background image
Requirement
select the radio button when listview load. that means first option should select
make a style theme in values folder and add a windowBackground item to it and add that style to your layout xml

Categories

Resources