1

Here is the code of my program which i used to implement the slide panel, but i want to make list view items in it. So how do i do it? Please help. Thanks in advance! MainActivity Code:

// Slide the Panel
    menuRightButton = (ImageView) findViewById(R.id.menuViewButton);
    menuRightButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!isExpanded) {
                isExpanded = true;
                // Expand

                FragmentManager fragmentManager = getFragmentManager();

                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                fragmentTransaction.replace(R.id.menuPanel,
                        new LeftMenuFragment());
                fragmentTransaction.commit();
                new ExpandAnimation(slidingPanel, panelWidth1,
                        Animation.RELATIVE_TO_SELF, 0.0f,
                        Animation.RELATIVE_TO_SELF, 0.55f, 0, 0.0f, 0, 0.0f);

            } else {
                isExpanded = false;
                // Collapse

                new CollapseAnimation(slidingPanel, panelWidth1,
                        TranslateAnimation.RELATIVE_TO_SELF, 0.55f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
                        0, 0.0f);

            }
        }
    });

Leftmenufragement.java

//Left Menu   
public class LeftMenuFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.leftmenu, container, false);
}

}

leftmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#2C323F"
 >

<ListView
    android:id="@+id/listview2"
    android:layout_width="250dp"
    android:layout_height="550dp"
    android:background="#32394A"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"/>

2 Answers2

0

Update Your LeftMenuFragment As :

public class LeftMenuFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

     // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.leftmenu, container, false);
    ListView menuList = (ListView) view.findViewById(R.id.listview2);
  String[] items = { "Home", "Setting"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, items);

    menuList .setAdapter(adapter); 
    return view;
}

}
Anjali
  • 1
  • 1
  • 13
  • 20
  • lets say i want few items in the menu list like "Home" , "settings", can you please let me know how do i do it in this code? thank you –  Jan 01 '15 at 12:45
  • i'm getting this error "cannot resolve constructor'ArrayAdapter'" –  Jan 01 '15 at 13:12
  • that because of context value in adapter constructor. I changed that. Check Again – Anjali Jan 01 '15 at 13:14
  • 1
    Thank you a lot it really helped me :D can you please answer one more of my post? please i will post the link –  Jan 01 '15 at 13:22
  • sorry i closed that link as these people over here won't help me out here is the link to the image that i wanted to implement to my app http://www.148apps.com/wp-content/uploads/2013/03/wally07.png –  Jan 01 '15 at 13:42
  • This ia nothing but sectioned. Header listview. In which you have to adr Condition in your list adapter to show background color according to current position of adapter – Anjali Jan 01 '15 at 14:00
  • i don't get it kindly can you please past any link or some example –  Jan 01 '15 at 14:07
  • Check this out. http://javatechig.com/android/listview-with-section-header-in-android – Anjali Jan 01 '15 at 14:20
  • what about random colour to the rows or separate shape for it? –  Jan 01 '15 at 14:28
  • Inside getview method of your adapter you have to add condition on the basis of position which will change the shade.if shades get repeated after particular item then use modulus function on list position in that case – Anjali Jan 01 '15 at 14:34
  • Miss!! can you please help me with this last issue please with my app? here i have managed to implement custom list view with array adapter but the string that i have created in the main activity is not showing up in list. here is the code::-- public class CustomAdapter extends ArrayAdapter { private LayoutInflater inflater; public CustomAdapter (Activity activity, String[] items){ super(activity, R.layout.custom_layout, items); inflater = activity.getWindow().getLayoutInflater(); } //next comment } –  Jan 04 '15 at 18:31
  • @Override public View getView(int position, View convertView, ViewGroup parent){ Random rnd = new Random(); int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); View rowView = inflater.inflate(R.layout.custom_layout, parent, false); rowView.setBackgroundColor(color); return rowView; } –  Jan 04 '15 at 18:33
  • Create another post with all code of your activity and adapter – Anjali Jan 05 '15 at 04:27
  • Miss i cant as these people will vote down and my points will go down i barely have 13... –  Jan 05 '15 at 04:31
  • How you are creating your string array? – Anjali Jan 05 '15 at 04:35
  • Just like you have shown, i have just changed the ArrayAdapter to ArrayAdapter adapter=new Custom_Left(getActivity(),ittems); –  Jan 05 '15 at 04:39
  • extend your CustomAdapter with BaseAdapter and pass your list to CustomAdapter – Anjali Jan 05 '15 at 04:44
  • miss, here is the post please look to it http://stackoverflow.com/questions/27773602/custom-listview-is-not-showing-up-the-string –  Jan 05 '15 at 04:52
