Search code examples
javaandroidandroid-toast

how can I show toast message when text section is blank and the button gets pressed (in android studio)?


I have Created ToDo list app in android studio. i want to show a toast message when the button pressed while there is no text in it. like "Please enter Text". and also prevent creating any blank list. i have two java class files. MainActivity and FileHelper

MainActivity (code):

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

private EditText ItemET;
private Button btn;
private ListView itemList;

private ArrayList<String> items;
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ItemET = findViewById(R.id.item_edit_text);
    btn = findViewById(R.id.add_btn);
    itemList = findViewById(R.id.item_list);

    items = FileHelper.readData(this);

    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
    itemList.setAdapter(adapter);

    btn.setOnClickListener(this);
    itemList.setOnItemClickListener(this);
}

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.add_btn:
            String ItemEntered = ItemET.getText().toString();
            adapter.add(ItemEntered);
            ItemET.setText("");
            FileHelper.writeData(items, this);
            Toast.makeText(this, "item Added", Toast.LENGTH_SHORT).show();

            break;


    }


}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    items.remove(position);
    adapter.notifyDataSetChanged();
    Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
}

}

FileHelper (code):

public class FileHelper {

public static final String FILENAME = "listinfo.dat";

public static void writeData(ArrayList<String> items, Context context){

    try {
        FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(items);
        oos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

    public static ArrayList<String> readData(Context context) {

        ArrayList<String> itemList = null;
        try {
            FileInputStream fis = context.openFileInput(FILENAME);
            ObjectInputStream ois = new ObjectInputStream(fis);
            itemList = (ArrayList<String>) ois.readObject();
        } catch (FileNotFoundException e) {

            itemList = new ArrayList<>();


            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return itemList;

    }

}


Solution

  • Have a look at below code.

    @Override
        public void onClick(View v) {
            switch(v.getId()) {
                case R.id.add_btn:
                    String ItemEntered = ItemET.getText().toString();
                    if(ItemEntered.trim().isEmpty()){
                        Toast.makeText(getApplicationContext(), "Please Enter some detail", Toast.LENGTH_LONG).show();
                    } else {
                          adapter.add(ItemEntered);
                          ItemET.setText("");
                          FileHelper.writeData(items, this);
                          Toast.makeText(this, "item Added", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }