Activity onCreate is Called Infinitely After Screen Rotation to Landscape - android

I created an Android app with custom Toolbar and NavigationDrawer. I used Android API 22 and tested on Nexus 5 (Android 5.1.0). When I started the app in landscape screen (and also rotate the app from portrait to landscape), the app always showed blinking infinitely. Although, I rotated to portrait, it kept blinking. I ran debugger and found that Activity.onCreate was always called repeatedly.
This is my code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyTheme.NoActionBar">
<activity
android:name=".activity.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.SettingsActivity"
android:configChanges="locale"
android:label="#string/settings"
android:parentActivityName=".activity.MainActivity"
android:theme="#style/MyTheme.Light">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity" />
</activity>
</application>
</manifest>
MainActivity.java
package com.example.activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.example.R;
import com.example.nav.NavDrawerItem;
import com.example.nav.NavDrawerListAdapter;
import com.example.util.VersionUtil;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private Toolbar mToolbar;
private DrawerLayout mDrawerLayout;
private LinearLayout mDrawerRoot;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private NavDrawerListAdapter mDrawerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
mDrawerRoot = (LinearLayout) findViewById(R.id.drawer_root);
initNavigationDrawer();
}
protected void initNavigationDrawer() {
if (mToolbar != null) {
setSupportActionBar(mToolbar);
}
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
String[] navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
TypedArray navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
TypedArray navMenuSelectedIcons = getResources().obtainTypedArray(R.array.nav_drawer_selected_icons);
ArrayList<NavDrawerItem> navDrawerItems = new ArrayList<>();
for (int i = 0, len = navMenuTitles.length; i < len; i++) {
navDrawerItems.add(new NavDrawerItem(navMenuTitles[i], navMenuIcons.getResourceId(i, -1), navMenuSelectedIcons.getResourceId(i, -1), false));
}
// Recycle the typed array
navMenuIcons.recycle();
navMenuSelectedIcons.recycle();
// setting the nav drawer list adapter
mDrawerAdapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
mDrawerList.setAdapter(mDrawerAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
selectDrawerMenuItem(0);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void setTitle(CharSequence title) {
if (mToolbar != null) {
mToolbar.setTitle(title);
} else {
super.setTitle(title);
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectDrawerMenuItem(position);
}
}
private void selectDrawerMenuItem(int position) {
switch (position) {
case 3: // Settings
showSettings();
break;
case 4: // Help and Feedback
sendHelpAndFeedbackEmail();
break;
default:
setSelectedNavItemMenu(position);
break;
}
mDrawerLayout.closeDrawer(mDrawerRoot);
}
private void setSelectedNavItemMenu(int position) {
for (int i = 0, size = mDrawerAdapter.getCount(); i < size; i++) {
NavDrawerItem menuItem = (NavDrawerItem) mDrawerAdapter.getItem(i);
menuItem.setSelected(i == position);
}
mDrawerList.setItemChecked(position, true);
}
private void showSettings() {
Intent settingsIntent = new Intent(this, SettingsActivity.class);
startActivity(settingsIntent);
}
private void sendHelpAndFeedbackEmail() {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setType("text/plain");
String versionName = VersionUtil.getVersionName(this);
int versionCode = VersionUtil.getVersionCode(this);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, String.format(getString(R.string.email_help_subject), getText(R.string.app_name), versionName, versionCode));
emailIntent.setData(Uri.parse("mailto:" + getText(R.string.email_send_to)));
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(emailIntent, getText(R.string.email_chooser_title)));
}
}
Why was the app blinking? Is there any solution to this problem?
Thanks.
EDIT #1
This was from Logcat:
...
03-22 22:50:51.716 31125-31125/com.example W/View﹕ requestLayout() improperly called by android.widget.ListView{654f6b5 VFED.VC. ......ID 0,75-912,1080 #7f0b0042 app:id/drawer_list} during layout: running second layout pass
03-22 22:50:51.821 31125-31125/com.example W/View﹕ requestLayout() improperly called by android.widget.ListView{3df1b0fa VFED.VC. ......ID 0,75-912,1080 #7f0b0042 app:id/drawer_list} during layout: running second layout pass
03-22 22:50:51.918 31125-31125/com.example W/View﹕ requestLayout() improperly called by android.widget.ListView{7b0a09b VFED.VC. ......ID 0,75-912,1080 #7f0b0042 app:id/drawer_list} during layout: running second layout pass
...
Those errors were repeated.
EDIT #2
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your normal content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- We use a Toolbar so that our drawer can be displayed
in front of the action bar -->
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<!-- The rest of your content view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Activity Content"
android:textColor="#000" />
</RelativeLayout>
</LinearLayout>
<!-- Your drawer view. This can be any view, LinearLayout
is just an example. As we have set fitSystemWindows=true
this will be displayed under the status bar. -->
<LinearLayout
android:id="#+id/drawer_root"
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:fitsSystemWindows="true">
<!-- Your drawer content -->
<ListView
android:id="#+id/drawer_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:divider="#null"
android:fastScrollEnabled="false"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
EDIT #3
NavDrawerListAdapter.java
import java.util.ArrayList;
import android.app.Activity;
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 com.example.R;
/**
* Created by edward on 01/03/2015.
*/
public class NavDrawerListAdapter extends BaseAdapter {
private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;
public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems) {
this.context = context;
this.navDrawerItems = navDrawerItems;
}
#Override
public int getCount() {
return navDrawerItems.size();
}
#Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.nav_list_item, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.nav_icon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.nav_title);
TextView txtCount = (TextView) convertView.findViewById(R.id.nav_counter);
NavDrawerItem item = navDrawerItems.get(position);
if (item.isSelected()) {
imgIcon.setImageResource(navDrawerItems.get(position).getIconSelectedResId());
} else {
imgIcon.setImageResource(navDrawerItems.get(position).getIconResId());
}
txtTitle.setText(item.getTitle());
if (navDrawerItems.get(position).isCounterVisible()) {
txtCount.setVisibility(View.VISIBLE);
txtCount.setText(navDrawerItems.get(position).getCount());
} else {
txtCount.setVisibility(View.GONE);
}
return convertView;
}
}
nav_list_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:background="#drawable/nav_background_selector"
android:minHeight="?android:attr/listPreferredItemHeightSmall">
<ImageView
android:id="#+id/nav_icon"
android:layout_width="#dimen/nav_icon_size"
android:layout_height="#dimen/nav_icon_size"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="24dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/nav_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/nav_icon"
android:gravity="center_vertical"
android:paddingRight="40dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/primary_text"
android:textStyle="bold"/>
<TextView
android:id="#+id/nav_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:background="#color/primary_light"
android:text="0"
android:textColor="#color/primary_text" />
</RelativeLayout>

Remove setContentView(R.layout.activity_main); from onConfigurationChanged() method
And if you are using this then remove it
android:fastScrollEnabled="true"
android:fastScrollAlwaysVisible="true"

Related

No view found for id 0x7f0c0071

I really tried to find something here that solves this problem but I didn't find anything...
MainActivity.java:
package de.thejakekols.brunoflo.teamgennewsql;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
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.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
SQLHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
db = new SQLHelper(this);
addItemsFromDb();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.addUserMenu) {
// AddUser addStudent = new AddUser();
// FragmentManager manager = getSupportFragmentManager();
// manager.beginTransaction().replace(R.id.content_main, addStudent).addToBackStack(null).commit();
getSupportFragmentManager().beginTransaction().add(R.id.content_main, new AddClass()).commit();
return true;
}else if(id == R.id.addClassMenu){
AddClass addClass = new AddClass();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_main, addClass).addToBackStack(null).commit();
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void addItemsFromDb() {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
final Menu menu = navigationView.getMenu();
ArrayList<String> klassen = db.getAllKlassen();
for (int i = 0; i < klassen.size(); i++) {
menu.add(klassen.get(i));
}
// refreshing navigation drawer adapter
for (int i = 0, count = navigationView.getChildCount(); i < count; i++) {
final View child = navigationView.getChildAt(i);
if (child != null && child instanceof ListView) {
final ListView menuView = (ListView) child;
final HeaderViewListAdapter adapter = (HeaderViewListAdapter) menuView.getAdapter();
final BaseAdapter wrapped = (BaseAdapter) adapter.getWrappedAdapter();
wrapped.notifyDataSetChanged();
}
}
}
}
And my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
And my app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="de.thejakekols.brunoflo.teamgennewsql.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"
android:id="#+id/include" />
</android.support.design.widget.CoordinatorLayout>
And my content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="de.thejakekols.brunoflo.teamgennewsql.MainActivity"
tools:showIn="#layout/app_bar_main">
</RelativeLayout>
And i'll only provide my AddUser.java, because the AddClass.java is nearly the same:
package de.thejakekols.brunoflo.teamgennewsql;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
/**
* A simple {#link Fragment} subclass.
*/
public class AddUser extends Fragment {
public AddUser() {
// Required empty public constructor
}
EditText vorname, nachname;
Button addstudent;
Spinner spinner;
SQLHelper db;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_add_user, container, false);
vorname = (EditText) view.findViewById(R.id.vorname);
nachname = (EditText) view.findViewById(R.id.nachname);
addstudent = (Button) view.findViewById(R.id.addStudent);
spinner = (Spinner) view.findViewById(R.id.spinner);
db = new SQLHelper(getActivity());
ArrayList<String> spinnerArray = db.getAllKlassen();
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
addstudent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vorname.setText("");
nachname.setText("");
if(db.addStudent(spinner.getSelectedItem().toString(), vorname.getText().toString(), nachname.getText().toString()) == true){
Toast.makeText(getContext(), "Der Benutzer " + vorname.getText().toString() + " " + nachname.getText().toString() + " wurde erfolgreich der Klasse " + spinner.getSelectedItem().toString() + " hinzugefügt", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getContext(), "Da ist was schief gelaufen!", Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}
my fragment_adduser.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.thejakekols.brunoflo.teamgennewsql.AddUser"
android:id="#+id/addStudentFragment">
<!-- TODO: Update blank fragment layout -->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/vorname"
android:hint="Vorname" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="#+id/vorname"
android:layout_alignParentStart="true"
android:id="#+id/nachname"
android:hint="Nachname" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/nachname"
android:layout_alignParentStart="true"
android:id="#+id/spinner" />
<Button
android:text="Schüler hinzufügen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner"
android:layout_alignParentEnd="true"
android:id="#+id/addStudent" />
</RelativeLayout>
I also tried it with a fresh fragment but it didn't worked aswell :(
Logcat Log:
12-1012:16:12.4904371-4371/de.thejakekols.brunoflo.teamgennewsqlE/FragmentManager:Noviewfoundforid0x7f0c0071(de.thejakekols.brunoflo.teamgennewsql:id/content_main)forfragmentAddClass{560ed4#0id=0x7f0c0071}
12-1012:16:12.4904371-4371/de.thejakekols.brunoflo.teamgennewsqlE/FragmentManager:Activitystate:
12-1012:16:12.4914371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:LocalFragmentActivity3469381State:
12-1012:16:12.4914371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mCreated=truemResumed=truemStopped=falsemReallyStopped=false
12-1012:16:12.4914371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mLoadersStarted=true
12-1012:16:12.4914371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:ActiveFragmentsin921187d:
12-1012:16:12.4914371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:#0:AddClass{560ed4#0id=0x7f0c0071}
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mFragmentId=#7f0c0071mContainerId=#7f0c0071mTag=null
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mState=1mIndex=0mWho=android:fragment:0mBackStackNesting=1
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mAdded=truemRemoving=falsemFromLayout=falsemInLayout=false
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mHidden=falsemDetached=falsemMenuVisible=truemHasMenu=false
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mRetainInstance=falsemRetaining=falsemUserVisibleHint=true
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mFragmentManager=FragmentManager{921187dinHostCallbacks{63bb372}}
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mHost=android.support.v4.app.FragmentActivity$HostCallbacks#63bb372
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:AddedFragments:
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:#0:AddClass{560ed4#0id=0x7f0c0071}
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:BackStackIndices:
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:#0:BackStackEntry{44453c3#0}
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:FragmentManagermiscstate:
12-1012:16:12.4924371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mHost=android.support.v4.app.FragmentActivity$HostCallbacks#63bb372
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mContainer=android.support.v4.app.FragmentActivity$HostCallbacks#63bb372
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:mCurState=5mStateSaved=falsemDestroyed=false
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:ViewHierarchy:
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:com.android.internal.policy.PhoneWindow$DecorView{e6f4440V.E........0,0-1080,1920}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.widget.LinearLayout{5abbf79V.E........0,0-1080,1794}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.view.ViewStub{f0d94beG.E........0,0-0,0#10203a9android:id/action_mode_bar_stub}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.widget.FrameLayout{50e691fV.E........0,0-1080,1794}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.FitWindowsLinearLayout{824646cV.E........0,0-1080,1794#7f0c0059app:id/action_bar_root}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.ViewStubCompat{59fda35G.E........0,0-0,0#7f0c005aapp:id/action_mode_bar_stub}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.ContentFrameLayout{38a3ecaV.E........0,0-1080,1794#1020002android:id/content}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v4.widget.DrawerLayout{b3a783bVFED....F..0,0-1080,1794#7f0c006dapp:id/drawer_layout}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.design.widget.CoordinatorLayout{1295b58V.ED.......0,0-1080,1794}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.design.widget.AppBarLayout{7c6f7baV.E........0,63-1080,210}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.Toolbar{680e4b1V.E........0,0-1080,147#7f0c006fapp:id/toolbar}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.AppCompatTextView{c757d96V.ED.......189,38-596,109}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.AppCompatImageButton{24e5d17VFED..C....0,0-147,147}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.ActionMenuView{284d504V.E........975,0-1080,147}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.ActionMenuPresenter$OverflowMenuButton{8e1aedVFED..C....0,10-105,136}
12-1012:16:12.4934371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.widget.RelativeLayout{30e566bV.E........0,210-1080,1794#7f0c0070app:id/include}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.design.widget.NavigationView{903dd22I.E........-735,0-0,1794#7f0c006eapp:id/nav_view}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.design.internal.NavigationMenuView{e51b3b3VFED.V.....0,0-735,1794#7f0c0077app:id/design_navigation_view}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.widget.LinearLayout{cfc3d70V.E........0,0-735,441#7f0c0076app:id/navigation_header_container}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.widget.LinearLayout{6be78e9V.E........0,0-735,420}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.AppCompatImageView{526a96eV.ED.......42,66-168,234#7f0c0084app:id/imageView}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.AppCompatTextView{229d80fV.ED.......42,234-693,327}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.support.v7.widget.AppCompatTextView{63fc09cV.ED.......42,327-519,378#7f0c0085app:id/textView}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/FragmentManager:android.view.View{a7cbaa5V.ED.......0,1794-1080,1920#1020030android:id/navigationBarBackground}
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlD/AndroidRuntime:ShuttingdownVM
12-1012:16:12.4944371-4371/de.thejakekols.brunoflo.teamgennewsqlE/AndroidRuntime:FATALEXCEPTION:main
Process:de.thejakekols.brunoflo.teamgennewsql,PID:4371
java.lang.IllegalArgumentException:Noviewfoundforid0x7f0c0071(de.thejakekols.brunoflo.teamgennewsql:id/content_main)forfragmentAddClass{560ed4#0id=0x7f0c0071}
atandroid.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
atandroid.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
atandroid.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
atandroid.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
atandroid.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
atandroid.os.Handler.handleCallback(Handler.java:739)
atandroid.os.Handler.dispatchMessage(Handler.java:95)
atandroid.os.Looper.loop(Looper.java:148)
atandroid.app.ActivityThread.main(ActivityThread.java:5417)
atjava.lang.reflect.Method.invoke(NativeMethod)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-1012:16:12.4941555-1962/system_processW/ActivityManager:Forcefinishingactivityde.thejakekols.brunoflo.teamgennewsql/.MainActivity
12-1012:16:12.4981214-1616/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x333implycreationofhostcolorbuffer
[12-1012:16:12.5011555:1962D/]
HostConnection::get()NewHostConnectionestablished0x7fc54b5b7360,tid1962
12-1012:16:12.5111255-1621/?D/AudioFlinger:mixer(0xf44c0000)throttleend:throttletime(55)
12-1012:16:12.5131214-1214/?E/EGL_emulation:tid1214:eglCreateSyncKHR(1660):error0x3004(EGL_BAD_ATTRIBUTE)
12-1012:16:12.5771555-1962/system_processD/gralloc_ranchu:gralloc_unregister_buffer:exitingHostConnection(isbuffer-handlingthread)
12-1012:16:12.6711555-1606/system_processI/OpenGLRenderer:InitializedEGL,version1.4
12-1012:16:12.6881214-1617/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:12.6941555-1606/system_processE/EGL_emulation:tid1606:eglSurfaceAttrib(1165):error0x3009(EGL_BAD_MATCH)
12-1012:16:12.6941555-1606/system_processW/OpenGLRenderer:FailedtosetEGL_SWAP_BEHAVIORonsurface0x7fc54b53fac0,error=EGL_BAD_MATCH
12-1012:16:12.6961214-1329/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:12.6981214-1329/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:13.0841555-1569/system_processW/ActivityManager:ActivitypausetimeoutforActivityRecord{32c16b2u0de.thejakekols.brunoflo.teamgennewsql/.MainActivityt338f}
12-1012:16:13.1171214-1579/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:13.1251950-2080/com.android.launcher3E/EGL_emulation:tid2080:eglSurfaceAttrib(1165):error0x3009(EGL_BAD_MATCH)
12-1012:16:13.1251950-2080/com.android.launcher3W/OpenGLRenderer:FailedtosetEGL_SWAP_BEHAVIORonsurface0x7fc5574928c0,error=EGL_BAD_MATCH
12-1012:16:13.1261214-1329/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:13.1311214-1329/?D/gralloc_ranchu:gralloc_alloc:format1andusage0x900implycreationofhostcolorbuffer
12-1012:16:13.6661950-2080/com.android.launcher3W/OpenGLRenderer:IncorrectlycalledbuildLayeronView:ShortcutAndWidgetContainer,destroyinglayer...
12-1012:16:13.6661950-2080/com.android.launcher3W/OpenGLRenderer:IncorrectlycalledbuildLayeronView:ShortcutAndWidgetContainer,destroyinglayer...
12-1012:16:14.8624371-4371/de.thejakekols.brunoflo.teamgennewsqlI/Process:Sendingsignal.PID:4371SIG:9
12-1012:16:14.8781555-1606/system_processE/Surface:getSlotFromBufferLocked:unknownbuffer:0x7fc548597ce0
12-1012:16:14.8871555-1708/system_processE/JavaBinder:!!!FAILEDBINDERTRANSACTION!!!(parcelsize=104)
12-1012:16:14.8871555-1708/system_processW/InputMethodManagerService:GotRemoteExceptionsendingsetActive(false)notificationtopid4371uid10041
12-1012:16:14.8891555-1708/system_processE/JavaBinder:!!!FAILEDBINDERTRANSACTION!!!(parcelsize=104)
12-1012:16:14.9081255-1621/?D/AudioFlinger:mixer(0xf44c0000)throttleend:throttletime(11)
12-1012:16:14.9221555-1625/system_processD/GraphicsStats:Buffercount:3
12-1012:16:14.9221555-1917/system_processI/WindowState:WINDEATH:Window{7880644u0de.thejakekols.brunoflo.teamgennewsql/de.thejakekols.brunoflo.teamgennewsql.MainActivity}
12-1012:16:14.9221555-1917/system_processW/WindowManager:Force-removingchildwinWindow{662c9bau0PopupWindow:81e55c2}fromcontainerWindow{7880644u0de.thejakekols.brunoflo.teamgennewsql/de.thejakekols.brunoflo.teamgennewsql.MainActivity}
12-1012:16:14.9261555-1625/system_processW/WindowManager:Failedlookingupwindow
java.lang.IllegalArgumentException:Requestedwindowandroid.os.BinderProxy#408fae5doesnotexist
atcom.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8733)
atcom.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8724)
atcom.android.server.wm.WindowState$DeathRecipient.binderDied(WindowState.java:1209)
atandroid.os.BinderProxy.sendDeathNotice(Binder.java:558)
12-1012:16:14.9261555-1625/system_processI/WindowState:WINDEATH:null
I'm new into Android :)
The ID specified on an <include> tag overrides the ID of the root View in the inflated layout. This means that the RelativeLayout you're using to hold your Fragment's View does not have ID content_main after inflation. It has ID include, so the FragmentManager can't find the ViewGroup for the ID specified in the FragmentTransaction.
You can see this in your hierarchy listing:
android.widget.RelativeLayout{30e566b V.E..... ... 0,210-1080,1794 #7f0c0070 app:id/include}
Simply remove android:id="#+id/include" from the <include> tag in the app_bar_main layout.
I thing you have done mistake in the code
final View view = inflater.inflate(R.layout.fragment_add_user, container, false);
Please check the inflated layout is correct.

Text Visible in other Activities,

i just needed some quick help. I got a nav drawer setup, and i use activity main as my welcome screen, so I put a text box on the activity_main, but the problem is that when I chose other nav drawer pages, i see the text box from the activity main.
What one of my activity pages should look like: http://puu.sh/nQFkc/1ed83811cc.png
What it ends up looking like: http://puu.sh/nQF93/785c9eee45.png
Need to mention im new to android?
Main Activity
package nota.outlawsindex;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Toolbar toolbar;
Scrollview scrollView;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
android.support.v7.app.ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
setupToolbar();
DataModel[] drawerItem = new DataModel[4];//need to update this if you add start counting at 0
drawerItem[0] = new DataModel(R.drawable.ic_connect, "Calculate Sentence");
drawerItem[1] = new DataModel(R.drawable.ic_fixtures, "Felonies");
drawerItem[2] = new DataModel(R.drawable.ic_table, "Misdemeanors");
drawerItem[3] = new DataModel(R.drawable.ic_drawer, "Infractions");
//add on to the list to create more pages
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.list_view_item_row, drawerItem);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerListener(mDrawerToggle);
setupDrawerToggle();
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
scrollView.setVisibility(View.GONE);
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ConnectFragment();
break;
case 1:
fragment = new FixturesFragment();
break;
case 2:
fragment = new TableFragment();
break;
case 3:
fragment = new SelfAddedFragment();
break;
//add more cases if you add more pages
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
#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) {
//
int id = item.getItemId();
//
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else if (id == R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show();
return true;
}
Toast.makeText(getApplicationContext(), "Search", Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
void setupToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//getSupportActionBar().setDisplayShowHomeEnabled(true);
}
void setupDrawerToggle() {
mDrawerToggle = new android.support.v7.app.ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name);
//This is necessary to change the icon of the Drawer Toggle upon state change.
mDrawerToggle.syncState();
}
}
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"
android:background="#color/backgroundColor">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/relativeLayout"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RelativeLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</RelativeLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/container_toolbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</FrameLayout>
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"
android:layout_gravity="right|top"
android:layout_below="#+id/scrollView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/content_frame"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/relativeLayout2"
android:focusable="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="? android:attr/textAppearanceSmall"
android:text=" Welcome to The Outlaw&apos;s Index! An app designed for individuals who want to educate themselves with knowledge regarding different types of offenses in the United States. Currently this app is directed towards individuals in the United States, but a Canadian version may be released in upcoming months. This app gives users the tools and resources to research different classes of offenses and the repercussions one would receive if convicted. Although this app is informative, advice from law enforcement, attorneys, and other members of the law should be considered more accurate as it may suit your personal needs regarding a specific case. To start off, choose any of the classes on the left hand side, or calculate a sentence and the repercussions that would follow."
android:id="#+id/textView2"
android:textColor="#ffffff"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:password="false"
android:phoneNumber="false"
android:singleLine="false"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
</RelativeLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Welcome"
android:id="#+id/textView"
android:textColor="#ffffff"
android:layout_above="#+id/scrollView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp" />
</RelativeLayout>
</RelativeLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/menuBackgroundColor"
android:choiceMode="singleChoice"
android:divider="#color/colorAccent"
android:dividerHeight="1dp"
android:paddingTop="15dp"/>
<!-- because there is no header in this one I am using android:paddingTop="15dp"
to push the menu below the level of the translucent top bar.-->
One of my Navdrawer pages (Same one as in screenshots)
Fragment
package nota.outlawsindex;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class TableFragment extends Fragment {
Button MisdemeanorClassAButton = null;
Button MisdemeanorClassBButton = null;
Button MisdemeanorClassCButton = null;
Button MisdemeanorClassDButton = null;
TextView MisdemeanorText;
public TableFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_table, container, false);
init(rootView);
return rootView;
}
public void init(View view) {
MisdemeanorClassAButton = (Button) view.findViewById(R.id.MisdemeanorClassAButton);
MisdemeanorText = (TextView) view.findViewById(R.id.MisdemeanorText);
MisdemeanorClassAButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
MisdemeanorText.setText(R.string.MA);
}
}
);
MisdemeanorClassBButton = (Button) view.findViewById(R.id.MisdemeanorClassBButton);
MisdemeanorText = (TextView) view.findViewById(R.id.MisdemeanorText);
MisdemeanorClassBButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
MisdemeanorText.setText(R.string.MB);
}
}
);
MisdemeanorClassCButton = (Button) view.findViewById(R.id.MisdemeanorClassCButton);
MisdemeanorText = (TextView) view.findViewById(R.id.MisdemeanorText);
MisdemeanorClassCButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
MisdemeanorText.setText(R.string.MC);
}
}
);
MisdemeanorClassDButton = (Button) view.findViewById(R.id.MisdemeanorClassDButton);
MisdemeanorText = (TextView) view.findViewById(R.id.MisdemeanorText);
MisdemeanorClassDButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
MisdemeanorText.setText(R.string.MD);
}
}
);
}
}
The Activity Page of one of the NavDrawer Pages
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/backgroundColor">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/MTB"
android:id="#+id/MisdemeanorText"
android:textColor="#ffffff"
android:layout_row="6"
android:layout_column="1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class A"
android:id="#+id/MisdemeanorClassAButton"
android:layout_marginTop="34dp"
android:layout_row="0"
android:layout_column="0"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class B"
android:id="#+id/MisdemeanorClassBButton"
android:layout_row="0"
android:layout_column="1"
android:layout_alignTop="#+id/MisdemeanorClassAButton"
android:layout_toRightOf="#+id/MisdemeanorClassAButton"
android:layout_toEndOf="#+id/MisdemeanorClassAButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class C"
android:id="#+id/MisdemeanorClassCButton"
android:layout_alignTop="#+id/MisdemeanorClassBButton"
android:layout_toLeftOf="#+id/MisdemeanorClassDButton"
android:layout_toStartOf="#+id/MisdemeanorClassDButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class D"
android:id="#+id/MisdemeanorClassDButton"
android:layout_alignBottom="#+id/MisdemeanorClassCButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
The issue is with your layout.
Two approaches to fix your problem -
Whenever you instantiate any fragment by selecting any option from the navigation drawer, you can set ScrollView (containing the welcome text) visibility to GONE.
Scrollview scrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
scrollView = (ScrollView) findViewById(R.id.scrollView);
}
private void selectItem(int position) {
scrollView.setVisibility(View.GONE);
....
}
Create a one more fragment with the layout where you only show your welcome text and launch this fragment in onCreate() of your activity. So this will show the welcome text when user starts your app. and when user selects any item from Navdrawer, you can launch another fragment of your choice. But for this you should remove welcome text from activity layout xml.

