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);
Related
I am new to android, I am trying to open URL image on my main screen. My problem is that I am using navigation drawer also on my main page. Everything works fine only my main image which is coming from database is going slightly upwards, whereas the example which I copied in that the image is looking perfectly.
I don't know why my image is going upwards. The image should render like this :
My image is rendering like this:
Here is my FragmentOne.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7">
<ImageView
android:id="#+id/ms1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3">
<ImageView
android:id="#+id/ms2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#mipmap/ic_launcher" />
<ImageView
android:id="#+id/ms3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
My containt_main.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">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
My Main activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
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 com.example.zeba.broccoli.AddToCart;
import com.example.zeba.broccoli.FragmentOne;
import com.example.zeba.broccoli.FragmentTwo;
import com.example.zeba.broccoli.Login;
import com.example.zeba.broccoli.R;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
/**
* As i told you i just create only navigation drawer with displaying images.
* I jus created two fragment only for understanding purpose.
* If you want more then developed by yourself.
* Remove this comment no need after you understood.
* #param savedInstanceState
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//add this line to display menu1 when the activity is loaded
displaySelectedScreen(0);
}
#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.menu_main
, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// User chose the "Settings" item, show the app settings UI...
case R.id.action_cart:
Intent ibs = new Intent(MainActivity.this,AddToCart.class);
ibs.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ibs);
finish();
break;
// User chose the "Favorite" action, mark the current item
// as a favorite...
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
return true;
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.br_menu) {
Intent i = new Intent(MainActivity.this,Login.class);
startActivity(i);
displaySelectedScreen(0);
}
else if (id == R.id.tr_ordr)
{
displaySelectedScreen(1);
}
else if (id == R.id.pl_ordr)
{
displaySelectedScreen(2);
} else if (id == R.id.profl)
{
displaySelectedScreen(3);
}
else if (id == R.id.addr)
{
displaySelectedScreen(4);
} else if (id == R.id.crds)
{
displaySelectedScreen(5);
}
return true;
}
private void displaySelectedScreen(int position)
{
try {
Fragment fragment = null;
if (position == 0)
{
fragment = new FragmentOne();
}
else if (position == 1)
{
fragment = new FragmentTwo();
}
else if (position == 2)
{
} else if (position == 3) {
} else if (position == 4) {
} else if (position == 5) {
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}catch (Exception e){
e.printStackTrace();
}
}
}
My FragmentOne.java
public class FragmentOne extends Fragment
{
Activity activity;
View rootView;
ImageView im1, im2, im3;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
rootView = inflater.inflate(R.layout.fragment_one, container, false);
activity = getActivity();
im1 = (ImageView)rootView.findViewById(R.id.ms1);
im2 = (ImageView)rootView.findViewById(R.id.ms2);
im2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Login.class);
startActivity(intent);
}
});
im3 = (ImageView)rootView.findViewById(R.id.ms3);
Picasso.with(activity).load("http://i.imgur.com/DvpvklR.png").into(im1);
Picasso.with(activity).load("https://api.learn2crack.com/android/images/donut.png").into(im2);
Picasso.with(activity).load("http://hdwallpaperbackgrounds.net/wp-content/uploads/2016/07/hd-nature-wallpapers.jpg").into(im3);
return rootView;
}
}
I tried doing debugging but then also no use, it's so weird problem.
The problem occur for main_content xml . your image is cut off when you use it in you main_activity xml . Use this trick it will work . Simple put margin above your main_content xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_marginTop="?actionBarSize"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
You have to include a custom action bar in your layout.
Please follow this link. It is explained very nicely with the code snaps and in depth. :)
In MainAcitivity you should extends Activity no an AppCompatActivity. AppCompatActivity show a bar
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 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}">
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.
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.