How to set menu to an AppBarLayout - java

I'm new to programming, and I want to add a settings menu button.
Like on the picture, my app doesn't show the three dots button.
MainActivity.java :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.vueSatellite:
Toast.makeText(this, "Hi", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="#dimen/appbar_padding"
android:text="#string/app_name"
android:textColor="#color/colorAccent"
android:textAppearance="#style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
app:tabTextColor="#color/colorAccent"/>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
settings.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/vueSatellite"
android:title="#string/item_vue_satellite"
app:showAsAction="never"/>
</menu>
What is my mistake ?
Or what do I have to add?
Tell me if there is missing information like the file AndroidManifest.xml for example.
Thanks

In your Activity just add
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Hope it works.

Related

fragments overlaid in bottom navigation and drawer layout using viewpager 2 in android app

I'm almost new to programming in Android and there are still things that I don't handle very well. Now I'm doing a project in Android using java that has me half frustrated since I've been trying to find and fix a problem for WEEKS but I still can't identify what it is. The app consists of a view that contains a drawer layout with a series of "options", it also contains a viewpager2 and a bottom navigation with 2 other "options." The error appears when I press the bottom navigation tabs since the two fragments they overlap each other, the same happens when I select an option from the navigation drawer, the fragments are superimposed, then I leave you part of my code so that I hope you can help me, thank you very much for taking the time to help me.
main:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
NavigationView navigationView;
//variables para cargar el fragment
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
String personName;
ViewPager2 viewPager2;
BottomNavigationView bottomNavigationView;
ViewPagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);//se pasa el toolbar
drawerLayout=findViewById(R.id.drawer);
navigationView=findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
actionBarDrawerToggle.syncState();
initView();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == R.id.user){
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new perfil_user());
fragmentTransaction.commit();
}
else if (item.getItemId() == R.id.exit){
finish();
}
//aquĆ­ van las otros fragment
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}
private void initView() {
viewPager2 = findViewById(R.id.vp_horizontal_ntb);
viewPager2.setOffscreenPageLimit(100);
bottomNavigationView = findViewById(R.id.navigation);
adapter = new ViewPagerAdapter(this);
viewPager2.setAdapter(adapter);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment=null;
switch (item.getItemId()){
case R.id.perfil:
fragment=new perfil_user();
break;
case R.id.generar_debate:
fragment=new genera_debate();
break;
}
if (fragment != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
return true;
}
});
}
}
files xml:
content main.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"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
drawer toolbar:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#067B30"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
>
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/navigation"
app:menu="#menu/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:foreground="?attr/selectableItemBackground" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/vp_horizontal_ntb"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
activity main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/drawer_toolbar"
>
</include>
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/content_main"
>
</include>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="#menu/drawer_menu"
app:headerLayout="#layout/drawer_header"
android:layout_gravity="start"
android:fitsSystemWindows="true"
>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>

Toolbar menu being cleared after being set (When activity is started)

Toolbar menu is inflated in fragment but then cleared later.
When activity is first created (or rotated) the toolbar menu is inflated properly in the fragment, but then gets cleared before it can be displayed.
I set the toolbar menu in my fragment's onActivityCreated, and it stays set through onResume in the activity. The next call I've found is in onCreateOptionsMenu in the activity, but at this point toolbar.getMenu().size() = 0
Somewhere between onResume and onCreateOptionsMenu the toolbar menu is getting reset.
Edit:
I'm not sure what is necessary to reproduce it, but here is all(?) of the relevant code.
Activity:
private Toolbar toolbar;
private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;
private FloatingActionButton fab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = findViewById(R.id.main_fab);
drawer = findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(this, drawer,toolbar,R.string.nav_drawer_open,R.string.nav_drawer_close);
drawer.addDrawerListener(toggle);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if (savedInstanceState == null) {
showFragment(ScheduleFragment.newInstance());
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(toggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
CharSequence retainTitle = toolbar.getTitle();
super.onPostCreate(savedInstanceState);
toggle.syncState();
toolbar.setTitle(retainTitle);
}
Fragment:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
toolbar = ((MainActivity)getActivity()).getToolbar();
setMenuAction(MENU_ACTION_DELETE);
}
protected void setMenuAction(int menuAction) {
Log.d(logger, "setting the menu");
switch(menuAction) {
case MENU_ACTION_DELETE:
toolbar.getMenu().clear();
toolbar.inflateMenu(R.menu.menu_delete);
break;
case MENU_ACTION_NONE:
Log.d(logger, "clearing menu");
toolbar.getMenu().clear();
break;
}
}
Layouts:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout">
<include
layout="#layout/activity_main"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/menu_nav" />
</android.support.v4.widget.DrawerLayout>
<?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"
tools:context=".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:layout_scrollFlags="scroll|snap|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/main_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_baseline_add_24px"
android:layout_margin="16dp" />
<include layout="#layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<FrameLayout
android:id="#+id/fragmentHolder"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The problem in my current code as posted was the line setSupportActionBar(toolbar);
This was not needed, and was in fact causing the troubling lifecycle problems stated above.

i have bottom navigation and navigation drawer and i set home screen fragment as default