Add a Drawer Layout to existing Map Fragment in Android

I already have a working Map fragment in my Activity.
<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">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_alignParentTop="true">
<android.support.design.widget.FloatingActionButton
android:id="#+id/add"
android:onClick="locate_me"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginTop="360dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:clickable="true"
android:src="#drawable/tick"
app:backgroundTint="#000"
android:layout_gravity="right|end"
app:layout_anchorGravity="right|end"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:onClick="locate_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="#drawable/mylocation"
app:backgroundTint="#000000"
android:layout_gravity="bottom|end"
app:layout_anchorGravity="right|end"/>
</fragment></android.support.v4.widget.DrawerLayout>
I want to add a Drawer Layout to this existing Activity. I have tried a lot but couldn't make it work. I have tried MapView as well. Can you give me some pointers? Thanks.
So this is pretty straight forward Here is a gist, with all the code below.
Dependency section of my build.gradle :
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.android.gms:play-services:7.8.0'
compile 'com.android.support:design:23.0.0'
}
XML file for my AppCompatActivity which includes the drawer layout and a SupportMapFragment, since I am using appcompat-v7 for this project:
<RelativeLayout 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"
tools:context=".MainActivity">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="16dp"
android:clickable="true"
android:src="#drawable/ic_maps_navigation"
app:backgroundTint="#777"
android:layout_gravity="bottom|end"/>
</FrameLayout>
<FrameLayout
android:id="#+id/nav_container"
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="fill_parent"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Then I have a single activity:
package com.test.mapdrawer;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity implements DrawerFragment.NavCallback {
private DrawerLayout drawer;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer);
setupDrawerLayout();
if(savedInstanceState == null) {
/*
Load the fragment for the drawer
*/
getSupportFragmentManager()
.beginTransaction()
.add(R.id.nav_container, DrawerFragment.newInstance(), "Drawer")
.commit();
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.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.
int id = item.getItemId();
// Handle the drawer Actions
if(mDrawerToggle.onOptionsItemSelected(item)){
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupDrawerLayout(){
// Instantiate the Drawer Toggle
mDrawerToggle = new ActionBarDrawerToggle(this, drawer, R.string.app_name, R.string.app_name){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
getSupportActionBar().setTitle(getString(R.string.app_name));
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
getSupportActionBar().setTitle(getString(R.string.app_name));
}
};
// Set the Toggle on the Drawer, And tell the Action Bar Up Icon to show
drawer.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public void onNavSelected(int position) {
Toast.makeText(this, "Selected item: "+ position + " from nav", Toast.LENGTH_SHORT).show();
}
}
And finally your typical Manifest for a Map project:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.mapdrawer" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="<YOUR_MAPS_API_KEY_HERE>"/>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Then you want to create and populate the Navigation Drawer Fragment:
package com.test.mapdrawer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class DrawerFragment extends Fragment {
private static final String [] NAV_ITEMS = {"Home", "Nav Item 2", " Nav Item 3", "Nav Item 4"};
private ListView mListView;
private NavCallback mCallback;
public interface NavCallback{
void onNavSelected(int position);
}
/**
* Create a static method to return this fragment
* #return - this fragment
*/
public static DrawerFragment newInstance(){
return new DrawerFragment();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallback = (MainActivity) activity;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.drawer_fragment, container, false);
/*
You can really use anything you want here but for simplicity lest assume ListView
*/
mListView = (ListView) view.findViewById(R.id.listViewNav);
mListView.setOnItemClickListener(ListListener);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Simple adapter, Also this is for simplicity and adapter can be used
mListView.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, NAV_ITEMS));
}
private final AdapterView.OnItemClickListener ListListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mCallback.onNavSelected(position);
}
};
}
Which has layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#android:color/black">
<ListView
android:id="#+id/listViewNav"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Please note I have updated 2 files for this Change and added 2
MainActivity -> now implements interface and onCreate contains logic to load fragment
activity_main.xml -> FrameLayout background for drawer is removed
New Drawerfragment
New drawer_fragment.xml
Also, the gist has been update to reflect these changes, happy coding!

