I am developing an android app and have a problem: my main activity contains a listview and a button and I need to disable the button when the listview is empty.
I've tried this but the listview doesn't have addTextChangedListener
.
I am developing an android app and have a problem: my main activity contains a listview and a button and I need to disable the button when the listview is empty.
I've tried this but the listview doesn't have addTextChangedListener
.
If you wants to hide the button, then you can use button.setvisiblity(View.GONE)
or else If you want to just disable the button then you can setAlpha and in button onClick listner you can restrict the action's if list view is empty.
You need to use the adapter .registerDataSetObserver()
method which is triggered every time the data of the ListView
is changed.
Here is a simple example of what you need to achieve
How it looks like
Layout
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btn_disable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Disabled" />
<Button
android:id="@+id/btn_add_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="@+id/btn_remove_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Behaviour
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnDisabled = findViewById(R.id.btn_disable);
Button btnRemoveData = findViewById(R.id.btn_remove_data);
Button btnAddData = findViewById(R.id.btn_add_data);
final ListView mList = findViewById(R.id.listview);
final List<String> data = new ArrayList<>();
final ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);
mList.setAdapter(adapter);
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
data.add("Data");
adapter.notifyDataSetChanged();
}
});
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
if (mList.getAdapter().isEmpty())
btnDisabled.setEnabled(false);
else
btnDisabled.setEnabled(true);
}
});
btnRemoveData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!data.isEmpty())
data.remove(data.size() - 1);
adapter.notifyDataSetChanged();
}
});
}
}