i have bottom navigation and navigation drawer and i set home screen fragment as default
now problem is when i run my app on Emulator hamburger icon and bottom navigation is showing and home Fragment by default
when i click to hamburger icon its not working
when i remove default home fragment then navigation drawer is working
How to solve this Problem so both navigationDrawer and bottom navigation will work
HomeScreenActivity.java
public class HomeScreenActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mToggle;
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
bottomNavigationView = findViewById(R.id.homeScreenBottomNavigation);
bottomNavigationView.setOnNavigationItemSelectedListener(bottomNavListener);
// getSupportFragmentManager().beginTransaction().replace(R.id.homeScreenframeLayout, new HomeFragment()).commit();
mDrawerLayout = findViewById(R.id.HomeScreenDrawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private BottomNavigationView.OnNavigationItemSelectedListener bottomNavListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.bottomNavHome:
selectedFragment = new HomeFragment();
break;
case R.id.bottomNavAdd:
selectedFragment = new AddFragment();
break;
case R.id.bottomNavSearch:
selectedFragment = new SearchFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.homeScreenframeLayout, selectedFragment).commit();
return true;
}
};
}
ActivityHomeScreen.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeScreenActivity"
>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/HomeScreenDrawerLayout"
>
<android.support.design.widget.NavigationView
app:headerLayout="#layout/left_navigation_header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/navigationView"
app:menu="#menu/drawer_layout"
android:layout_gravity="start"
android:background="#ffff"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/homeScreenframeLayout"
>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/homeScreenBottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_navigation"
android:background="?android:attr/windowBackground"
>
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
The issue is with your ActivityHomeScreen.xml. You have to make DrawerLayout as the parent view. Inside that you have to place BottomNavigationDrawer and remaining code.
And You havent created custom appbar for naviagtion drawer(it now comes with navigation drawer template)
Your ActivityHomeScreen.xml should look like this:
ActivityHomeScree.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>
Now create a layout file app_bar_main.xml. Inside it create a toolbar and place your frameLayout here.
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"
tools:context=".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>
<!--
Place your frameLayout here
-->
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
app:menu="#menu/navigation" />
</android.support.design.widget.CoordinatorLayout>
Now both Navdrawer and BottomNavigationDrawer should work.

Actionbar icons not beeing updated

I am trying to change an icon in the actionbar, but I can not get it work. If I send to the log the getIcon().toString(), It seems that the icon is modified, but the actionbar is not updated.
This is the main activity layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<android.support.v7.widget.Toolbar
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/AppTheme">
</android.support.v7.widget.Toolbar>
The menu xml file:
<menu 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"
tools:context=".MainActivity">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item
android:id="#+id/about"
android:title="#string/about"
android:orderInCategory="110"
app:showAsAction="never" />
<item
android:id="#+id/connect"
android:title="Connect"
android:icon="#drawable/ic_play"
android:orderInCategory="130"
app:showAsAction="always" />
<item
android:id="#+id/testin"
android:title="Test_in"
android:icon="#drawable/ic_testin"
android:orderInCategory="120"
app:showAsAction="always" />
And the main activity code:
//Menu Items
MenuItem testinMenu, connectMenu;
Menu actionMenu;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Action Bar
toolbar = findViewById( R.id.appbar);
setSupportActionBar( toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menumain, menu);
actionMenu = menu;
connectMenu = (MenuItem) menu.findItem(R.id.connect);
testinMenu = (MenuItem) menu.findItem(R.id.testin);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.connect: {
connectMenu.setIcon( R.drawable.ic_stop);
invalidateOptionsMenu();
if( isRainbowConnected == false) {
connect();
} else
disconnect();
break;
}
case R.id.testin: {
if( isRainbowConnected == true)
sendTestIn();
break;
}
// case blocks for other MenuItems (if any)
}
return true;
}
I have tried everything, but nothing seems to work.
Thanks in advance!

Same Layout but different result

I have a problem. With the same layout, one time it's charging on launch and second time it's by an intent.
Good:
Bad:
Here is my MainActivity. I implement the drawer and so on... :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Get the list of shoes
new getSneakers().execute();
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = setupDrawerToggle();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// Tie DrawerLayout events to the ActionBarToggle
mDrawer.addDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
private ActionBarDrawerToggle setupDrawerToggle() {
return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
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.all_sneak) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_main, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
new getSneakers().execute();
} else if (id == R.id.my_colleciton) {
} else if (id == R.id.nav_want) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawer.closeDrawer(GravityCompat.START);
return true;
}
Here is my content_main. This is included by the app bar main and it's showing the shoes. For me the problem is coming from here, but I try something and it changes nothing.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.etml.sneakcolleciton.MainActivity"
tools:showIn="#layout/app_bar_main"
android:id="#+id/content_main"
android:background="#eaecea">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
My Activit_Main is the simple menu where I include the app_main_main whose content one includes where the RecyclerView is:
<?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"
android:background="#eaecea">
<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>
Here's the app_bar_main where I include the content_main where is the list with the shoes:
<?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:id="#+id/layout_main"
tools:context="com.etml.sneakcolleciton.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
android:id="#+id/main_container"
layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
Try this:
replace
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.etml.sneakcolleciton.MainActivity"
tools:showIn="#layout/app_bar_main"
android:id="#+id/content_main"
android:background="#eaecea">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>`
with
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.etml.sneakcolleciton.MainActivity"
tools:showIn="#layout/app_bar_main"
android:id="#+id/content_main"
android:background="#eaecea">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"/>
</LinearLayout>`

Categories

Resources