Skip to content

Instantly share code, notes, and snippets.

@bipinvaylu
Created October 5, 2019 10:49
Show Gist options
  • Save bipinvaylu/7f4904b5943ca29ee35d45764622d269 to your computer and use it in GitHub Desktop.
Save bipinvaylu/7f4904b5943ca29ee35d45764622d269 to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity {
List<String> doItList = new ArrayList<>();
ArrayAdapter<String> adapter;
ListView listView = null;
TextView emptyView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = findViewById(R.id.listview);
emptyView = findViewById(R.id.empty_view);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
doItList);
listView.setAdapter(adapter);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAddDoItItem();
}
});
}
@Override
protected void onResume() {
super.onResume();
refreshUI();
}
protected void refreshUI() {
if (doItList.size() > 0) {
listView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
adapter.notifyDataSetChanged();
} else {
listView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
}
}
protected void showAddDoItItem() {
final EditText inputView = new EditText(this);
inputView.setHint("Add Do Item Name");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
lp.setMargins(10, 10, 10, 10);
inputView.setLayoutParams(lp);
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Do It")
.setCancelable(true)
.setView(inputView)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
doItList.add(inputView.getText().toString());
refreshUI();
}
})
.create();
alertDialog.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment