Android Error I'm a real noob when it comes to Android Studio, and I'm trying to make it so when I click a button in IntroActivity, it will direct me to the FeaturedActivity page, which contains a list and navigation drawer, if that's important. But when I click the first time in the emulator, the app stops, and when I go back and click it again, nothing happens. I have Java Code for both activities. Could someone help me please?
<activity
android:name=".FeaturedActivity"
android:label="#string/title_activity_featured"
android:theme="#style/AppTheme.NoActionBar"></activity>
The first activity
public class IntroActivity extends AppCompatActivity {
Button ComeInButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
ComeInButton = (Button) findViewById(R.id.ComeIn);
ComeInButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(IntroActivity.this, FeaturedActivity.class);
startActivity(i);
}
});
}
}
Featured Activity
public class FeaturedActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
String [] art_Names;
TypedArray pics;
String[] artist_Names;
String[] desc;
List<RowItem> rowItems;
ListView myListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_featured);
rowItems=new ArrayList<RowItem>();
art_Names=getResources().getStringArray(R.array.Featured_Arts);
pics=getResources().obtainTypedArray(R.array.Art_Pics);
artist_Names=getResources().getStringArray(R.array.Artist_Names);
desc=getResources().getStringArray(R.array.Descriptions);
for(int i=0;i<art_Names.length;i++){
RowItem item=new RowItem(art_Names[i], pics.getResourceId(i, -1),artist_Names[i],desc[i]);
rowItems.add(item);
}
myListView=(ListView)findViewById(R.id.list);
CustomerAdapter adapter = new CustomerAdapter(this,rowItems);
myListView.setAdapter(adapter);
myListView.setOnClickListener((View.OnClickListener) this);
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();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String art_name=rowItems.get(position).GetArtName();
Toast.makeText(getApplicationContext(),""+art_name,Toast.LENGTH_SHORT).show();
}
#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.featured, 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;
}
}
The question is a little vague on the details, but taking a shot in the dark, did you register FeaturedActivity in the app manifest?
Something like:
<activity
android:name=".FeaturedActivity"
android:label="#string/title_activity_featured"
android:theme="#style/AppTheme">
should be in your AndroidManifest.xml file.
You are probably getting a nice error message in logcat, so please look there :)
I think the crash cause is where you are casting FeaturedActivity to View.OnClickListener inside FeatureActivity#onCreate(). since it does not implement View.OnClickListener, you get ClassCastException and since you do not catch this exception, then your app crashes.
EDIT I meant this line:
public class FeaturedActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
...
myListView.setOnClickListener((View.OnClickListener) this); // this line
}
...
}
EDIT 2 You can fix it by implementing View.OnClickListener in FeatureActivity like this:
public class FeaturedActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
...
myListView.setOnClickListener(this); // cast is not needed anymore
}
#Override
protected void onClick(View v) {
// handle click
}
...
}
By the way, If you want to handle item click for ListView you should use AdapterView#setOnItemClickListener() instead so you get notified when one of the items in ListView is clicked
Related
I'm trying to implement the Navigation Drawer Activity on my Dashboard Activity. In my application, I have created a Theme where the 'Toolbar/ActionBar' is removed and just put an ImageView for my burger menu on a custom layout and include it on my dashboard layout. What I wanted is that when this ImageView is clicked the sliding drawer will display.
What I tried to do is add an Navigation Drawer Activity by com.project.projectname -> New -> activity -> 'Navigation Drawer Activity' and automatically it adds NavigationActivity class , activity_navigation.xml
app_bar_navigation.xml, etc.
But I was wondering how can I open the drawer?
In my Dashboard Activity I added this
public class DashboardActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
View side_tab = findViewById(R.id.side_tab);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// open drawer here
}
});
Also, in NavigationActivity it uses ActionBarDrawerToggle, I must not use this because I don't apply an ActionBar. But what alternative can I use?
Here is my NavigationActivity
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
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);
}
#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.navigation, 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();
Intent goToSreen;
if (id == R.id.nav_dashboard) {
goToSreen = new Intent(NavigationActivity.this, DashboardActivity.class);
startActivity(goToSreen);
} else if (id == R.id.nav_others) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Thank you for your kind help.
Update
So in my Dashboard Activity I added the following
NavigationActivity navigationActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
View side_tab = findViewById(R.id.side_tab);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
navigationActivity = new NavigationActivity() ;
navigationActivity.drawer.openDrawer(GravityCompat.START);
}
});
And in my Navigation Activity I add a global variable
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
I have managed to call the drawer in my Dashboard Activity but the sliding menu still doesn't display when I clicked my burger menu. The app crashes and displays java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(int)' on a null object reference
Add this in your NavigationActivity
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//Add this line
public static DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
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();
.......
And next change this
expanded_menu.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// open drawer here
NavigationActivity. drawer.openDrawer(GravityCompat.START);
}
});
Try this hope it will solve your problrm.
Add the following code in NavigationActivity:
DrawerLayout mDrawerLayout; // Declare globally
In onCreate() :
mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// open drawer here
mDrawerLayout.openDrawer(Gravity.LEFT);
}
});
If the drawer is open, close it onBackPressed():
#Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
return;
}
}
Hope this helps.
I extend my NavigationActivity to my DashboardActivity like below
public class DashboardActivity extends NavigationActivity {
side_tab = findViewById(R.id.side_tab);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
side_tab.setVisibility(View.INVISIBLE);
drawer.openDrawer(GravityCompat.START);
}
});
and then follow the said answered by #Hassan Usman and #tahsinRupam
after that I added the DrawerLayout in my dashboard.xml
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
No suggestions are appeard and If written manually it gives an error which tells me to make a class. (I am try to retrive data from firebase and put it in a recycler view..! Using Firebase recycler adapter.)
public class Main_Page extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
FirebaseAuth mFirebaseAuth;
FirebaseAuth.AuthStateListener mAuthStateListener;
GoogleApiClient mGoogleApiClient;
//Recycler view
private RecyclerView mTodoList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main__page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Firebase Auth
mFirebaseAuth = FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null) {
finish();
startActivity(new Intent(Main_Page.this, SignIn.class));
}
}
};
FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
startActivity(new Intent(Main_Page.this, Add_To_Do.class));
}
});
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);
//Recycler View
mTodoList = (RecyclerView) findViewById(R.id.todo_list);
mTodoList.setHasFixedSize(true);
mTodoList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
/*If i write FirebaseRecyclerAdapter it is not showing any suggestions
and if i write it manually i am getting an redline and it tells me to make a
class or a function*/
FirebaseRecy
}
//Recycler View
public static class ToDoViewHolder extends RecyclerView.ViewHolder{
View mView;
public ToDoViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setLabel(String label){
TextView post_label = (TextView)
mView.findViewById(R.id.post_label);
post_label.setText(label);
}
public void setNote(String note){
TextView post_note = (TextView) mView.findViewById(R.id.post_note);
post_note.setText(note);
}
}
#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__page, 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) {
mFirebaseAuth.signOut();
}
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;
}
}
Error Screenshot
Build.Gradle
Add this:
com.firebaseui:firebase-ui-database:0.4.0 in build.Gradle
i have made a interface in the fragment from where i want to transfer the value to the mainactivity but the problem is that i can't implement it's listener i don't why?
help me to resolve this issue.Thank you in advance
fragment class:
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
TransferValue SendData;
private RecyclerView recyclerView;
public Business() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.business_recycler_view);
FetchLists f = new FetchLists();
f.execute(10, 0);
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
SendData = (TransferValue) context;
} catch (ClassCastException e) {
Log.d("ashu", "implement the methods");
throw new ClassCastException("implemented the methods");
}
}
public interface TransferValue {
public void sendValue(StringList value, int positionValue);
}
}
Mainactivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener implements Business.TransferValue //error
{
#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.setDrawerListener(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 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);
}
FragmentManager frag = getSupportFragmentManager();
if (id == R.id.nav_sport) {
frag.beginTransaction().replace(R.id.content_frame, new Sport()).commit();
} else if (id == R.id.nav_entertainment) {
frag.beginTransaction().replace(R.id.content_frame, new Entertainment()).commit();
} else if (id == R.id.nav_general) {
frag.beginTransaction().replace(R.id.content_frame, new General()).commit();
} else if (id == R.id.nav_business) {
frag.beginTransaction().replace(R.id.content_frame, new Business()).commit();
} else if (id == R.id.nav_techno) {
frag.beginTransaction().replace(R.id.content_frame, new Technology()).commit();
}
}
}
Instead of this:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener implements Business.TransferValue //error
{
Use this method because multiple interfaces need the only comma as separator, not multiple implements:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, Business.TransferValue
{
I have make the main part marked with bold (B) I am confused with, where to keep OnClickListner on the code that intent TextView to the new activity.
I want to make clickable menus in navigation drawer. I have login activity. So I want to make login clickable that intents to the login activity.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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();
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_search_category) {
// Handle the camera action
} else if (id == R.id.nav_login) {
/*t1=(TextView)findViewById(R.id.nav_login);*/
t1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent i=new Intent(MainActivity.this, LoginActivity.class);
startActivity(i);
}
});
} else if (id == R.id.nav_register) {
} else if (id == R.id.nav_test) {
} else if (id == R.id.nav_test1) {
} else if (id == R.id.nav_test2) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
You don't need the OnClickListener of the TextView. Just do:
else if (id == R.id.nav_login) {
Intent i=new Intent(MainActivity.this, LoginActivity.class);
startActivity(i);
}
I have a problem with the navigation drawer. I want to know how to display recyclerview when the user clicks on different element of each menu.
Here are some source code and an illustrative capture
N.B: different menu items ("Accueil, Contacts Staff, etc ...") are in an .xml file in the layout.
MainActiviy.java
public class MainActivity extends AppCompatActivity {
ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
setupWindowAnimations();
//définir la toolbr en tant qu'actionbar
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, 0, 0);
drawerLayout.setDrawerListener(drawerToggle);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
});
//on remplit notre viewpager, comme à notre habitude
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
#Override
public Fragment getItem(int position) {
return RecyclerViewFragment.newInstance();
}
#Override
public CharSequence getPageTitle(int position) {
return "Tab " + position;
}
#Override
public int getCount() {
return 1;
}
});
//indique au tablayout quel est le viewpager à écouter
tabLayout.setupWithViewPager(viewPager);
}
private void setupWindowAnimations() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Explode explode = new Explode();
getWindow().setExitTransition(explode);
Fade fade = new Fade();
getWindow().setReenterTransition(fade);
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
#OnClick(R.id.fab)
public void onFabClick() {
Snackbar.make(fab, "Here's a Snackbar", Snackbar.LENGTH_LONG)
.setAction("Undo", new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}).show();
}
}
Sample source code for you
public class NavActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav);
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);
}
#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 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;
}
}
Hope this helps you
Not understand exactly what you want. Where do you want to display recycler view? You already have OnNavigationItemSelectedListener, just check which menu you have clicked and do what you want:
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.contact:
// do anything you want, ex
showContactList();
break;
}
the function to show your contact list
private void showContactList(){
List<Contact> data = getYourDataSomeHow();
yourAdapter.setData(data);
yourRecyclerView.setAdapter(yourAdapter);
}