I am pretty newbie with android development and I am trying to make work an easy example.
When the layout contains fragments, this fragments are inflated and the method findByViewId returns a NULL pointer. I would like to konw how to avoid that.
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.todoit.MainActivity"
tools:ignore="MergeRootFrame" />
so far the only solution I found was to dont use fragment layout :/
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.todoit.MainActivity$PlaceholderFragment">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/myEditText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/myListView"
android:layout_below="#+id/myEditText"
android:layout_alignRight="#+id/myEditText"
android:footerDividersEnabled="false" /> </RelativeLayout>
this code returned a NULL pointer
setContentView(R.layout.activity_main); ListView myListView=
(ListView)findViewById(R.id.myListView);
the only way I found was to use this one instead.
setContentView(R.layout.fragment_main); ListView myListView=
(ListView)findViewById(R.id.myListView);
all this code in the onCreate method.
could someone tell me how to solve this issue? is there a way to inflate the whole layout including fragments?
thanks a lot.
Jose
You should inflate the fragment's view in the fragment code only, not in the activity code. You use onCreateView method to do this.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
ListView myListView= (ListView) v.findViewById(R.id.myListView);
return v;
}
If you want to find your ListView after onCreateView has already been called, you can use getView() method:
ListView myListView= (ListView) getView().findViewById(R.id.myListView);
ListView myListView= (ListView) fragment.getView().findViewById(R.id.myListView); needs to be called from inside the fragment class code and not the main activity. Call it inside method onActivityCreated, by that time the onCreateView function of fragment would have succeeded
Related
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
I'm having an issue where I can create a Fragment, its view appears to be created, but it doesn't show up. The fragment itself is created and any code inside runs without issue, but it is just invisible somewhere. The back button also interacts with it just fine (it "closes" it), it just doesn't physically show up on screen (only the main layout is displayed).
From my FragmentActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_view);
// some logic here that sets a button listener that calls openAddNamePane() when pressed
}
public void openAddNamePane(){
AddNamePane addNamePane = new AddNamePane();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.layout_root, addNamePane, "AddnamePane");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
This is the layout for the View that is first displayed (activity_main_view.xml aka my 'root layout'):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="#color/white"
android:id="#+id/layout_root">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/addPlayerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Player" />
<Button android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonLabel" />
</LinearLayout>
<ListView android:id="#+id/playerListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#color/white"
android:entries="#array/testStringArray" >
</ListView>
</LinearLayout>
This is my fragment class:
public class AddNamePane extends Fragment {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("AddNamePane.onCreateView()");
View view = inflater.inflate(R.layout.add_player_pane_layout, container, false);
return view;
}
// a few other methods here, onAttach(), onStop(), onPause(), etc
// all are empty of logic with just a println to see if they are being called correctly (in which they are)
}
This is the layout for that "AddNamePane" fragment:
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="test name here"
android:hint="Name here" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
I am pretty new to using fragments, not too sure if my issue is in code or if it is something to do with the layouts. My goal is to add the "AddNamePane" to the 'root layout' replacing the main view temporarily. What I did to attempt this was to give my root layout an id and use that in the FragmentTransaction. If it matters, the api level I am using is 8 (Android 2.2) thus using things from android.support.v4.app package.
You're trying to programmatically replace a LinearLayout, while also expecting it to have the hard-coded children. This won't work. You should use a FrameLayout for your FragmentTransaction that is also a child of your layout_root.
If you update your post with the actual xml, I can show you exactly how to do this.
I had a similar problem, when fragment wasn't showing up. The problem was that I removed AndroidAnnotations, but forgot to move the code from onViewCreated and onCreate methods to the onCreateView. I found this mistake by comparing the code of two fragments - one, that was showing up and the other that wasn't. So, if you have similar problems, try to check the code of fragments. It may not show any errors at first glance, but will not still work or show up because of those errors.
Particularly, check that you have this method and that you inflate a view:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_fragment_layout, container, false);
return v;
}
tools:layout="#layout/fragment_up"
Use your layout file as above
<TextView
android:id="#android:id/empty"
style="#style/FacebookFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/no_result_found"
android:textColor="#color/white" />
this is the xml code for empty view. I am setting empty view as follows:
list.setAdapter(adapter);
list.setEmptyView(findViewById(android.R.id.empty));
I have the items in the listview even then also before listview is populated the empty view is shown.
I don't want that empty view to be shown when listview contains some item.
Please help.
if you are using ListActivity then no need to set the empty view manually. Remove list.setEmptyView(findViewById(android.R.id.empty)); from your code.
Change your layout like below then it's automatically add EmptyView if list has no item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
You have to set the empty view before setting the adapter, if your MainActivity is not extending ListActivity.
Try this
list.setEmptyView(findViewById(android.R.id.empty));
list.setAdapter(adapter);
Sorry for the late answer!
Alternate method: Make your activity extend from ListActivity. And add a textview to your list layout with id android.R.id.empty
Try this
View view = getLayoutInflater() .inflate(<resource id of the empty view>, null);
ViewGroup viewGroup= ( ViewGroup)list.getParent();
viewGroup.addView(view);
list.setEmptyView(view);
list.setAdapter(adapter);
There are some correct answers, however, I think this is the best approach I've found:
View emptyView = getLayoutInflater().inflate(R.layout.empty_list_cart, null);
addContentView(emptyView, listView.getLayoutParams()); // You're using the same params as listView
listView.setEmptyView(emptyView);
listView.setAdapter(adapter);
Whatever #dhawalSodhaParmar has explained is correct. But there is a confusion when you look at the way the answer is put down.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SampleActivity">
<ListView
android:id="#+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:divider="#null"
android:dividerHeight="0dp" />
<!-- Empty view is only visible when the list has no items. -->
<TextView
android:id="#+id/empty_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="NO DOCUMENTS AVAILABLE!"
android:textColor="#525252"
android:textAppearance="?android:textAppearanceMedium"
android:visibility="gone"/>
</RelativeLayout>
Suppose if you already have a list view and adapter backing the same with data. And assuming everything is working fine, whoever wants to implement this empty list view screen has to start like below:
Step 1:
Modify the existing Activity XML file which has the list view with the one I have used here. You can use either a Linear Layout or Relative Layout, but preferably Relative because of the flexibility. And a textView like I have written.
Step 2:
Go to your Activity Java file, and after your setContentView
ListView emptyListView = (ListView) findViewById(R.id.list);
// set your adapter here
// set your click listener here
// or whatever else
emptyListView.setEmptyView(findViewById(R.id.empty_text_view));
That's it, now run and you are set!
// first of all create a global variable of Text view
// which is displayed when the list is empty
private TextView mEmptyStateTextView;
//then in onCreate(Bundle savedInstanceState)
/Setting empty text view
mEmptyStateTextView=(TextView) findViewById(R.id.emptyview);
view.setEmptyView(mEmptyStateTextView);
//then in onLoadfinished method
mEmptyStateTextView.setText(R.string.no_earthquakes);
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 have a ListView that I am creating. The content is set from a separate adapter class. When the adapter returns zero items I want the empty view to display from a separate xml. This is happening inside a SherlockFragment. However for some reason I cannot get the emptyView to display.
Help Please?
Code:
RelativeLayout fragView = new RelativeLayout(getActivity());
ListView listView = new ListView(getActivity());
listView.setEmptyView(getActivity().findViewById(R.id.emptyView));//This is having no effect
scoreViewAdapter = new ScoreViewAdapter(getActivity(),
databaseHelper.getAllTimeRecords());
listView.setAdapter(scoreViewAdapter);
if (scoreViewAdapter.getCount() < 1)
listView.//can I set something here to force or change my view? From the methods available to ListView I don't see any potential solutions..
fragView.addView(listView);
Here is the xml....pretty standard.:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/emptyView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/emptyText"
android:src="#drawable/ic_android_logo" />
<TextView
android:id="#+id/emptyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:text="No Items Found"
android:textAppearance="?android:attr/textAppearanceLarge" />
Thanks to Faizan's feedback to lead me here. Here is the solution:
View empty_view = inflater.inflate(R.layout.empty_view, null);
...
if (scoreViewAdapter.getCount() < 1)
fragView.addView(empty_view);
fragView.addView(listView);
You have to inflate that xml using LayoutInflater and then call
listView.setEmptyView(inflated_view);
You can get get the inflater from the onCreateView overrided method anc do
View inflated_view = inflater.inflate(R.layout.my_empty_layout,null);
I hope it will resolve the problem.
I believe you extends FragmentList and not ListActivity
so you actually can use setEmptyView