Error Occurs in implementing the Navigation drawer - android

I have one Login Activity if user click on login button than automatically its go to Navigation Drawer but i got an error. Please Help me out from this error.
MainActivity.java
import android.content.Intent;
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.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {
private Toolbar toolbar;
private DrawerLayout drawer;
public String emailRegistration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_business);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//toolbar.setTitle("toolbar");
setSupportActionBar(toolbar);
//Set adapter to AutoCompleteTextView
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
// if (!emailRegistration.matches("[a-zA-Z0-9._-]+#[a-z]+.[a-z]+")) {
//
// email.setError("Invalid Email Address");
//
// }
// fab.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// Snackbar.make(v, "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);
}
#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.action_settings) {
return true;
}
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_camera) {
setTitle("Home");
Intent i = new Intent(MainActivity.this, HomeFragment.class);
startActivity(i);
} else if (id == R.id.nav_gallery) {
setTitle("Edit Profile");
EditFragment editFragment = new EditFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment,editFragment).commit();
} else if (id == R.id.nav_slideshow) {
setTitle("Business");
BusinessFragment businessFragment = new BusinessFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment,businessFragment).commit();
} else if (id == R.id.nav_manage) {
setTitle("Message");
MessageFragment messageFragment = new MessageFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment,messageFragment).commit();
} 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;
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// TODO Auto-generated method stub
//Log.d("AutocompleteContacts", "onItemSelected() position " + position);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
InputMethodManager imm = (InputMethodManager) getSystemService(
INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// TODO Auto-generated method stub
// Show Alert
Toast.makeText(getBaseContext(), "Position:"+i+" Country:"+adapterView.getItemAtPosition(i),
Toast.LENGTH_LONG).show();
Log.d("AutocompleteContacts", "Position:"+i+" Country:"+adapterView.getItemAtPosition(i));
}
}
LoginActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
public EditText email, password;
public TextView textView;
public Button login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
email = (EditText) findViewById(R.id.email);
// if( email.getText().toString().length() == 0 )
// email.setError("Email is required!" );
password = (EditText) findViewById(R.id.text_input_password_toggle);
// if( password.getText().toString().length() == 0 )
// password.setError("Password is required!" );
textView = (TextView) findViewById(R.id.text2);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
});
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(LoginActivity.this,signupactivity.class);
startActivity(i);
}
});
}
}
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>
Error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.addDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference
SignupActivity. java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
public class signupactivity extends AppCompatActivity {
public EditText firstname, lastname, email;
private AutoCompleteTextView actv;
public Button signup;
String[] languages={"Indore","Ujjain","Bhopal","Delhi","Bangalore","Chennai"};
List<CharSequence> list = new ArrayList<CharSequence>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
for (int i=0;i<20;i++){
list.add("test " + i); // Add the item in the list
}
firstname = (EditText)findViewById(R.id.firstname);
lastname = (EditText)findViewById(R.id.lastname);
actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
email = (EditText) findViewById(R.id.email);
signup = (Button)findViewById(R.id.signup);
ArrayAdapter adapter = new
ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
actv.setAdapter(adapter);
actv.setThreshold(1);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(signupactivity.this, LoginActivity.class);
startActivity(i);
}
});
}
}
Image I want the blank fragment when i click on the home, edit profile but its show everywhere signup activity because my content_main framelayout id is fragment and its call in mainactivity and its my signup page too (fragmentManager.beginTransaction().replace(R.id.fragment,editFragment).commit();) So how to solve this problem

In your MainActivity.java you are using
setContentView(R.layout.fragment_business);
But your drawer is in activity_main.xml
So your drawer is a null and you cannot add a listener to a null object.
Change the setContentView to
setContentView(R.layout.activity_main);

Related

java.lang.RuntimeException: Canvas: trying to draw too large(125440000bytes) bitmap

