Reputation: 41
I have been fallowing on a touch + tutorials android beginners from a week, and i used android.support.v4.widget.Drawer Layout (drawer layout ) in my main.xml file. but its not working,i am not able to see drawer on toolbar.
error i am getiing is below
02-09 08:30:18.730 24972-24972/? D/dalvikvm: Late-enabling CheckJNI
02-09 08:30:18.742 24972-24972/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
02-09 08:30:18.742 24972-24972/? I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
02-09 08:30:18.742 24972-24972/? W/dalvikvm: VFY: unable to resolve interface method 17962: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
02-09 08:30:18.746 24972-24972/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
02-09 08:30:18.746 24972-24972/? I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
02-09 08:30:18.746 24972-24972/? W/dalvikvm: VFY: unable to resolve interface method 17966: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
02-09 08:30:18.746 24972-24972/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
02-09 08:30:18.754 24972-24978/? D/dalvikvm: Debugger has detached; object registry had 1 entries
02-09 08:30:18.758 24972-24972/? I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
02-09 08:30:18.758 24972-24972/? W/dalvikvm: VFY: unable to resolve virtual method 421: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-09 08:30:18.758 24972-24972/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-09 08:30:18.758 24972-24972/? I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
02-09 08:30:18.758 24972-24972/? W/dalvikvm: VFY: unable to resolve virtual method 443: Landroid/content/res/TypedArray;.getType (I)I
02-09 08:30:18.758 24972-24972/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-09 08:30:18.814 24972-24972/? D/dalvikvm: GC_FOR_ALLOC freed 134K, 11% free 2451K/2728K, paused 1ms, total 2ms
02-09 08:30:18.826 24972-24972/? W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
02-09 08:30:18.838 24972-24972/? D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so
02-09 08:30:18.842 24972-24972/? D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-09 08:30:18.842 24972-24972/? D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so
02-09 08:30:18.874 24972-24972/? W/EGL_genymotion: eglSurfaceAttrib not implemented
02-09 08:30:18.886 24972-24972/? D/OpenGLRenderer: Enabling debug mode 0
02-09 08:30:18.890 24972-24975/? D/dalvikvm: GC_CONCURRENT freed 45K, 7% free 2794K/3004K, paused 2ms+0ms, total 3ms
02-09 08:53:28.198 24972-24975/com.example.lucy.zoo D/dalvikvm: GC_CONCURRENT freed 379K, 16% free 2799K/3320K, paused 0ms+0ms, total 4ms
The code is good but I don't know what is the issue. Can anyone please look into my code and suggest me where does the problem lie. Thanks in advance.
public class MainActivity extends AppCompatActivity {
private DrawerLayout mdrawerlyout;
private ActionBarDrawerToggle mActionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mdrawerlyout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this,mdrawerlyout,R.string.drawer_opend,R.string.drawer_closed){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if(getSupportActionBar() != null) {
getSupportActionBar().setTitle(R.string.drawer_opend);
}
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if(getSupportActionBar() !=null) {
getSupportActionBar().setTitle(R.string.drawer_closed);
}
}
};
mdrawerlyout.setDrawerListener(mActionBarDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if(mActionBarDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Below is my layout file
<?xml version="1.0" encoding="utf-8"?>
<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.example.lucy.zoo.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<ListView
android:id="@+id/drawer"
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="match_parent"></ListView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Upvotes: 0
Views: 2393
Reputation: 6884
This is how you should implement it.
public class NavigationDrawerFragment extends Fragment {
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
private NavigationDrawerCallbacks mCallbacks;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ListView mDrawerListView;
private View mFragmentContainerView;
private int mCurrentSelectedPosition = 0;
private boolean mFromSavedInstanceState;
private boolean mUserLearnedDrawer;
public NavigationDrawerFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
if (savedInstanceState != null) {
mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
mFromSavedInstanceState = true;
}
selectItem(mCurrentSelectedPosition);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
new String[]{
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
public boolean isDrawerOpen() {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(),
mDrawerLayout,
R.drawable.ic_drawer,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).commit();
}
getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
};
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallbacks = (NavigationDrawerCallbacks) activity;
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
}
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void showGlobalContextActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setTitle(R.string.app_name);
}
private ActionBar getActionBar() {
return ((ActionBarActivity) getActivity()).getSupportActionBar();
}
public static interface NavigationDrawerCallbacks {
void onNavigationDrawerItemSelected(int position);
}
}
I have used the following tutorial to help me implement it. Hope it will help you too :)
Upvotes: 1
Reputation: 456
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/drawerrrrFolder"
>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
//your whole layout comes here
</RelativeLayout>
<!--Drawer LAyout-->
<RelativeLayout
android:id="@+id/mydrawerFolder"
android:layout_width="240dp"
android:layout_height="fill_parent"
android:background="#ffffff"
android:layout_gravity="start">
<RelativeLayout
android:id="@+id/sslayoutmydrawerFolder"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/searcboxxxmydrawerFolder"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="10dp"
>
<EditText
android:id="@+id/editsearchtextmydrawerFolder"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/drawwersearchback"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_toLeftOf="@+id/cancelsearchboxlayoutmydrawerFolder"
android:paddingLeft="50dp"/>
<RelativeLayout
android:id="@+id/cancelsearchboxlayoutmydrawerFolder"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/searchcancelicon"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/drawersearchicon"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<ListView
android:id="@+id/drawerlistitemsmydrawerFolder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/sslayoutmydrawerFolder"
android:divider="@null"
android:scrollbars="none"
>
</ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
hope you got this.
Upvotes: 0
Reputation: 273
First you have to fill items in list view than you have to assign list view to drawer layout.
to see this create new project from Android studio =>select Navigation drawer activity from Add activity to mobile window
Upvotes: 1