I have a xml Layout file (ListLayout.xml) where i create listView and i want this layout to be shown on a fragment on main_activity_layout. so go to MainActivity_layout and i added a fragment , when it is proposed to choose a class i choosed the class Listfrg where i return the layout of listLayout.xml within OnCreateView methode , but nothing is shown why ?
ListLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/progress_container_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<FrameLayout
android:id="#+id/list_container_id"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/empty_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" >
</ListView>
</FrameLayout>
</FrameLayout>
MainActivityLayout
<fragment
android:layout_width="162dp"
android:layout_height="match_parent"
android:name="com.example.pack2.t9ahbin.Listfrg"
android:id="#+id/fragment3" />
Class Listfrg :
public class Listfrg extends ListFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frglist, container, false);
return view;
}
}
nothing is shown why?
Because there is no data in your list.
Please use an ArrayAdapter or some Adapter variant, put some data into it with adapter.add, and the use listView.setAdapter. To actually give data to your Listview.
You can do this in onCreateView and use getListView() or this to get the ListView.
ListView listView = (ListView) view.findViewById(android.R.id.list);
An Adapter is the data container for a ListView. Without one, how do you expect to display data?
Also your empty view needs to use android:id="#android:id/empty"
This is all well covered in AdapterViews
Related
I need help with my android app. I need customize items in listview. My app works with fragments. I don’t know how to implement list_item.xml to my code.
KartyListFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.karty_list, container, false);
return view;
}
karty_list.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="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:visibility="gone" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/prazdna_db"
android:textColor="#f00"
android:visibility="visible" />
</LinearLayout>
I want to designed one row in list_item.xml but i don´t know how to.
here you can find a good tutorial to build your list view.. I think it will be helpful for you.
Jus create the layout you want and pass it along to the list adapter as the view to be inflated. For example, using a SimpleAdapter:
mAdapter = new SimpleAdapter(context, list, R.layout.my_custom_list_item, from, to);
mListView.setAdapter(myAdapter);
I am trying to create an activity with 2 Fragment
First fragment show a list of galleries using a LisFragment
Second fragment show the preview images of the gallery selected in the first fragment
When I declare the fragment in the view it works. But now I want to manage fragment dynamically :
GalleriesFragment gfs = new GalleriesFragment();
GalleryFragment gf = new GalleryFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.layout_m,gfs);
ft.add(R.id.layout_m,gf);
ft.commit();
My 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="horizontal"
android:id="#+id/layout_m">
</LinearLayout>
When doing this only the first fragment added is shown, I tried to invert their order when adding fragment...
When I was working on the layout I could set my fragment width with:
android:layout_width="400dp"
Now I have 2 questions :
How can I make my 2 fragments to display?
How can I set the width (programmatically?) of the 1st fragment and the second to fill the remaining width?
Thanks for your help!
PS:
GalleriesFragment doesn't have a layout and is not overriding onCreateView, but has an ArrayAdapter returning view like this for each rows:
<?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" >
<ImageView
android:id="#+id/idimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:layout_marginLeft="10sp"
android:layout_marginTop="10sp"
android:contentDescription="preview"
>
</ImageView>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/texttitre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="7sp"
android:layout_marginTop="10sp"
android:background="#layout/roundcorner"
android:gravity="center"
android:text="Title here" >
</TextView>
<TextView
android:id="#+id/itemdescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:text="description" >
</TextView>
</LinearLayout>
</LinearLayout>
GalleryFragment :
<?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" >
<Gallery
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Update 2 :
I added an onCreateView in my GalleriesFragment class :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
FrameLayout.LayoutParams lparams = new FrameLayout.LayoutParams(400, LayoutParams.MATCH_PARENT);
container.setLayoutParams(lparams);
return super.onCreateView(inflater, container, savedInstanceState);
}
This fixes the first fragment's width, but the second one is still not showing...
How can I make my 2 fragments to display ?
Your current approach will make the first fragment supplied to the add method to take all the space of your container layout. One solution is to use a layout that reserves space for each of the two fragments that get added, something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_m"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/frame1"
android:layout_width="100dp"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/frame2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Then in code:
ft.add(R.id.frame1, gfs);
ft.add(R.id.frame2, gf);
ft.commit();
How can I set the width (programmatically ?)of the 1st fragment and
the second to fill the remaining width?
Fragments are not just ordinary views but one way to do that is to use your single LinearLayout layout file and set the LayoutParams of the fragment's view in onActivityCreated:
getView().setLayoutParams(new LinearLayout.LayoutParams(100, LinearLayout.LayoutParams.WRAP_CONTENT));
Of course this will make your fragments to be tied to the parent container, also I don't know how well this will work in the overall fragment system.
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.
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.
For some reason the empty view, a TextView in this case, always appears even when the ListView is not empty. I thought the ListView would automatically detect when to show the empty view.
<RelativeLayout android:id="#+id/LinearLayoutAR"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ListView android:id="#+id/ARListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
<ProgressBar android:id="#+id/arProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"></ProgressBar>
<!-- Here is the view to show if the list is emtpy -->
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No Results" />
</RelativeLayout>
How can I hook up the empty view properly?
When you extend FragmentActivity or Activity and not ListActivity, you'll want to take a look at:
ListView.setEmptyView()
It should be like this:
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No Results" />
Note the id attribute.
As appsthatmatter says, in the layout something like:
<ListView android:id="#+id/listView" ... />
<TextView android:id="#+id/emptyElement" ... />
and in the linked Activity:
this.listView = (ListView) findViewById(R.id.listView);
this.listView.setEmptyView(findViewById(R.id.emptyElement));
Does also work with a GridView...
I tried all the above solutions.I came up solving the issue.Here I am posting the full solution.
The xml file:
<RelativeLayout
android:id="#+id/header_main_page_clist1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:paddingBottom="10dp"
android:background="#ffffff" >
<ListView
android:id="#+id/lv_msglist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/divider_color"
android:dividerHeight="1dp" />
<TextView
android:id="#+id/emptyElement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="NO MESSAGES AVAILABLE!"
android:textColor="#525252"
android:textSize="19.0sp"
android:visibility="gone" />
</RelativeLayout>
The textView ("#+id/emptyElement") is the placeholder for the empty listview.
Here is the code for java page:
lvmessage=(ListView)findViewById(R.id.lv_msglist);
lvmessage.setAdapter(adapter);
lvmessage.setEmptyView(findViewById(R.id.emptyElement));
Remember to place the emptyView after binding the adapter to listview.Mine was not working for first time and after I moved the setEmptyView after the setAdapter it is now working.
Output:
I highly recommend you to use ViewStubs like this
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ViewStub
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout="#layout/empty" />
</FrameLayout>
See the full example from Cyril Mottier
<ListView android:id="#+id/listView" ... />
<TextView android:id="#+id/empty" ... />
and in the linked Activity:
this.listView = (ListView) findViewById(R.id.listView);
this.listView.setEmptyView(findViewById(R.id.empty));
This works clearly with FragmentActivity if you are using the support library. Tested this by building for API 17 i.e. 4.2.2 image.
Activity code, its important to extend ListActivity.
package com.example.mylistactivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.example.mylistactivity.R;
// It's important to extend ListActivity rather than Activity
public class MyListActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
// shows list view
String[] values = new String[] { "foo", "bar" };
// shows empty view
values = new String[] { };
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
values));
}
}
Layout xml, the id in both views are important.
<?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">
<!-- the android:id is important -->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!-- the android:id is important -->
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="i am empty"/>
</LinearLayout>
Just to add that you don't really need to create new IDs, something like the following will work.
In the layout:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/empty"
android:text="Empty"/>
Then in the activity:
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setEmptyView(findViewById(android.R.id.empty));
I had this problem. I had to make my class extend ListActivity rather than Activity, and rename my list in the XML to android:id="#android:id/list"
A programmatically solution will be:
TextView textView = new TextView(context);
textView.setId(android.R.id.empty);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setText("No result found");
listView.setEmptyView(textView);
First check the list contains some values:
if (list.isEmpty()) {
listview.setVisibility(View.GONE);
}
If it is then OK, otherwise use:
else {
listview.setVisibility(View.VISIBLE);
}