Im new to Android Programming,I was trying to build Order Food App. I used Picasso to load Images. But when I try to run my app it shows:trying to draw too large bitmap.
Can someone please tell what's the error
Here is my source code
package com.andisofttechnology.myapplication.Model;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v7.app.AppCompatActivity;
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.LayoutInflater;
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.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.andisofttechnology.myapplication.Common.Common;
import com.andisofttechnology.myapplication.Interface.ItemClickListener;
import com.andisofttechnology.myapplication.R;
import com.andisofttechnology.myapplication.ViewHolder.MenuViewHolder;
import com.andisofttechnology.myapplication.model.Category;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
import java.util.Locale;
public class Home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
FirebaseDatabase database;
DatabaseReference category;
TextView txtFullName;
RecyclerView recycler_menu;
LinearLayoutManager 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");
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)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() {
FirebaseRecyclerOptions<Category> options =
new FirebaseRecyclerOptions.Builder<Category>()
.setQuery(MenuViewHolder, Category.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class,R.layout.menu_item,MenuViewHolder.class,category) {
#Override
public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.menu_item, parent, false);
return new MenuViewHolder(view);
}
#Override
protected void onBindViewHolder(MenuViewHolder holder, int position, Category model) {
holder.txtMenuName.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getName()).into(holder.imageView);
final Category clickItem = model;
holder.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) {
// 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);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
/* if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} 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;
}
}
........................................................................................................................................................................................

ListView didn't showing data in Fragment at Navigation Drawer

i had problem that my string-array not showing in listview at ListFragment, i didn't get any error, just the data of string-array not display. i create a navigation drawer that show data form another fragment
here my ListFragment
import android.os.Bundle;
import android.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class FragmentFood extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_food,container,false);
String[] food = getResources().getStringArray(R.array.food_array);
ArrayAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,food);
setListAdapter(adapter);
return rootView;
}
}
MainActivity.java
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
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.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends FragmentActivity
implements NavigationView.OnNavigationItemSelectedListener {
String nama;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
Fragment fragment = null ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
Intent intent = getIntent();
nama = intent.getStringExtra("Nama");
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0);
TextView tvNama = (TextView) headerView.findViewById(R.id.tv_name);
tvNama.setText(nama);
fragmentManager = getFragmentManager();
if (savedInstanceState == null) {
fragment = new FragmentFood();
callFragment(fragment);
}
/*FragmentFood fragmentFood = (FragmentFood) getSupportFragmentManager().findFragmentByTag("fragmentfood");
if (fragmentFood == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frame_content,null,"fragmentfood");
transaction.commit();
}*/
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.setDrawerListener(toggle);
toggle.syncState();
}
#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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
ListFragment listFragment=null;
int id = item.getItemId();
if (id == R.id.nav_food) {
listFragment = new FragmentFood();
callFragment(listFragment);
} else if (id == R.id.nav_dessert) {
} else if (id == R.id.nav_drink) {
/*fragment = new FragmentDrink();
callFragment(fragment);*/
} else if (id == R.id.nav_chartpayment) {
} else if (id == R.id.nav_status) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void callFragment(Fragment fragment) {
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragment);
fragmentTransaction.replace(R.id.frame_content, fragment);
fragmentTransaction.commit();
}
}
fragment_food.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"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
i dont have any idea to show data in ListFragment at NavigationDrawer

How to write the User name in the Navigation Drawer of a navigational activity From One of its Fragments?