Custom Drawer Layout for Slide Over Action Bar

My requirement is i need the functionality of Navigation Drawer (Navigation Menu should appear both by clicking on the toggle icon and also dragging from margin) + Drawer layout on top of the action bar.
Check this post, i want the similar action.
I had gone through many post regarding this in SO itself, most of them saying to use a third-party library to use to get this done. But i don't want to use, instead in One SO question CommonsWare said like this can be done by tweaking the Drawerlayout.
How to achieve this?
Note: I don't want to use external library as it was creating problems.
In Android Default you cannot move the DrawerLayout along with the Action Bar. However if your are keen on using the Default Navigation Drawer. Hide the Action bar and create a Top layout similar to action bar. It will move with the drawerLayout. If you want further help code wise let me know.
Find my updated answer
MainActivity.java
package com.example.android.navigationdrawerexample;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
ImageView menubtn, addbtn;
LinearLayout menuLayout;
RelativeLayout frame;
TranslateAnimation anim;
float moveFactor, lastTranslate = 0.0f;
ListView accList;
String[] menuValues = { "Add", "View" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment())
.commit();
}
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
menuLayout = (LinearLayout) findViewById(R.id.menu);
accList = (ListView) findViewById(R.id.drawer_list);
frame = (RelativeLayout) findViewById(R.id.rl_main);
menubtn = (ImageView) findViewById(R.id.menu_btn);
addbtn = (ImageView) findViewById(R.id.add_btn);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, menuValues);
accList.setAdapter(adapter);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, R.string.open, R.string.close) {
public void onDrawerClosed(View view) {
}
public void onDrawerOpened(View drawerview) {
// adapter = new AccountAdapter(this, R.layout.row_acc, values);
}
#SuppressLint("NewApi")
public void onDrawerSlide(View drawerView, float slideOffset) {
// use this code only if you need the fragment to slide over, if you want the
// drawerlayout to be above the main screen then ignore this code.
//moveFactor = (menuLayout.getWidth() * slideOffset);
//drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
// Gravity.LEFT);
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// frame.setTranslationX(moveFactor);
//} else {
// anim = new TranslateAnimation(lastTranslate, moveFactor,
// 0.0f, 0.0f);
// anim.setDuration(0);
// anim.setFillAfter(true);
// frame.startAnimation(anim);
// lastTranslate = moveFactor;
//}
}
};
drawerLayout.setDrawerListener(drawerToggle);
menubtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (drawerLayout.isDrawerVisible(Gravity.LEFT)) {
return;
} else {
drawerLayout.openDrawer(Gravity.LEFT);
}
}
});
accList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
// Write your code
drawerLayout.closeDrawers();
}
}
});
addbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "Action Bar Icon code as per your requirement", Toast.LENGTH_LONG).show();
}
});
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container,
false);
return rootView;
}
}
}
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/rl_main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="40dp" >
<ImageView
android:id="#+id/menu_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:src="#drawable/ic_drawer" />
<ImageView
android:id="#+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:src="#android:drawable/ic_dialog_info"/>
</RelativeLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/top_layout" />
</RelativeLayout>
<!-- The navigation drawer -->
<LinearLayout
android:id="#+id/menu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#android:color/white"
android:orientation="vertical" >
<TextView
android:id="#+id/welcome_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:text="OPEN" />
<ListView
android:id="#+id/drawer_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:choiceMode="singleChoice"
android:divider="#android:color/white"
android:dividerHeight="2dp"
android:listSelector="#android:color/white" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
And another important part Please change your application theme to noActionbar. Let me know if this satisfies your requirements.
Have you checked the Navigation Drawer documentation already? You have to provide a layout for the navigation drawer anyways, so it's always custom and up to you how it looks like.

