I have been trying to implement a Navigation Drawer in Android. I have been following this throughout the implementation. It seems that drawer_layout is not getting resolved from the R file. I can't seem to find where have I gone wrong. Here is my code:
MainActivity.java
package com.project.breadcrumbs.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
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.Button;
import android.widget.TextView;
import com.project.breadcrumbs.R;
import com.project.breadcrumbs.helper.SQLiteHandler;
import com.project.breadcrumbs.helper.SessionManager;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
private DrawerLayout mDrawerLayout;
private TextView txtName;
private TextView txtEmail;
private Button btnLogout;
private SQLiteHandler db;
private SessionManager session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.project.breadcrumbs.R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
txtName = (TextView) findViewById(R.id.name);
txtEmail = (TextView) findViewById(R.id.email);
btnLogout = (Button) findViewById(R.id.btnLogout);
// SqLite database handler
db = new SQLiteHandler(getApplicationContext());
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();
String name = user.get("name");
String email = user.get("email");
// Displaying the user details on the screen
txtName.setText(name);
txtEmail.setText(email);
// Logout button click event
btnLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
logoutUser();
}
});
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onDrawerItemSelected(View view, int position) {
}
/**
* Logging out the user. Will set isLoggedIn flag to false in shared
* preferences Clears the user data from sqlite users table
* */
private void logoutUser() {
session.setLogin(false);
db.deleteUsers();
// Launching the login activity
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.project.breadcrumbs.activity.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/welcome"
android:textSize="20dp" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#color/lbl_name"
android:textSize="24dp" />
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13dp" />
<Button
android:id="#+id/btnLogout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:background="#color/btn_logut_bg"
android:text="#string/btn_logout"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="15dp" />
</LinearLayout>
</fragment>
</android.support.v4.widget.DrawerLayout>
I have tried 'dependencies' cannot be applied to '(groovy.lang.Closure)' and have tried to clean and then rebuild the project.
In your DrawerLayout Add Drawer Id:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout"
tools:context="${relativePackage}.${activityClass}">
Related
I want to create a food menu using RecycleView by connecting to firebase, I have declared recycler_menu as variable, but when i type recycler_menu = (RecyclerView)findViewById(R.id.recycler_menu); there are error showing cannot resolve symbol R.id.recycler_menu. Need help on solving this error.I have added implementation 'com.android.support:recyclerview-v7:27.1.1' support library but still unable to solve this error.
Home.java
package com.example.liew.idelivery;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.support.design.widget.NavigationView;
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.widget.TextView;
import android.widget.Toast;
import com.example.liew.idelivery.Common.Common;
import com.example.liew.idelivery.Interface.ItemClickListener;
import com.example.liew.idelivery.Model.Category;
import com.example.liew.idelivery.ViewHolder.MenuViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class Home extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
FirebaseDatabase database;
DatabaseReference category;
TextView txtFullName;
RecyclerView recycler_menu;
RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Menu");
setSupportActionBar(toolbar);
//Init Firebase
database = FirebaseDatabase.getInstance();
category = database.getReference("Category");
TextView txtFullName;
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
//set name for user
View headerView = navigationView.getHeaderView(0);
txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
txtFullName.setText(Common.currentUser.getName());
// Load menu
recycler_menu = (RecyclerView)findViewById(R.id.recycler_menu);
recycler_menu.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recycler_menu.setLayoutManager(layoutManager);
loadMenu();
}
private void loadMenu() {
FirebaseRecyclerAdapter<Category,MenuViewHolder> adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class,R.layout.menu_item,MenuViewHolder.class,category){
#Override
protected void populateViewHolder(MenuViewHolder viewHolder, Category model, int position) {
viewHolder.txtMenuName.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.imageView);
final Category clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(Home.this,""+clickItem.getName(), Toast.LENGTH_SHORT).show();
}
});
}
};
recycler_menu.setAdapter(adapter);
}
#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.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_menu) {
// Handle the camera action
} else if (id == R.id.nav_cart) {
}else if (id == R.id.nav_orders) {
}else if (id == R.id.nav_logout) {
}else if (id == R.id.nav_share) {
}else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
MenuViewHolder.java
package com.example.liew.idelivery.ViewHolder;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.liew.idelivery.Interface.ItemClickListener;
import com.example.liew.idelivery.R;
public class MenuViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
public TextView txtMenuName;
public ImageView imageView;
private ItemClickListener itemClickListener;
public MenuViewHolder(View itemView){
super(itemView);
txtMenuName = (TextView)itemView.findViewById(R.id.menu_name);
imageView = (ImageView)itemView.findViewById(R.id.menu_image);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener){
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View view){
itemClickListener.onClick(view,getAdapterPosition(),false);
}
}
menu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardElevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/menu_image"
android:scaleType="centerCrop"
android:src="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/menu_name"
android:text="Name of Menu"
android:textAlignment="center"
android:textColor="#android:color/white"
android:background="#4f0e0d0e"
android:textSize="20sp"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
activity_home.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"
android:background="#drawable/background"
tools:openDrawer="start">
<include
layout="#layout/app_bar_home"
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_home"
android:background="#color/overlayBackground"
app:itemTextColor="#android:color/white"
app:itemIconTint="#android:color/white"
app:menu="#menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
menu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardElevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/menu_image"
android:scaleType="centerCrop"
android:src="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/menu_name"
android:text="Name of Menu"
android:textAlignment="center"
android:textColor="#android:color/white"
android:background="#4f0e0d0e"
android:textSize="20sp"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
content_home.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Home"
tools:showIn="#layout/app_bar_home">
<android.support.v7.widget.RecyclerView>
android:id="#+id/recycler_menu"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
Try this in your content_home.xml:
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_menu"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
What is wrong with your code is the > character is placed in the wrong position, your original code:
<android.support.v7.widget.RecyclerView>
android:id="#+id/recycler_menu"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
</android.support.v7.widget.RecyclerView>
findViewById searches for the ID "recycler_menu" in the xml-files.
This error appears if there is no ID like this in your xml-files.
As I see it right this ID isn't implemented in your XML-File. Maybe you have some more xml-Files where it is implemented? Please show the xml file where this ID is implemented
if you want to claim
recycler_menu =(RecyclerView)findViewById(R.id.recycler_menu);
You should create a xml file like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recycler_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
`
Code for getting reference is perfectly alright
recycler_menu = (RecyclerView)findViewById(R.id.recycler_menu);
Go to XML of activity_home and re-confirm that recyclerview has id with name
recycler_menu
like this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recycler_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
for more help kindly send XML of activity_home
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.
I want my scrollview to have its visibiility set to gone whne i chose one of my nav drawer pages, i tried to do it with some help but got nullpointerexecptions and i got errors "Cannot Resolve Method setVisibility(int)" and Scrollview cannot be resolved,
MainActivity
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"
android:visibility="visible">
<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'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" />
<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_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp" />
</RelativeLayout>
</ScrollView>
</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.-->
This is API 15, also im new to android development
Change this line:
ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
to this line:
scrollView = (ScrollView) findViewById(R.id.scrollView);
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'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.
can I get a second set of eyes on this. I'm trying to write a Signup activity and it keeps crashing on me. I've been struggling since last night. Can anyone point out what I'm doing wrong?
Here is the activity code
package mw.technology.app.ribbit;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class SignUpActivity extends Activity {
protected EditText mUsername;
protected EditText mPassword;
protected EditText mEmail;
protected Button mSignUpButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
mUsername = (EditText)findViewById(R.id.usernameField);
mPassword = (EditText)findViewById(R.id.passwordField);
mEmail = (EditText)findViewById(R.id.emailField);
mSignUpButton = (Button)findViewById(R.id.signupButton);
mSignUpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String username = mUsername.getText().toString();
String password = mPassword.getText().toString();
String email = mEmail.getText().toString();
username = username.trim();
password = password.trim();
email = email.trim();
if (username.isEmpty() || password.isEmpty() || email.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
builder.setMessage(R.string.signup_error_message)
.setTitle(R.string.signup_error_tittle)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
// create the new user!
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sign_up, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* 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_sign_up,
container, false);
return rootView;
}
}
}
And here is the layout code
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mw.technology.app.ribbit.SignUpActivity"
tools:ignore="MergeRootFrame" >
<RelativeLayout
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" >
<EditText
android:id="#+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/username_hint" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/userName"
android:ems="10"
android:hint="#string/password_hint"
android:inputType="textPassword" />
<EditText
android:id="#+id/emailField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/password"
android:ems="10"
android:hint="#string/email_hint"
android:inputType="textEmailAddress" />
<Button
android:id="#+id/signupButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/emailField"
android:text="#string/sign_up_button_label" />
</RelativeLayout>
</FrameLayout>
Thanks in advance and I apoligize for my current ineptitude.
Any thoughts?