0

I have 3 activity say first_activity,second_activity,third_activity.I have used FrameLayout to open a slider .!

Now what I want to open the slider on click of button that is available on all three activities.

So now like in the Test 1 there is a button on the left side in the same way i have on all three activities so on click of that i want to open the slider.

This my slider code

public class MainActivity extends Activity {

    // Within which the entire activity is enclosed
    private DrawerLayout mDrawerLayout;

    // ListView represents Navigation Drawer
    private ListView mDrawerList;

    // ActionBarDrawerToggle indicates the presence of Navigation Drawer in the
    // action bar
    private ActionBarDrawerToggle mDrawerToggle;

    // Title of the action bar
    private String mTitle = "";

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = "Test App";
        getActionBar().setTitle(mTitle);

        // Getting reference to the DrawerLayout
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.drawer_list);

        // Getting reference to the ActionBarDrawerToggle
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close) {

            /** Called when drawer is closed */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu();
            }

            /** Called when a drawer is opened */
            public void onDrawerOpened(View drawerView) {
                // getActionBar().setTitle("Approval Status");
                invalidateOptionsMenu();
            }
        };

        // Setting DrawerToggle on DrawerLayout
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        // Creating an ArrayAdapter to add items to the listview mDrawerList
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getBaseContext(), R.layout.drawer_list_item, getResources()
                        .getStringArray(R.array.menus));

        // Setting the adapter on mDrawerList
        mDrawerList.setAdapter(adapter);

        // Enabling Home button
        getActionBar().setHomeButtonEnabled(true);

        // Enabling Up navigation
        getActionBar().setDisplayHomeAsUpEnabled(true);

        // Setting item click listener for the listview mDrawerList
        mDrawerList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // Getting an array of rivers
                String[] menuItems = getResources().getStringArray(
                        R.array.menus);

                // Currently selected river
                mTitle = menuItems[position];

                // Creating a fragment object
                // WebViewFragment rFragment = new WebViewFragment();
                //
                // // Passing selected item information to fragment
                // Bundle data = new Bundle();
                // data.putInt("position", position);
                // data.putString("url", getUrl(position));
                // rFragment.setArguments(data);
                //
                //
                // // Getting reference to the FragmentManager
                // FragmentManager fragmentManager = getFragmentManager();
                //
                // // Creating a fragment transaction
                // FragmentTransaction ft = fragmentManager.beginTransaction();
                //
                // // Adding a fragment to the fragment transaction
                // ft.replace(R.id.content_frame, rFragment);
                //
                // // Committing the transaction
                // ft.commit();

                // Closing the drawer
                mDrawerLayout.closeDrawer(mDrawerList);

            }
        });
    }

    protected String getUrl(int position) {
        switch (position) {
        case 0:
            return "";
        case 1:
            return "";
        case 2:
            return "";
        case 3:
            return "";
        case 4:
            return "";
        case 5:
            return "";
        case 6:
            return "";
        default:
            return "";
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /** Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Pooja Dubey
  • 683
  • 2
  • 14
  • 34

1 Answers1

0

You can't open a drawer in a different activity unless you have a drawer for that activity.

I would suggest you to use fragments.

Further before deciding on your design i suggest you take a look at this link first

http://developer.android.com/design/patterns/navigation.html

In oncreate

if (savedInstanceState == null){
            getUrl(0);
        }

In onItemClick call getUrl(position)

Then in getUrl

  protected String getUrl(int position) {

    FragmentTransaction fragTran = getFragmentManager()
                                 .beginTransaction();

    switch (position) {
    case 0:
            // make one a Fragment
            fragTran.replace(R.id.content_frame, fragment1);
        break;
    case 1:
           // second fragment
           fragTran.replace(R.id.content_frame, fragment2);

        break
    case 2:
          // third fragment
          fragTran.replace(R.id.content_frame, fragment3);  
        break;
    }
    // Commit the transaction and close the drawer
    fragTran.commit();  
    mDrawerList.setItemChecked(position, true);
    mDrawerLayout.closeDrawer(drawerList);

 }  

Example:

public class Fragment1 extends Fragment {
    String[] titles={"A","B","C"};
    @Override
    public View onCreateView(
            LayoutInflater inflater, 
            ViewGroup container, 
            Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment1, container, false);
        return rootView;
    }   
}

MainActivity.java

public class MainActivity extends Activity {

    // Fields -----------------------------------------------------------------
    private DrawerLayout drawerLayout;
    private ListView drawerList;
    private ActionBarDrawerToggle drawerToggle;
    private MenuListAdapter menuAdapter;
    private int[] icons;
    private Fragment fragment1;
    private Fragment fragment2;
    private Fragment fragment3;
    private CharSequence drawerTitle;
    private CharSequence title;
    private final String[] titles = new String[]{
            "Title Fragment #1",
            "Title Fragment #2",
            "Title Fragment #3"
    };
    private final String[] subtitles = new String[]{
            "Subtitle Fragment #1",
            "Subtitle Fragment #2",
            "Subtitle Fragment #3"
    };

    // Lifecycle Callbacks ----------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Base implemenation
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Instantiate the fragments
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();

        // Get the title from this activity
        title = drawerTitle = getTitle();

        // Get the icons from the drawables folder
        icons = new int[]{
                R.drawable.action_about,
                R.drawable.action_settings,
                R.drawable.collections_cloud
        };

        // Get the drawer layout from the XML file and the ListView inside it
        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        drawerList = (ListView)findViewById(R.id.listview_drawer);

        // Set a custom shadow over that overlays the main content
        // when the drawer opens
        drawerLayout.setDrawerShadow(
                R.drawable.drawer_shadow, GravityCompat.START);

        // Pass the string arrays to the MenuListAdapter, set the drawer
        // list adapter to it and set up its click listener
        menuAdapter = new MenuListAdapter(
                MainActivity.this, titles, subtitles, icons);
        drawerList.setAdapter(menuAdapter);
        drawerList.setOnItemClickListener(new DrawerItemClickListener());

        // Enable the action bar to have up navigation
        getActionBar().setHomeButtonEnabled(true);
        //getActionBar().setDisplayHomeAsUpEnabled(true);

        // Allow the the action bar to toggle the drawer
        drawerToggle = new ActionBarDrawerToggle(
                this,
                drawerLayout,
                R.drawable.ic_drawer,
                R.string.drawer_open,
                R.string.drawer_close){

            public void onDrawerClosed(View view){
                super.onDrawerClosed(view);
            }
            public void onDrawerOpened(View view){
                getSupportActionBar().setTitle(drawerTitle);
                super.onDrawerOpened(view);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        // If this is the first time opening this activity,
        // start with loading fragment #1
        if (savedInstanceState == null){
            selectItem(0);
        }       

    }

    // Methods ----------------------------------------------------------------
    @Override
    public boolean onOptionsItemSelected(MenuItem item){

        // If the user has pressed the action bar icon
        if (item.getItemId() == android.R.id.home){

            // If the drawer is open, close it; vice versa
            if (drawerLayout.isDrawerOpen(drawerList)){
                drawerLayout.closeDrawer(drawerList);
            } else {
                drawerLayout.openDrawer(drawerList);
            }
        }

        // Finish by letting the super class do the rest
        return super.onOptionsItemSelected(item);

    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState){

        // Call the super implementation and synchronize the drawer
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();

    }
    @Override
    public void onConfigurationChanged(Configuration newConfig){

        // Call the super implemenation on this activity
        // and the drawer toggle object
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);

    }
    private void selectItem(int position){

        // Create a new fragment transaction and start it
        FragmentTransaction fragTran = getFragmentManager()
                                       .beginTransaction();

        // Locate the position selected replace the content view
        // with the fragment of the number selected
        switch (position){
            case 0:{
                fragTran.replace(R.id.content_frame, fragment1);
                break;
            }
            case 1:{
                fragTran.replace(R.id.content_frame, fragment2);
                break;
            }
            case 2:{
                fragTran.replace(R.id.content_frame, fragment3);
                break;
            }
        }

        // Commit the transaction and close the drawer
        fragTran.commit();
        drawerList.setItemChecked(position, true);
        drawerLayout.closeDrawer(drawerList);

    }
    public void setTitle(CharSequence title){

        // Save the passed in title and set the action bar title
        this.title = title;
        getActionBar().setTitle(title);

    }

    // Classes ----------------------------------------------------------------
    private class DrawerItemClickListener 
    implements ListView.OnItemClickListener{

        @Override
        public void onItemClick(
                AdapterView<?> parent, 
                View view, 
                int position,
                long id) {

            // When clicked, select open the appropriate fragment
            selectItem(position);

        }

    }

}

Fragment2.java

public class Fragment2 extends Fragment {
    @Override
    public View onCreateView(
            LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment2, container, false);
        return rootView;
    }
}

Fragment3.java

public class Fragment3 extends Fragment {
    @Override
    public View onCreateView(
            LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment3, container, false);
        return rootView;
    }
}

activity_main.xml

<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/listview_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"/>

</android.support.v4.widget.DrawerLayout>

drawer_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:orientation="horizontal"
    style="?attr/dropdownListPreferredItemHeight" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical|left"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            style="?attr/spinnerDropDownItemStyle"/>
        <TextView 
            android:id="@+id/subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            style="?attr/spinnerDropDownItemStyle"/>

    </LinearLayout>    

</LinearLayout>

Have framen1.xml,framgent2.xml and fragment3.xml and customize the way you want.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • i am not getting this ..i have the slider code and i want that to be added in my activity for slider functionality .please explain more or for the reference i can mail u my demo project – Pooja Dubey Jan 07 '14 at 05:16
  • @PoojaDubey its not a slider in the first place. Its a NavigationDrawer. Its there for the `FirstActivtiy`. Now what is your requirement. I am not doing your homework. Big no to mailing. If you want the drawer in different activities then you need to have the code that you have in first activity in all the three activities. What you require is a fragment not activities – Raghunandan Jan 07 '14 at 05:18
  • See the issue i am facing that as i posted the image .on click of that white line i want to add the code of NavigationDrawer as i have posted in the question .and the second thing how could i call this from different activity – Pooja Dubey Jan 07 '14 at 05:20
  • @PoojaDubey pls look at this http://developer.android.com/design/patterns/navigation.html. You can't open the drawer in a different activity. You need to have the drawer code in different activities for that. Your design seems to be wrong. Use Fragments You can add fragments to the container. So its the same activity but a different fragment. You can change the title of the actionbar also. So according to me what you need is fragments not activities. Pls understand what you need first then code. Further more you can look at the gmail app which wil give you a better idea. – Raghunandan Jan 07 '14 at 05:23
  • and can u suggest me how could i do this in a single activity on a click of the image ?? – Pooja Dubey Jan 07 '14 at 05:26
  • @PoojaDubey use fragments instead of activities. – Raghunandan Jan 07 '14 at 05:27
  • will suggest me some thing on this bounty question http://stackoverflow.com/questions/20835135/set-text-on-a-gridview-image-in-android/20951239?noredirect=1#comment31493064_20951239 – Pooja Dubey Jan 07 '14 at 05:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44666/discussion-between-raghunandan-and-pooja-dubey) – Raghunandan Jan 07 '14 at 05:30
  • want to know i one thing if i have crated a fragment that have a list view now i want to use adapter class for that.So how could i use that – Pooja Dubey Jan 07 '14 at 11:58
  • @PoojaDubey Hey, Where are you yar? – Piyush Jun 09 '14 at 05:34