Click here to Skip to main content
15,313,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1-week-Android newbie here.
I developed some basic activities and I wanted to implement Navigation Drawer only I realized I need to change to fragment. Any idea what should I do given my basic activity.

public class MainActivity extends AppCompatActivity {
    DatabaseReference db;
    FirebaseHelper helper;
    CustomAdapter adapter;
    ListView lv;
    EditText nameEditTxt, destinationEditTxt, originEditTxt;
    public EditText dateEditTxt;
    DatePickerDialog datePickerDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        lv = (ListView) findViewById(R.id.lv);

        //INITIALIZE FIREBASE DB
        db = FirebaseDatabase.getInstance().getReference();
        helper = new FirebaseHelper(db);

        //ADAPTER
        adapter = new CustomAdapter(this, helper.retrieve());
        lv.setAdapter(adapter);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                displayInputDialog();
            }
        });}

    public void showPickerDialog() {
        DatePickerDialog dpd = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            dpd = new DatePickerDialog(MainActivity.this);
            dpd.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                    // here show picked date as you want
                    String date = "year " + i + "\nmonth " + i1 + "\nday " + i2;
                    dateEditTxt.setText(date);
                }
            });
        }

        if (dpd != null)
            dpd.show();
    }

    //DISPLAY INPUT DIALOG
    private void displayInputDialog() {
        Dialog d = new Dialog(this);
        d.setTitle("Add New Request");
        d.setContentView(R.layout.input_dialog);
        nameEditTxt = (EditText) d.findViewById(R.id.nameEditText);
        destinationEditTxt = (EditText) d.findViewById(R.id.destinationEditText);
        originEditTxt = (EditText) d.findViewById(R.id.originEditText);
        dateEditTxt = (EditText) d.findViewById(R.id.dateEditText);
        Button saveBtn = (Button) d.findViewById(R.id.saveBtn);


        dateEditTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPickerDialog();
            }
        });
        //SAVE
        saveBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //GET DATA
                String name = nameEditTxt.getText().toString();
                String destination = destinationEditTxt.getText().toString();
                String origin = originEditTxt.getText().toString();
                String date = dateEditTxt.getText().toString();
                //SET DATA
                Request s = new Request();
                s.setName(name);
                s.setOrigin(origin);
                s.setDestination(destination);
                s.setDate(date);

                //SIMPLE VALIDATION

                if (name != null && name.length() > 0) {
                    //THEN SAVE
                    if (helper.save(s)) {
                        //IF SAVED CLEAR EDITXT
                        dateEditTxt.setText("");
                        originEditTxt.setText("");
                        destinationEditTxt.setText("");
                        dateEditTxt.setText("");

                        adapter = new CustomAdapter(MainActivity.this, helper.retrieve());
                        lv.setAdapter(adapter);
                    }
                } else {
                    Toast.makeText(MainActivity.this, "No empty fills", Toast.LENGTH_SHORT).show();
                }
            }
        });
        d.show();
    }}


What I have tried:

Tried to search tutorials but i couldn't really understand and some solution wouldn't work
Posted
Updated 19-Aug-16 2:11am

1 solution

See Fragments | Android Developers[^]. Start by creating a simple test so you can see how it works. You may need to redesign your existing application in order to use this feature.

See also Creating a Navigation Drawer | Android Developers[^].
   
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900