0
DashboardActivity.java


    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.android.volley.AuthFailureError;
    import com.android.volley.NetworkError;
    import com.android.volley.NoConnectionError;
    import com.android.volley.ParseError;
    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.ServerError;
    import com.android.volley.TimeoutError;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.StringRequest;
    import com.android.volley.toolbox.Volley;
    import com.bumptech.glide.Glide;

    import org.json.JSONException;
    import org.json.JSONObject;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;

    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.OnClick;
    import de.hdodenhof.circleimageview.CircleImageView;

    public class DashboardActivity extends AppCompatActivity {
        @BindView(R.id.txt_userName)
        TextView txtUserName;
        @BindView(R.id.txt_city)
        TextView txtCity;
        @BindView(R.id.profile_image)
        CircleImageView profileImage;

        //HandburgerMenu

        private ListView mDrawerList;
        private LinearLayout mDrawerPane;
        private ActionBarDrawerToggle mDrawerToggle;
        private DrawerLayout mDrawerLayout;
        private ImageView toggle;
        private ImageView profile_image;
        private ArrayList<NavItem> mNavItems = new ArrayList<>();

        SessionManager mSessionManager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_dashboard);
            ButterKnife.bind(this);


            mSessionManager = new SessionManager(DashboardActivity.this);
            //HandburgerMenu ----------------------------------------------------------
            toggle = findViewById(R.id.toggle);

            mNavItems.add(new NavItem("Products", "", R.drawable.products));
            mNavItems.add(new NavItem("Saved Resume", "", R.drawable.my_resume));
            mNavItems.add(new NavItem("My Resume", "", R.drawable.my_resume));
            mNavItems.add(new NavItem("My Documents", "", R.drawable.my_docu));
            mNavItems.add(new NavItem("Interview Preparation", "", R.drawable.interview));
            mNavItems.add(new NavItem("About Us", "", R.drawable.about));
            mNavItems.add(new NavItem("My Blogs", "", R.drawable.blogs));
            mNavItems.add(new NavItem("All Blogs", "", R.drawable.blogs));
            mNavItems.add(new NavItem("Settings", "", R.drawable.settings));
            mNavItems.add(new NavItem("Logout", "", R.drawable.logout));

            // DrawerLayout
            mDrawerLayout = findViewById(R.id.drawerLayout);

            // Populate the Navigtion Drawer with options
            mDrawerPane = findViewById(R.id.drawerPane);
            mDrawerList = findViewById(R.id.navList);
            // mDrawerList.setDividerHeight(20);
            DrawerListAdapter adapter = new DrawerListAdapter(this, mNavItems);
            mDrawerList.setAdapter(adapter);

            // Drawer Item click listeners
            mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    selectItemFromDrawer(position);
                }
            });


            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
                @Override
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);

                    invalidateOptionsMenu();


                    //Called when drawer is opened

                }

                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);

                    //Called when drawer is Closed
                    invalidateOptionsMenu();
                }
            };

            mDrawerLayout.setDrawerListener(mDrawerToggle);


            toggle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                        mDrawerLayout.openDrawer(GravityCompat.START);


                        SetDrawerDetails();

                    } else {
                        mDrawerLayout.closeDrawer(mDrawerPane);
                    }
                }
            });
            //-------------------------------------------------------------------------------------------


            mDrawerList.setItemChecked(0, true);
            setTitle(mNavItems.get(0).mTitle);


        }



        //Method to set drawer details
        public void SetDrawerDetails() {

            txtUserName.setText(mSessionManager.getStringData("USERNAME") + " " + mSessionManager.getStringData("USER_SURNAME"));
            txtCity.setText(mSessionManager.getStringData("USER_CITY"));

            try {
                Log.e("USER_IMAGE", "USER_IMAGE " + mSessionManager.getStringData("USER_IMAGE"));
                Glide.with(this).load(mSessionManager.getStringData("USER_IMAGE")).into(profileImage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        /*
         * Called when a particular item from the navigation drawer
         * is selected.
         * */
        private void selectItemFromDrawer(int position) {
            Fragment fragment = null;
            Boolean IsInMeters = false;
            switch (position) {
                case 0:
                    // fragment = new ProductsFragment();
                    break;
                case 1:
                    //  fragment = new SavedResumeFragment();

                    break;
                case 2:
                    //   fragment = new MyResumeFragment();

                    break;
                case 3:
                    //  fragment = new MyDocFragment();


                    break;
                case 4:

                    //  fragment = new InteriewPreparationFragment();


                    break;
                case 5:
                    fragment = new AboutUsFragment();

                    break;
                case 6:
                    //   fragment = new BlogsFragment();

                    break;
                case 7:
                    //   fragment = new BlogsFragment("All");

                    break;
                case 8:
                    //      fragment = new ChangePasswordFragment();

                    break;
                case 9:

                    mSessionManager.removeData("USERID");
                    mSessionManager.removeData("USERNAME");
                    mSessionManager.removeData("USER_MIDDLE_NAME");
                    mSessionManager.removeData("USER_SURNAME");
                    mSessionManager.removeData("USER_FULLNAME");
                    mSessionManager.removeData("USER_ADDRESS");
                    mSessionManager.removeData("USER_MOBILE");
                    mSessionManager.removeData("USER_CITY");
                    mSessionManager.removeData("USER_STATE");
                    mSessionManager.removeData("USER_COUNTRY");
                    mSessionManager.removeData("USER_DOB");
                    mSessionManager.removeData("USER_EMAIL");
                    mSessionManager.removeData("USER_IMAGE");
                    mSessionManager.removeData("USER_STATUS");
                    //  startActivity(new Intent(DashboardActivity.this, LoginActivity.class));
                    finish();
                    break;
                default:

            }


            if (position != 9) {
                if (fragment != null) {
                    FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.mainContent, fragment)
                            .addToBackStack(mNavItems.get(position).mTitle)
                            .commit();
                }
            }

            mDrawerList.setItemChecked(position, true);
            setTitle(mNavItems.get(position).mTitle);

            // Close the drawer
            mDrawerLayout.closeDrawer(mDrawerPane);
        }


        @Override
        public void onBackPressed() {
            // super.onBackPressed();


            if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                mDrawerLayout.closeDrawer(GravityCompat.START);
            } else {

                FragmentManager fragmentManager = getFragmentManager();
                if (fragmentManager.getBackStackEntryCount() > 1) {

                    fragmentManager.popBackStack();
                } else {


                    finish();

                }
                Log.e("popping BACKSTRACK===> ", "" + fragmentManager.getBackStackEntryCount());
            }
        }


    }





