Search code examples
androidlistviewnavigation-drawer

Navigation Drawer Layout on all activities of the application


I have two activities - MainActivity for user login page, and the second activity displaying user details(projects made by user) in a ListView. I want to create a Navigation Drawer Layout that will appear in both activities. I found the solution here. But the problem is, my second activity extends ListActivity to display user details in ListView, so I cannot extend anything else. here is my second activity:

public class ProjectList extends ListActivity {
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
ListView listView;
JSONArray projects = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.project_list_inflator);
    listView = (ListView)findViewById(android.R.id.list);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
    listView.setAdapter(adapter);
    new HttpGetHandler().execute();
}



private class HttpGetHandler extends AsyncTask<String, Void, Void> {
    String jsonUrl = "some url";
    String imgUrl = "http://canvasflip.com/protected/app/elements/userElements/";
    @Override
    protected Void doInBackground(String... params) {
        HttpGet httpGet = new HttpGet(jsonUrl);
        //HttpGet httpGet1 = new HttpGet(imgUrl);
        try {
            HttpResponse httpResponse = MainActivity.httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream content = httpEntity.getContent();
            String result = convertToString(content);
            JSONObject jsonObject = new JSONObject(result);
            projects = jsonObject.getJSONArray("projects");
            ProjectList.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for(int i = 0; i<projects.length(); i++) {
                            JSONObject p = projects.getJSONObject(i);
                            String title = p.getString("title");
                            listItems.add(title);
                            adapter.notifyDataSetChanged();
                        }

                    }catch(Exception e) {
                        Log.d("MSG", e.toString());
                    }
                }
            });
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    public String convertToString(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine())!=null) {
            result += line;

        }
        inputStream.close();
        return result;
    }
 }
}

Solution

  • Just make a new Activity that has the source of Navigation drawer implementation

    like public class DrawerActivity extends ListActivity Override the DrawerLayout as

    protected DrawerLayout drawerLayout;

    in onCreate of your drawer Activity initialize the drawer layout as,

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout1);
    

    And let your MainActivity extends the DrawerAcivity.

    then replace the setContentView as given.

    //  setContentView(R.layout.activity_main);
        
        LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.activity_main, null, false);
        drawerLayout.addView(contentView, 0);