Android Navigation Drawer not being closed by dragging it

I'm trying to implement a Navigation Drawer and I managed successfully to integrate it with the Action Bar: it opens and closes when the application button is pressed and it closes when I press on a blank part of the screen, but it does not allow to be dragged back to its original position. Once opened, I can't close it with a swipe (as in every application I have seen so far).
Here is my Java code:
package com.example.notificationswhisperer;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity
extends Activity {
private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
private List<AppDrawerItem> drawerItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications_whisperer);
drawerItems = new ArrayList<AppDrawerItem>();
drawerItems.add(
new AppDrawerItem("Settings",
getResources().getDrawable(R.drawable.ic_action_settings)));
drawerItems.add(
new AppDrawerItem("Test",
getResources().getDrawable(R.drawable.ic_action_settings)));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.layout_app_drawer);
drawerToggle = new ActionBarDrawerToggle(
this, // Context
drawerLayout, // DrawerLayout object
R.drawable.ic_drawer, // Drawer icon
0, // Open Drawer description
0) { // Closed Drawer description
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
invalidateOptionsMenu();
}
public void onDrawerSlide(View view, float offset) {
if (offset > .5) {
onDrawerOpened(view);
} else {
onDrawerClosed(view);
}
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.START);
drawerList = (ListView) findViewById(R.id.list_app_drawer);
drawerList.setAdapter(new AppDrawerAdapter(this, drawerItems));
drawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
drawerList.setItemChecked(position, true);
drawerLayout.closeDrawer(drawerList);
}
});
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
drawerToggle.onConfigurationChanged(config);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.notifications_whisperer, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my layout for the activity:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout_app_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.notificationswhisperer.NotificationsWhisperer" >
<ListView
android:id="#+id/list_app_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/black"
android:dividerHeight="0.5dp"
android:background="#android:color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</android.support.v4.widget.DrawerLayout>
And this is for the List entries:
<?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="48dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/list_item_app_drawer_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="8dp"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/list_item_app_drawer_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/list_item_app_drawer_image"
android:textSize="18sp" />
</RelativeLayout>
Am I missing something? Thank you in advance.
ADDENDUM: Additionally, the back button won't close the Drawer.
I had the very same issue : the drawer could be opened by a swipe, but could only be closed by a click on the actionbar button.
After some research, it appeared that the underlying activity had several critical bugs : some fragments were added which were adding an un-initialized pager (with a null adapter)
Anyway, when I fixed these bugs (which had nothing to do with the drawer implementation), then behavior went correct.

Categories

Resources