DrawerListAdapter.java



    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    import java.util.ArrayList;

    public class DrawerListAdapter extends BaseAdapter {

        private Context mContext;
        private ArrayList<NavItem> mNavItems;

        public DrawerListAdapter(Context context, ArrayList<NavItem> navItems) {
            mContext = context;
            mNavItems = navItems;
        }

        @Override
        public int getCount() {
            return mNavItems.size();
        }

        @Override
        public Object getItem(int position) {
            return mNavItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.drawer_item, null);
            } else {
                view = convertView;
            }

            TextView titleView = view.findViewById(R.id.title);
            TextView subtitleView = view.findViewById(R.id.subTitle);
            ImageView iconView = view.findViewById(R.id.icon);

            titleView.setText(mNavItems.get(position).mTitle);
            subtitleView.setText(mNavItems.get(position).mSubtitle);
            iconView.setImageResource(mNavItems.get(position).mIcon);

            return view;
        }
    }


NavItem.java

    public class NavItem {
    public String mTitle;
    public String mSubtitle;
    public int mIcon;

    public NavItem(String title, String subtitle, int icon) {
        mTitle = title;
        mSubtitle = subtitle;
        mIcon = icon;
    }
    }



AboutUsFragment.java



    import android.app.Fragment;
    import android.app.ProgressDialog;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.Unbinder;

    import static com.dev.sidemenudemo.Constants.URL_BASE_WEB;


    public class AboutUsFragment extends Fragment {

        @BindView(R.id.webView)
        WebView webView;
        Unbinder unbinder;

        public AboutUsFragment() {
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_about_us, container, false);

            unbinder = ButterKnife.bind(this, view);


            webView.setWebViewClient(new WebViewClient());    //the lines of code added
            webView.setWebChromeClient(new WebChromeClient()); //same as above
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl(URL_BASE_WEB + "about-us");


            final ProgressDialog progressBar = new ProgressDialog(getActivity());
            progressBar.setMessage("Please wait...");


            webView.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    if (!progressBar.isShowing()) {
                        progressBar.show();
                    }
                }

                public void onPageFinished(WebView view, String url) {
                    if (progressBar.isShowing()) {
                        progressBar.dismiss();
                    }
                }

                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    if (progressBar.isShowing()) {
                        progressBar.dismiss();
                    }
                }
            });


            //To handle Webpage back in fragment
            webView.setOnKeyListener(new View.OnKeyListener() {

                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK
                            && event.getAction() == MotionEvent.ACTION_UP
                            && webView.canGoBack()) {
                        webView.goBack();
                        return true;
                    }
                    return false;
                }
            });


            return view;
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            unbinder.unbind();
        }
    }