HomeActivity.java (Navigational Activity)
import android.app.Fragment;
import android.app.FragmentManager;
mport android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
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.view.View;
import android.widget.TextView;
import com.example.dellpc.reuz_app.DTO.User;
import com.example.dellpc.reuz_app.Fragments.BrowseAdFragment;
import com.example.dellpc.reuz_app.Fragments.LoginFragment;
import com.example.dellpc.reuz_app.Fragments.NetworkErrorFragment;
import com.example.dellpc.reuz_app.Fragments.TopMainFragment;
import com.example.dellpc.reuz_app.R;
import com.example.dellpc.reuz_app.utils.CheckNetworkConnection;
import com.example.dellpc.reuz_app.utils.NetworkErrorFragmentSetter;
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Fragment fragment;
private String title;
private DrawerLayout drawer;
private NavigationView navigationView;
private View layout;
private User user=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle("REUZ");
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) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.bringToFront();
System.out.println(getSupportActionBar().getHeight());
View headerLayout = navigationView.getHeaderView(0);
layout=headerLayout.findViewById(R.id.navigationHeader);
getSupportActionBar().setTitle("REUZ");
this.setInitialFragment();
this.listeners();
/*this.getUserObject();*/
}
public void listeners(){
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(CheckNetworkConnection.isConnectionAvailable(HomeActivity.this))
{
getLoginFragment();
}
else{
fragment= NetworkErrorFragmentSetter.getNetworkErrorFragment(HomeActivity.this);
replaceFragment();
}
}
});
getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
Fragment currentFragment = getFragmentManager().findFragmentById(R.id.content_frame);
if (currentFragment instanceof TopMainFragment) {
getSupportActionBar().setTitle("REUZ");
}
else if (currentFragment instanceof BrowseAdFragment) {
getSupportActionBar().setTitle("All Categories");
}
else if (currentFragment instanceof LoginFragment) {
getSupportActionBar().setTitle("LOGIN");
}
else if(currentFragment instanceof NetworkErrorFragment){
getSupportActionBar().setTitle("NETWORK ERROR");
}
}
});
}
#Override
public void onBackPressed() {
//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) {
// 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);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the home action
setInitialFragment();
} else if (id == R.id.nav_browse) {
fragment=new BrowseAdFragment();
} else if (id == R.id.nav_submitAd) {
Intent intent=new Intent(getApplicationContext(),SubmitAdActivity.class);
startActivity(intent);
} else if (id == R.id.nav_help) {
} else if (id == R.id.nav_my_ads) {
} else if (id == R.id.nav_wishList) {
}
replaceFragment();
//drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
//drawer.closeDrawer(GravityCompat.START);
return true;
}
public void setTitleOnActionBar(String title) {
getSupportActionBar().setTitle(title);
}
public void setInitialFragment(){
fragment=new TopMainFragment();
FragmentTransaction ft=getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
// ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
drawer.closeDrawer(GravityCompat.START);
}
public void getLoginFragment(){
fragment=new LoginFragment();
FragmentTransaction ft=getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
drawer.closeDrawer(GravityCompat.START);
}
public void replaceFragment(){
FragmentTransaction ft=getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
drawer.closeDrawer(GravityCompat.START);
}
#Override
public void onResume() {
super.onResume();
// Set the title
}
public void getUserObject(User user){
this.user=user;
if(user!=null){
changeLayoutName();
}
}
public void changeLayoutName(){
TextView name= (TextView)layout.findViewById(R.id.textView8);
name.setText(user.getName());
}
}
LoginFragment.java(Fragment)
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.dellpc.reuz_app.Activities.HomeActivity;
import com.example.dellpc.reuz_app.Activities.RegisterActivity;
import com.example.dellpc.reuz_app.Activities.UnableToLoginActivity;
import com.example.dellpc.reuz_app.DTO.User;
import com.example.dellpc.reuz_app.R;
import com.example.dellpc.reuz_app.Validator.IValidation;
import com.example.dellpc.reuz_app.Validator.Validation;
import com.example.dellpc.reuz_app.dao.IUserDAO;
import com.example.dellpc.reuz_app.dao.UserDAO;
public class LoginFragment extends Fragment {
private EditText emailET, passwordET;
private Button loginBTN;
private TextView utlTV, registerTV, invalidEmailTV, invalidPasswordTV;
private View view;
private NavigationView navigationView;
private User user;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// keep the fragment and all its data across screen rotation
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_login, container, false);
Toolbar toolbar = (Toolbar)view.findViewById(R.id.toolbar);
((HomeActivity)getActivity()).getSupportActionBar().setTitle("LOGIN");
this.mapping();
this.listeners(view);
return view;
}
public void mapping(){
emailET = (EditText)view.findViewById(R.id.emailET);
passwordET = (EditText)view.findViewById(R.id.passwordET);
loginBTN = (Button)view.findViewById(R.id.loginBTN);
utlTV = (TextView)view.findViewById(R.id.utlTV);
invalidEmailTV = (TextView)view.findViewById(R.id.invalidEmailTV);
invalidPasswordTV = (TextView)view.findViewById(R.id.invalidPasswordTV);
registerTV = (TextView)view.findViewById(R.id.registerTV);
}
public void listeners(View view){
loginBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = emailET.getText().toString();
String password = passwordET.getText().toString();
validateAndMoveToActivity(email, password);
}
});
utlTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent utlIntent = new Intent(getActivity(), UnableToLoginActivity.class);
startActivity(utlIntent);
}
});
registerTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent registerIntent = new Intent(getActivity(), RegisterActivity.class);
startActivity(registerIntent);
}
});
emailET.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
invalidEmailTV.setText("");
invalidPasswordTV.setText("");
}
});
passwordET.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s)
{
invalidEmailTV.setText("");
invalidPasswordTV.setText("");
}
});
}
public void validateAndMoveToActivity(String email, String password){
IValidation iValidation = new Validation();
if(!iValidation.isFilled(email) && iValidation.isFilled(password)){
invalidEmailTV.setText("This field is required");
}
else if(iValidation.isFilled(email) && !iValidation.isFilled(password)) {
invalidPasswordTV.setText("This field is required");
}
else if(!iValidation.isFilled(email) && !iValidation.isFilled(password)){
invalidEmailTV.setText("This field is required");
invalidPasswordTV.setText("This field is required");
}
else
{
IUserDAO iUserDAO = new UserDAO(getActivity().getApplicationContext());
/*
if(iUserDAO.isEmailPasswordExist(email, password))*/
if(iUserDAO.isEmailExist(email)){
user=iUserDAO.isEmailPasswordExist(email,password);
if(user!=null)
{
Toast.makeText(view.getContext(), "Successful Login", Toast.LENGTH_SHORT).show();
HomeActivity home=new HomeActivity();
home.getUserObject(user);
/* Intent homeACIntent = new Intent(view.getContext(), HomeActivity.class);
homeACIntent.putExtra("user",user);
startActivity(homeACIntent);*/
// Remove this fragment.
getActivity().getFragmentManager().popBackStack();
}
else {
invalidPasswordTV.setText("Invalid email or password");
}
}
else {
// After successful login where do you want to move your activity.
invalidEmailTV.setText("Invalid phone number or email");
}
}
}
#Override
public void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
state.putSerializable("user",user);
}
}
HomeActivity contains Two Fragments- Login And Top
Initially it shows top fragment
Login Fragment has a option for registration
After successful registration the user is navigated to login fragment
Now after Login the control must reach to the "Top" and the name of the user has to written in the Navigation Drawer
How to do it?
this is my Drawer .t want to write name of User instead of welcome to Reuz
HomeActivity
I see that you're saving the user details in your local database, and then trying to load the data after the user has successfully logged in. The issue that you're facing is that the TextView is not being updated. One possible solution that you can call the changeLayoutName() method from the onViewCreated() method of the TopMainFragment itself.

