Red Shaman
Red Shaman

Reputation: 139

Empty ListView show another activity

I am making an application which uses ListView to show DB elements. But in the first opening or when user will delete everything DB will be empty and then ListView will show blank screen. I want to show a message when it happens. Not only TextView but also Button. For example "DB now empty. Click on button and try to add some records". ListView 's setEmptyView method allows to add only TextView . Is it really possible to create a layout and show it when ListView is empty?

Upvotes: 1

Views: 329

Answers (3)

Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

You can use the property setEmptyView.

If the list is empty and your desired display.

    ListView listView = (ListView) findViewById(R.id.lst);
    View child = getLayoutInflater().inflate(R.layout.empty_view, null);
    ((ViewGroup)listView.getParent()).addView(child);
    listView.setEmptyView(child);

Upvotes: 2

Awais Ahmad
Awais Ahmad

Reputation: 427

You can pre-define a layout containing text view and and button as you stated in your question and set it's visibility = gone in your xml or in java code. When the List View will be empty you can set visibility the visibility of the listview.setVisibility(View.GONE) and set your's layout (containing button and a textview) layout.setVisibility(View.VISIBLE)

Upvotes: 0

Alok Gupta
Alok Gupta

Reputation: 1360

You can dynamically add a button in the activity layout if your list is empty. Set onClickListner on the button and override the onClick().

Refer below code:

LinearLayout linear = (LinearLayout)findViewById(R.id.layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId("btnDynamic");
btn.setText("Go to create entry page");
linear.addView(btn, params);

btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Toast.makeText(view.getContext(),
                "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                .show();
    } 
}); 

Let me know if this helps.

Upvotes: 0

Related Questions