Users Online

· Guests Online: 108

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

113 Java Android Program to Demonstrate Navigation Drawer with Different Fragements in Android

Java Android Program to Demonstrate Navigation Drawer with Different Fragements in Android

Here is source code of the Program to Demonstrate Navigation Drawer with Different Fragements in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

 

Main Activity

package com.example.navigation_drawer1;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
 
@SuppressLint("NewApi")
public class MainActivity extends Activity {
    private String[] titles;
    private DrawerLayout layout_drawer;
    private ListView drawer_content;
    private ActionBarDrawerToggle action_bar_toggle;
    private CharSequence title;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        title = getActionBar().getTitle();
        titles = getResources().getStringArray(R.array.operating_systems);
        layout_drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer_content = (ListView) findViewById(R.id.left_drawer);
 
        drawer_content.setAdapter(new ArrayAdapter<String>(this,
                R.layout.fragment_layout, R.id.textView_fragment, titles));
        drawer_content.setOnItemClickListener(new DrawerItemClickListener());
 
        action_bar_toggle = new ActionBarDrawerToggle(this, /* Main Activity */
        layout_drawer, /* layput for navigation drawer */
        R.drawable.ic_drawer, R.string.open, /* on open drawer */
        R.string.close /* on close drawer */) {
 
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(title);
            }
 
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("Open Drawer");
            }
        };
 
        layout_drawer.setDrawerListener(action_bar_toggle);
 
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
 
    }
 
    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }
 
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        action_bar_toggle.syncState();
    }
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        action_bar_toggle.onConfigurationChanged(newConfig);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (action_bar_toggle.onOptionsItemSelected(item)) {
            return true;
        }
        switch (item.getItemId()) {
        case R.id.action_settings:
            Toast.makeText(this, "settings", Toast.LENGTH_LONG).show();
            break;
 
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
 
    private void selectItem(int position) {
        Class_Fragment fragment = new Class_Fragment();
        Bundle args = new Bundle();
        args.putInt(Class_Fragment.ARG_OS, position);
        fragment.setArguments(args);
        android.app.FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment).commit();
 
        drawer_content.setItemChecked(position, true);
        getActionBar().setTitle((titles[position]));
        layout_drawer.closeDrawer(drawer_content);
    }
 
}

Class Fragement

package com.example.navigation_drawer1;
 
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
class Class_Fragment extends Fragment {
    public static final String ARG_OS = "OS";
    private String string;
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout, null);
        TextView textView = (TextView) view
                .findViewById(R.id.textView_fragment);
        textView.setText(string);
        return view;
    }
 
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
 
    @Override
    public void setArguments(Bundle args) {
        string = args.getString(ARG_OS);
    }
}

Activity_Main

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#333"
        android:choiceMode="singleChoice"
        android:divider="#666"
        android:dividerHeight="1dp"
        android:paddingLeft="15sp"
        android:paddingRight="15sp" />
 
</android.support.v4.widget.DrawerLayout>

Layout_Fragment

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView_fragment"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:text="New Text" 
    android:textSize="30dp"
    android:textColor="@android:color/holo_green_dark"/>

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.72 seconds
10,825,562 unique visits