Android: Navigation drawer indication not working

I am following a tutorial on YouTube, but I'm stuck at (+/- 41 min).
I'm expected to see a indication when I click on a menu item, but this doesn't happen.
The picture below shows how it's supposed to look:
how it should look:
Main Code:
package com.example.flori.mylordring;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
private static ImageButton imgView;
private int test;
int [] images = {R.drawable.im1,R.drawable.im2,R.drawable.im3,R.drawable.im4,R.drawable.im5,R.drawable.im6,R.drawable.im7};
private ActionBarDrawerToggle actionBarDrawerToggle;
private DrawerLayout drawerLayout;
private ListView navList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
navList = (ListView)findViewById(R.id.navlist);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ArrayList<String> navArray = new ArrayList<String>();
navArray.add("Home");
navArray.add("Fragment 1");
navArray.add("Fragment 2");
navArray.add("Fragment 3");
navArray.add("Fragment 4");
navArray.add("Fragment 5");
navList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,navArray);
navList.setAdapter(adapter);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.opendrawer,R.string.closedrawer);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Rolololo", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
protected void onPostCreate(Bundle saveInstancesState){
super.onPostCreate(saveInstancesState);
actionBarDrawerToggle.syncState();
}
public void image(View view){
imgView=(ImageButton)findViewById(R.id.imageButton);
imgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
test++;
test = test % images.length;
imgView.setImageResource(images[test]);
}
}
);
}
#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;
}else if(id == android.R.id.home){
if(drawerLayout.isDrawerOpen(navList)){
drawerLayout.closeDrawer(navList);
}else{
drawerLayout.openDrawer(navList);
}
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
}
thank you in advance ;)
try to implement NavigationView.OnNavigationItemSelectedListener instead of AdapterView.OnItemClickListener and implement this method:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
// Handle the camera action
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
} else if (id == R.id.add_goal) {
}
Implementing NavigationDrawer is easy with the latest version of Android Studio. Just create new Activity from File-New-Activity-Navigation Drawer Activity.
Here is a good tutorial with the latest Navigation View

