Showing empty view when ListView is empty
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.
empty view not display in listview
try this
View emptyV = getActivity().getLayoutInflater().inflate(R.layout.empty_view, null);
TextView tvMSG = (TextView) emptyV.findViewById(R.id.tvMSG);
tvMSG.setText("No product available.");
ViewGroup viewGroup= (ViewGroup) listView.getParent();
viewGroup.addView(emptyV, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
listView.setEmptyView(emptyV);
show something when listview is empty
I want you to check this post setEmptyView on ListView not showing its view in a android app, it should solve your empty view issue.
And if you want to prevent the app from throwing an IndexOutOfBoundsException, you can simply check the size of your dataModels
array before setting up your adapter.
if (dataModels.size() > 0)
{
//Setup our adapter here
}
Hope this helps :)
setEmptyView is not displaying image when listView is empty
public class FenceActivity extends AppCompatActivity {
List<Fence> fenceList;
SQLiteDatabase sqLiteDatabase;
ListView listViewFences;
FenceAdapter fenceAdapter;
DataBaseHelper dataBaseHelper;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.savedfences);
listViewFences = findViewById(R.id.fencesListView);
fenceList = new ArrayList<>();
showFencesFromDatabase();
}
public void showFencesFromDatabase() {
dataBaseHelper = new DataBaseHelper(this);
Cursor cursor = dataBaseHelper.getAllData();
if (cursor.moveToFirst()) {
do {
fenceList.add(new Fence(cursor.getInt(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getInt(3)));
} while (cursor.moveToNext());
}
cursor.close();
fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
listViewFences.setAdapter(fenceAdapter);
listViewFences.setEmptyView(findViewById(R.id.emptyElement));
}
public void reloadFencesFromDatabase() {
dataBaseHelper = new DataBaseHelper(this);
Cursor cursor = dataBaseHelper.getAllData();
fenceList.clear();
if (cursor.moveToFirst()) {
do {
fenceList.add(new Fence(cursor.getInt(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getInt(3)));
} while (cursor.moveToNext());
}
cursor.close();
fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
listViewFences.setAdapter(fenceAdapter);
listViewFences.setEmptyView(findViewById(R.id.emptyElement));
}
}
Instead of listViewFences.setEmptyView(findViewById(R.id.emptyElement));
add this
listViewFences.setVisibility(fenceList.size>0?View.VISIBLE:View.GONE);
emptyElement.setVisibility(fenceList.size>0?View.GONE:View.VISIBLE);
Also define the emptyElement view
in the activity.
emptyView not being replaced by listView
try this
<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/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="@+id/no_requests_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:text="no request" >
</TextView>
TextView emptyView = findViewById(R.id.no_requests_textView);
if(requestDetailsArrayList.size()==0)
{
emptyView.setVisibility(View.VISIBLE);
ListView.setVisibility(View.GONE)
}
else
{
emptyView.setVisibility(View.GONE);
ListView.setVisibility(View.VISIBLE)
}
Android displaying text when ListView is empty
I'm guessing you are using a regular Fragment
or Activity
with a ListView
inside of it. If you are, you must add the empty layout to the ListView
manually.
E.g.
ListView lv = (ListView)findViewById(android.R.id.list);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);
Then your ListView
will automatically use this view when its adapter is empty
If you are using a ListActivity
you do not need to call setEmptyView()
on the ListView
since the ListActivity
automatically manages that for you.
Custom empty view in ListView android
Change your xml like below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<FrameLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_centerInParent="true"
android:id="@+id/emptyView">
<TextView
android:layout_width="wrap_content"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Click Refresh to load data"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_retry"
android:textColor="@android:color/white"
android:background="@drawable/orange_button_selecter"
android:layout_gravity="center"
android:textSize="25sp"
android:text="@string/retry"/>
</FrameLayout>
<ListView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:dividerHeight="2dp"
android:id="@+id/list_foods"/>
</RelativeLayout>
put that lines in java
View empty = findViewById(R.id.emptyView);
btn_retry=(Button)empty.findViewById(R.id.btn_retry);
listView.setEmptyView(empty);
Now you can write onClickListener() in same Activity as it belongs with same xml layout.
btn_retry.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Do the refresh thing or verify with Toast.
}
});
Here, If the listview is an empty it will show button refresh. Otherwise it will display the filled list.
How i can display a message when listview is empty
ListView lv;
ArrayList < String > FilesInFolder = GetFiles(Environment.getExternalStorageDirectory() + "/" + "foldername" + "/" + "file.txt");
lv = (ListView) findViewById(R.id.lit_view);
// This will do the needful
if (FilesInFolder == null || FilesInFolder.isEmpty()) {
lv.setVisibility(View.GONE)
Toast.makeText(this, "Their is nothing to view", Toast.LENGTH_SHORT).show()
// Add another textView in xml and make it visible
} else
lv.setAdapter(new ArrayAdapter < String > (this,
android.R.layout.simple_list_item_1, FilesInFolder));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView << ? > parent, View v, int position, long id) {
Intent intent = new Intent(getApplicationContext(), pdfviewer.class);
intent.putExtra("FileName", (String) parent.getItemAtPosition(position));
startActivity(intent);
finish();
}
});
}
Related Topics
Passing Data from One Activity to Another Using Bundle - Not Displaying in Second Activity
Panic: Broken Avd System Path. Check Your Android_Sdk_Root Value
Using Android Vector Drawables on Pre Lollipop Crash
How to Use Apply a Custom Drawable to Radiobutton
Removing Log Call Using Proguard
Android Firebase Dynamitemodule: Failed to Load Module Descriptor
Difference Between Onstart() and Onresume()
Viewpager with Google Maps API V2: Mysterious Black View
How to Rename the Android Package Name
Checking Toast Message in Android Espresso
Environment.Getexternalstoragedirectory Does Not Return the Path to the Removable Storage
Execution Failed for Task ':App:Compiledebugaidl': Aidl Is Missing
How to Turn On/Off Wifi Hotspot Programmatically in Android 8.0 (Oreo)
How to Test Android Referral Tracking
Android HTML5 Input Type="Password" and Numeric Keyboard