activity_dashboard.xml


    <?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.mplussoft.android.jobscruze.Activity.DashboardActivity">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

        <!-- The main content view -->
        <RelativeLayout
            android:id="@+id/mainContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <!--

            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    -->


        </RelativeLayout>


    </LinearLayout>

    <!-- The navigation drawer -->
    <LinearLayout
        android:id="@+id/drawerPane"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <!-- Profile Box -->


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:background="@drawable/sidebar_bg"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/profile_image"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_margin="10dp"
                android:background="@mipmap/ic_launcher_round"
                android:padding="2dp"
                app:civ_border_color="@color/white"
                app:civ_border_width="2dp" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txt_userName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Johnson Doe"
                    android:textColor="@color/black"
                    android:textSize="@dimen/txtsize_normal"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/txt_city"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:text="Pune"
                    android:textSize="@dimen/txtsize_normal" />


            </LinearLayout>
        </LinearLayout>

        <!-- List of Actions (pages) -->
        <ListView
            android:id="@+id/navList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="30dp"
            android:scrollbars="none"
            android:background="#ffffffff"
            android:choiceMode="singleChoice"
            android:divider="@color/colorSidemenudivider"
            android:dividerHeight="1dp" />

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




drawer_item.xml


    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="30dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_marginRight="30dp"
    android:layout_marginTop="30dp">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/icon"
        android:gravity="center_vertical"
        android:textColor="@color/colorSidemenuText"
        android:textSize="@dimen/txtsize_normal" />

    <TextView
        android:id="@+id/subTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/title"
        android:layout_toRightOf="@+id/icon"
        android:visibility="gone" />

</RelativeLayout>


toolbar.xml


    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tool"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/colortoolbar"
       >

        <ImageView
            android:id="@+id/toggle"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="20dp"
            android:layout_centerVertical="true"
            android:scaleType="centerInside"
            android:src="@drawable/toggle" />

        <ImageView
            android:id="@+id/img_logo"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:scaleType="centerInside"
            android:src="@drawable/jobs_white" />

    </RelativeLayout>


</RelativeLayout>
Pratibha Sarode
  • 1,819
  • 17
  • 17