Error with using addtextchangedlistener to NavigationView Edittext

I'm trying to add an addtextchangedlistener event inside a navigationView edittext.
I want to add it so whenever I type characters in the textbox, it will automatically capture the text and then filter my menu using the text supplied.
What happens is when I run the app, it throws a null pointer exception on the
addTextChangedListener. How can I fix this to allow it to detect the input and control it from there?
this is my xml and class files:
nav_header_home.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="#dimen/nav_header_height"
android:background="#e3e4e5"
android:theme="#style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="#dimen/search_bar_layout_height"
android:id="#+id/editTextSearchBar"
android:background="#drawable/rounded_edittext"
android:hint="Search"
android:textColorHint="#color/colorSearchBarHint"
android:textAlignment="center"
android:drawableLeft="#drawable/ic_menu_search"
android:textColor="#color/colorProfileName"
/>
</LinearLayout>
<LinearLayout
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" 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" app:menu="#menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
my HomeActivity class:
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
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.EditText;
import android.widget.ListView;
import com.app.test.R;
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
int mProfileMenuCtr = 0;
String [] mValues = {"Inbox","News","My Profile"};
EditText mSearchBar;
String mTempText;
int [] mTempCtr;
int mTempCtr2;
MenuItem mItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mSearchBar = (EditText)findViewById(R.id.editTextSearchBar);
mSearchBar.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mSearchBar.setError(null);
}
#Override
public void afterTextChanged(Editable s) {
mTempText = mSearchBar.toString();
mTempCtr2 = 0;
for(int o = 0;o <mValues.length;o++){
boolean containerContainsContent = mValues[o].toLowerCase().contains(mTempText.toLowerCase());
if(containerContainsContent)
{
mTempCtr2++;
mTempCtr [mTempCtr2] = o;
}
}
invalidateOptionsMenu();
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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.setDrawerListener(toggle);
toggle.syncState();
}
#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.activity_home_drawer, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.activity_home_drawer, menu);
hideMenuItems(menu, false);
return super.onPrepareOptionsMenu(menu);
}
private void hideMenuItems(Menu menu, boolean visible)
{
for(int i = 0; i < menu.size(); i++){
menu.getItem(i).setVisible(visible);
}
for(int ii = 0; ii < mTempCtr.length;ii++){
menu.getItem(ii).setVisible(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);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_inbox) {
} else if (id == R.id.nav_news) {
} else if (id == R.id.nav_my_profile) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Its too late to answer but here is my answer
Find navigation header and then find EditText
View header = navigationView.getHeaderView(0);
mSearchBar = (EditText) header.findViewById(R.id. editTextSearchBar);
then Add TextWatcher on EditText

Categories

Resources