This is my main.xml file--
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.pawan.googlesignin.SongsActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#color/colorAccent"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:id="#+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="live music" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scheduled" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" /></RelativeLayout>
This is my items.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/share"
android:title="share"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
<item android:id="#+id/feedback"
android:title="feedback"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
<item android:id="#+id/profile"
android:title="profile"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
This is my MainActivity.xml--
public class SongsActivity extends AppCompatActivity {
private static final String TAG="MainActivity";
private SectionsPageAdapter mSectionPageAdapter;
private ViewPager mViewpager;
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songs);
Log.d(TAG, "onCreate:starting");
mSectionPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewpager =findViewById(R.id.container);
setupViewPager(mViewpager);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewpager);
toolbar =findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("My app");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, VenuesActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void setupViewPager(ViewPager viewPager){
SectionsPageAdapter adapter=new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new live(),"live music");
adapter.addFragment(new scheduled(),"scheduled");
viewPager.setAdapter(adapter);
}
}
Friends, I am not able to click on the back navigation button and also on the three dots on the top right corner of the Toolbar.
Please give a solution for this, I have tried all the solutions on provided here.
I can't find any error here.Please help me...
in my case :
changing activity_main.xml from this:
<include
android:id="#+id/toolbar"
layout="#layout/asset_bar_layout"/>
to this:
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="#+id/toolbar"
android:background="#color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:popupTheme="#style/MenuStyle"/>
</com.google.android.material.appbar.AppBarLayout>
solved the problem
Use this instead to handle the back button on the actionbar.
case android.R.id.home:
super.onBackPressed();
return true;
Hello,i have find the solution for your query
Please update your code like this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
System.out.println("back button pressed");
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
return true;
case R.id.share:
System.out.println("share button pressed");
case R.id.feedback:
System.out.println("feedback button pressed");
case R.id.profile:
System.out.println("profile button pressed");
default:
return super.onOptionsItemSelected(item);
}
}
You have forgot to call finish()method and defining the click listener of your menu item.
Please ask me if you need any more help.
Related
Iam Implementing navigation drawer in my application but its menu not working on click. I dont know whats wrong but on click event is not doing anything or toasting a message.Click anything from drawer menu dont do anything.Firstly other button were also not working but adding them in a seprate layout solved the issue.but drawer menu are still not working
Here is my xml :
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Here in java Iam using it :
public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author_navigation);
viewDeclaration();
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
private void viewDeclaration() {
newSurveyBtn = findViewById(R.id.new_surveys_button);
surveyWithRef = findViewById(R.id.get_survey_button);
surveyResult = findViewById(R.id.analyze_survey);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_share:
System.out.println("ss" + "coming");
Toast.makeText(getApplicationContext(), "Share", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_survey_count:
Toast.makeText(getApplicationContext(), "Surveys", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_logout:
Toast.makeText(getApplicationContext(), "logout", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
Solution for this is in the XML file bring navigation view below the include tag and it will work
Change this
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
To this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
You don't initialize actionBarDrawerToggle. You have to do it.
When you are using AndroidX try to use the methods related to it.
Add the host fragment when you want to implement the fragments in that particular activity:
xml:
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<fragment
android:id="#+id/host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/navigation_graph"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/app_bar_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu" />
Now use the navigation graph where you need to implement the fragments you are about to use in the activity :
navigation_graph.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation_graph"
app:startDestination="#id/profile">
<fragment
android:id="#+id/profile"
android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="#string/profile"
tools:layout="#layout/fragment_profile" />
<fragment
android:id="#+id/features"
android:name="com.example.jetpacknavigationexample.Fragments.FeaturesFragment"
android:label="#string/features"
tools:layout="#layout/fragment_features" />
<fragment
android:id="#+id/signout"
android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="#string/signout"
tools:layout="#layout/fragment_signout" />
</navigation>
In your java class add the implement the Navigation component code :
public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
//Add this
NavController mNavController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author_navigation);
//add this
mNavController = Navigation.findNavController(this,R.id.host_fragment);
NavigationUI.setupActionBarWithNavController(this, mNavController, drawerLayout);
NavigationUI.setupWithNavController(navigationView,mNavController);
mNavigationView.setNavigationItemSelectedListener(this);
viewDeclaration();
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
private void viewDeclaration() {
newSurveyBtn = findViewById(R.id.new_surveys_button);
surveyWithRef = findViewById(R.id.get_survey_button);
surveyResult = findViewById(R.id.analyze_survey);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_share:
System.out.println("ss" + "coming");
Toast.makeText(getApplicationContext(), "Share",
Toast.LENGTH_SHORT).show();
break;
case R.id.menu_survey_count:
Toast.makeText(getApplicationContext(), "Surveys",
Toast.LENGTH_SHORT).show();
break;
case R.id.menu_logout:
Toast.makeText(getApplicationContext(), "logout",
Toast.LENGTH_SHORT).show();
break;
}
return true;
}
You can test your app now. let me know after you implement this.
I'm actually working on an app which includes navigation view and the app is running on androidx, I tried all the possible solutions from my point of view. Actual problem is onNavigationItemSelected is not calling even though I implemented NavigationView.OnNavigationItemSelectedListener to my base class.
My HomeActivity class which implements NavigationView.OnNavigationItemSelectedListener is
public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = "checkDate";
ActivityHomeBinding binding;
Context context;
LinearLayoutManager layoutManager;
HomeAdapter adapter;
ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
context = this;
binding = DataBindingUtil.setContentView(this, R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
drawerToggle = new ActionBarDrawerToggle(this, binding.drawer, R.string.open, R.string.close);
binding.drawer.addDrawerListener(drawerToggle);
drawerToggle.syncState();
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
List<EventDay> events = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
events.add(new EventDay(calendar, R.drawable.ic_event_black_24dp));
binding.homeLayout.calendarView.setEvents(events);
binding.homeLayout.calendarView.setOnDayClickListener(eventDay -> {
Toast.makeText(context, eventDay.getCalendar().getTime().toString(), Toast.LENGTH_SHORT).show();
Log.d(TAG, "onDayClick: "+Calendar.DAY_OF_MONTH);
});
layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(RecyclerView.VERTICAL);
adapter = new HomeAdapter(context);
binding.homeLayout.rvEvents.setNestedScrollingEnabled(true);
binding.homeLayout.rvEvents.setHasFixedSize(true);
binding.homeLayout.rvEvents.setAdapter(adapter);
binding.homeLayout.rvEvents.setLayoutManager(layoutManager);
binding.navView.setNavigationItemSelectedListener(this);
ItemClickSupport.addTo(binding.homeLayout.rvEvents).setOnItemClickListener((recyclerView, position, v) -> {
Intent intent = new Intent(context, SiteReachActivity.class);
startActivity(intent);
});
}
#Override
public void onBackPressed() {
if (binding.drawer.isDrawerOpen(GravityCompat.START)){
binding.drawer.closeDrawer(GravityCompat.START);
return;
}
super.onBackPressed();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home){
binding.drawer.openDrawer(GravityCompat.START);
}
return true;
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
Intent intent;
switch (id){
case R.id.menu_profile:
intent = new Intent(context, ProfileActivity.class);
startActivity(intent);
break;
case R.id.menu_dashboard:
break;
case R.id.menu_history:
intent = new Intent(context, HistoryActivity.class);
startActivity(intent);
break;
case R.id.menu_TADA:
break;
case R.id.menu_logOut:
break;
}
return true;
}
}
And the layout file is
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="#color/white"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#color/colorPrimary"
app:menu="#menu/activity_main_drawer"/>
<include
android:id="#+id/home_layout"
layout="#layout/home_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
And the layout file for activity_main_drawer is
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_profile"
android:icon="#drawable/user"
android:title="#string/user"/>
<item android:id="#+id/menu_dashboard"
android:icon="#drawable/home"
android:title="Dashboard"/>
<item android:id="#+id/menu_history"
android:icon="#drawable/history"
android:title="History"/>
<item android:id="#+id/menu_TADA"
android:icon="#drawable/wallet"
android:title="TADA"/>
<item
android:id="#+id/menu_logOut"
android:icon="#drawable/ic_power_settings_new_black_24dp"
android:title="Logout"/>
</menu>
In case anything more needed plese do comment. TIA
If you are using androidX then you have to use the NavController for navigation through fragments.
I will post the code I have tried
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
NavController mNavController;
Navigation mNavigation;
BottomNavigationView mBottomNavigationView;
NavigationView mNavigationView;
DrawerLayout mDrawerLayout;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mDrawerLayout = findViewById(R.id.drawer_layout);
mBottomNavigationView = findViewById(R.id.bottom_navigation_view);
mNavigationView = findViewById(R.id.navigation_view);
//mNavController = Navigation.findNavController(this,R.id.host_fragment);
mNavController = Navigation.findNavController(this,R.id.host_fragment);
NavigationUI.setupWithNavController(mBottomNavigationView,Navigation.findNavController(this,R.id.host_fragment));
NavigationUI.setupActionBarWithNavController(this, mNavController, mDrawerLayout);
NavigationUI.setupWithNavController(mNavigationView,mNavController);
mNavigationView.setNavigationItemSelectedListener(this);
//NavigationUI.setupActionBarWithNavController(this,Navigation.findNavController(this,R.id.host_fragment));
}
#Override
public boolean onSupportNavigateUp() {
/*return mNavController.popBackStack(R.id.host_fragment,false);*/
return NavigationUI.navigateUp(Navigation.findNavController(this, R.id.host_fragment), mDrawerLayout);
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onBackPressed() {
NavController navController = Navigation.findNavController(this, R.id.host_fragment);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Objects.requireNonNull(navController.getCurrentDestination()).getId() == R.id.nav_first) {
finishAffinity();
} else {
mNavController.popBackStack(R.id.nav_first, false);
}
}
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.home:
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
mDrawerLayout.closeDrawers();
int id = menuItem.getItemId();
switch (id){
//Nav drawer items
case R.id.profile:
mNavController.navigate(R.id.profile);
break;
case R.id.features:
mNavController.navigate(R.id.features);
break;
case R.id.signOut:
finishAffinity();
break;
}
return true;
}
}
You need to have the host fragment where you need to implement the fragments in the activity layout where you are implementing the Navigation
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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/white"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/toolbar_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="8"
android:gravity="center"
android:padding="10dp"
android:text="#string/jetpack_example"
android:textColor="#000"
android:textSize="15sp" />
<ImageView
android:id="#+id/search_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:padding="10dp"
android:src="#drawable/about_icon" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#android:color/white"
app:labelVisibilityMode="selected"
app:layout_constraintBottom_toBottomOf="#+id/host_fragment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/menu_drawer" />
<fragment
android:id="#+id/host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/navigation_graph"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/nav_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
Next you need to have navigation graph for the navigation and using the arguments.You can also implement actions.Create a navigation folder and implement the below implementing the fragment when you click on the particular item.
navigation_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation_graph"
app:startDestination="#id/nav_first">
<fragment
android:id="#+id/nav_first"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.FirstFragment"
android:label="#string/first"
tools:layout="#layout/fragment_first" />
<fragment
android:id="#+id/nav_second"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.SecondFragment"
android:label="#string/second"
tools:layout="#layout/fragment_second" />
<fragment
android:id="#+id/nav_third"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.ThirdFragment"
android:label="#string/third"
tools:layout="#layout/fragment_third" />
<fragment
android:id="#+id/nav_fourth"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.FourthFragment"
android:label="#string/fourth"
tools:layout="#layout/fragment_fourth" />
<fragment
android:id="#+id/profile"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.ProfileFragment"
android:label="#string/profile"
tools:layout="#layout/fragment_profile" />
<fragment
android:id="#+id/features"
android:name="com.escapadetechnologies.jetpacknavigationexample.Fragments.FeaturesFragment"
android:label="#string/features"
tools:layout="#layout/fragment_features" />
It is very handy and with the androidx lot of boilerplate code can be avoided. If you have any doubts you can comment on my answer.
I have navigation drawer, and it have items on it. but when I click,nothing happens. what's wrong with this code?
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navi_1:
Intent intent = new Intent(BaseOrderActivity.this,BaseOrderActivity.class);
intent.putExtra("dataDealerCode",dataorderCode);
startActivity(intent);
finish();
return true;
case R.id.navi_2:
Intent intent2 = new Intent(BaseOrderActivity.this,MappingProductRecycler.class);
intent2.putExtra("dataDealerCode",dataorderCode);
startActivity(intent2);
finish();
return true;
case R.id.navi_3:
Intent intent3 = new Intent(BaseOrderActivity.this,UpdateProductActivity.class);
intent3.putExtra("dataDealerCode",dataorderCode);
startActivity(intent3);
finish();
return true;
case R.id.navi_4:
return true;
}
//drawerLayout.closeDrawers();
return true;
}
});
When I put breakpoint at NavigationItemSelected, it does not stop at breakpoint, so I cannot start the other activity base on my selected . why?
this is my Layout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">
<FrameLayout
android:animateLayoutChanges="true"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BaseOrderActivity"
/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation"/>
<RelativeLayout
android:id="#+id/main_layout"
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=".BaseOrderActivity">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tab_layout"
android:layout_y="50dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
what wrong with my Layout?
Since you have mentioned that your navigation item selection doesn't seem to work, here are the possible trouble shooting steps:
Check if the navigation view is properly initialized.
mNavigationView = (NavigationView) findViewById(R.id.navigation); //navigation is the id of the xml for your navigation view.
Check the ids in your switch case. R.id.navi_1: etc. (navi_1, etc.) are indeed valid.
Edit:
You should have only 2 view (child) in your drawer layout. The first child is the one that will be shown when the drawer is CLOSED and the second when the drawer is OPEN.
See the sample xml below.
<?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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
// this is the main screen, shown when the drawer is CLOSED
// 1st VIEW
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/toolbar"/>
<ListView
android:id="#+id/list_view_home_users"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
// This view is shown when drawer is OPEN
// 2nd View
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header_layout"
app:itemTextAppearance="#style/TextAppearance.Design.Snackbar.Message"
app:menu="#menu/my_navigation_items" />
</android.support.v4.widget.DrawerLayout>
EDIT 2:
In your case, you can make the relative layout as the first view.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/main_layout"
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=".BaseOrderActivity">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tab_layout"
android:layout_y="50dp" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation"/>
</android.support.v4.widget.DrawerLayout>
Try this code instead
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
Intent intent;
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navi_1:
intent= new Intent(BaseOrderActivity.this,BaseOrderActivity.class);
intent.putExtra("dataDealerCode",dataorderCode);
startActivity(intent);
finish();
break;
case R.id.navi_2:
intent = new Intent(BaseOrderActivity.this,MappingProductRecycler.class);
intent.putExtra("dataDealerCode",dataorderCode);
startActivity(intent);
finish();
break;
case R.id.navi_3:
intent = new Intent(BaseOrderActivity.this,UpdateProductActivity.class);
intent.putExtra("dataDealerCode",dataorderCode);
startActivity(intent);
finish();
break;
case R.id.navi_4:
break;
}
//drawerLayout.closeDrawers();
return true;
}
});
Try this code instead
int ids=menuItem.getItemId();
switch (ids) {
case R.id.itemID:
//Your Code
break
default:
break;
}
drawerLayout.closeDrawers();
return true;
When you have to use switch statement should use break in your every case. but now your using return in inside case block
you should follow this steps it will work...
Implement the navigationView linstner in your BaseOrderActivity like BaseOrderActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
Create your menu in your menu.xml
Initialize you NavigationView in Oncreate
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Finally use this override methods
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity, menu);
return true;
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Intent intent = null;
switch (id) {
case R.id.navi_1:
intent = new Intent(this,BaseOrderActivity.class);
intent.putExtra("dataDealerCode",dataorderCode);
startActivity(intent);
break;
case R.id.navi_2:
intent = new Intent(this,MappingProductRecycler.class);
intent.putExtra("dataDealerCode",dataorderCode);
startActivity(intent);
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Note: Once you use finish then your current Activity/Context will getting Stop/Terminate, So If you use safe way
Hope it will help you..
I have used a menuInflater to create a menu in the onCreateOptionsMenu method of your activity. I have used this to display arrows on the toolbar for a calendar so that the user can go to previous or next month. But for some reason the arrows are not getting displayed
Pls can someone help.
MonthGridActivity:
private MonthGridFragment monthGridFragment;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_calendar_grid, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_previous:
monthGridFragment.loadLastMonth();
return true;
case R.id.action_next:
monthGridFragment.loadNextMonth();
return true;
case R.id.all_events:
monthGridFragment.showAllEvents();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar_grid);
monthGridFragment = new MonthGridFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.activity_calendar_grid_container, monthGridFragment)
.commit();
}
menu_calendar_grid.xml
<item
android:id="#+id/action_previous"
android:title="#string/prev_month"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:icon="#drawable/arrow_previous" />
<item
android:id="#+id/action_next"
android:title="#string/next_month"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:icon="#drawable/arrow_next" />
<item
android:id="#+id/all_events"
android:title="#string/view_all_events"
android:orderInCategory="101"
app:showAsAction="never"
android:icon="#android:drawable/ic_menu_view" />
MonthGridFragment.java
public void loadNextMonth() {
calendar.setTime(CalUtil.addMonth(calendar.getTime(), 1));
refresh();
}
public void loadLastMonth() {
calendar.setTime(CalUtil.subtractMonth(calendar.getTime(), 1));
refresh();
}
activity_calendar_grid.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame">
<android.support.v7.widget.Toolbar
android:id="#+id/myActivity_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_calendar_grid.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/calendar_grid_layout">
<include layout="#layout/calendar_grid_header" />
<GridView
android:paddingTop="1dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/calendar_grid_view"
android:numColumns="7"
android:stretchMode="columnWidth"
android:background="#color/white_gray"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp" />
in onCreate method create a toolbar:
Toolbar toolbar = (Toolbar) findViewById(R.id.myActivity_toolbar);
setActionBar(toolbar);
so in the layout add the view:
<Toolbar
android:id="#+id/myActivity_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
UPD:
You have to use either Toolbar from the support library or the standard one. If MonthGridActivity extends from Activity, use Toolbar (not android.support.v7.widget.Toolbar) in the layout. Otherwise MonthGridActivity must be extended from AppCompatActivity
I have two questions/problems :)
First: I have implemented a Sliding Tabs Layout. Now the fragment is use for the Tabs get cut off at the bottom:
Fragment gets cut off (FAB is on bottom|right)
Second: When I press the FAB i want to open a new Activity (the FAB is on a Fragment). But when I press it, a blank Activity opens (the acctual Activity has a Layout and is working on its own) and when I press the back button I exit the app.
For the First question: This is a Methode I call in the onCreate of the MainActivity (Where the Sliding Tabs Layout is implemented) (I dont know if you need the code for the ViewPager so I didn't include it, if you need it tell me please):
private void excTasks(){
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(4);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
This is the ViewPagerAdapter:
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
//return mFragmentTitleList.get(position);
// Wenn man nur Icons will -> return null, sont das obrige verwenden
return null;
}
}
And here I add the Fragments:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new NewsFragment(), "NEWS");
adapter.addFragment(new ProfileFragment(), "PROFILE");
adapter.addFragment(new NotificationFragment(), "NOTIF");
adapter.addFragment(new AboutFragment(), "ABOUT");
viewPager.setAdapter(adapter);
}
This is the .xml of the MainActivity:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<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|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorColor="#color/white"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
For the second question:
This is in the Fragment (in the onViewCreated) where I want the FAB (OfferActivity is what should be opened when pressing the button):
//FAB
FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Click action
Intent intent = new Intent(con, OfferActivity.class);
getActivity().startActivity(intent);
}
});
As said before, the OfferActivity is working fine when displayed in one Tab, so I won't include the code of it.
Here is the FAB xml (inside a RelativeLayout): EDIT: included the whole file
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:layout_weight="0"
android:layout_marginTop="5dp"
android:foregroundGravity="fill"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/fab_margin"
android:layout_marginRight="#dimen/fab_margin"
android:src="#drawable/fab_add"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:baselineAlignBottom="true"
android:clickable="true"
app:backgroundTint="#color/colorPrimary" />
Thank you for answering and sorry if this questions got ask already! :)
EDIT: here is the styles.xml and I have included the whole FAB xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:actionBarDivider">#color/colorPrimary</item>
<!--<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>-->
</style>
<style name="MyMaterialTheme" parent="AppTheme">
<item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
<item name="android:windowAllowEnterTransitionOverlap" tools:targetApi="lollipop">true</item>
<item name="android:windowAllowReturnTransitionOverlap" tools:targetApi="lollipop">true</item>
<item name="android:windowSharedElementEnterTransition" tools:targetApi="lollipop">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition" tools:targetApi="lollipop">#android:transition/move</item>
</style>
The use of layout_baselineAlignBottom attribute is creating problem I think. The app:layout_anchor attribute enables us to specify the layout on which we wish to anchor our floating action button & app:layout_anchorGravity specifies the placement of the anchor. Place the following floating action button xml at the end just before </android.support.design.widget.CoordinatorLayout>.
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/fab_margin"
android:layout_marginRight="#dimen/fab_margin"
android:src="#drawable/fab_add"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:baselineAlignBottom="true"
android:clickable="true"
app:backgroundTint="#color/colorPrimary"
app:layout_anchor="#id/viewpager"
app:layout_anchorGravity="bottom|right|end" />
Please note: Floating Action button can only be used in conjunction with Coordinator layout.
Edit: Okay, wrap your Relative layout inside a coordinator layout, and change
app:layout_anchor="#id/viewpager" to app:layout_anchor="#id/listview" in floating action button 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout 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"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:layout_weight="0"
android:layout_marginTop="5dp"
android:foregroundGravity="fill"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:clickable="true"
app:backgroundTint="#color/colorPrimary"
app:layout_anchor="#id/list"
android:src="#drawable/fab_add"
android:layout_marginBottom="#dimen/fab_margin"
android:layout_marginRight="#dimen/fab_margin"
app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>
You could use that FloatingActionButton with CoordinatorLayout like this:
<CoordinatorLayout>
<AppbarLayout/>
<scrollableView/>
<FloatingActionButton/>
</CoordinatorLayout>
Something like this(you can use SupportLibrary or using it inside the CoordinatorLayout) this is the best way:
<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:id="#+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<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|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="#color/white"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="#drawable/ic_add"
app:backgroundTint="#color/ColorAccent"
app:borderWidth="0dp"
app:fabSize="normal"
app:layout_anchor="#id/coordinator"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
Hope something got messed up with the value-v21 style.xml.
Try adding the below set of properties in the value-v21 style.xml.
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:fitsSystemWindows">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
and add the below code to the fab fragment.
private int getNavigationBarHeight() {
Resources resources = getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
#Override
public void onCreateView(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int navBarHeight = getNavigationBarHeight();
findViewById(R.id.base_frame).setPadding(0, 0, 0, navBarHeight);
findViewById(R.id.menu_frame).setPadding(0, 0, 0, navBarHeight);
}
}
Similar issue here
Good Luck..!
Try this code :-
public class DeliveryTab extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
android.support.v4.app.FragmentManager mFragmentManager;
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getActivity().setTitle("Delivery");
View x = inflater.inflate(com.Weal.sachin.omcom.R.layout.activity_delivery_tab,null);
tabLayout = (TabLayout) x.findViewById(com.Weal.sachin.omcom.R.id.sliding_tabs_delivery);
viewPager = (ViewPager) x.findViewById(com.Weal.sachin.omcom.R.id.viewpager_delivery);
viewPager. setOffscreenPageLimit(3);
FloatingActionButton fab = (FloatingActionButton)x.findViewById(R.id.fabdelivery);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentTransaction t = getFragmentManager().beginTransaction();
Add_Delivery mFrag = new Add_Delivery();
t.replace(R.id.framelayout, mFrag);
t.commit();
}
});
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
return x;
}
class MyAdapter extends FragmentPagerAdapter {
com.Weal.sachin.omcom.TodayVisit TodayVisit;
Home home;
com.Weal.sachin.omcom.UpdatesFragment UpdatesFragment;
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
#Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new DeliveryToday();
case 1 : return new YesterdayDelivery();
case 2 : return new Delivery_all();
}
return null;
}
#Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Today";
case 1 :
return "Yesterday";
case 2 :
return "Delivery All";
}
return null;
}
}
}
Here the xml :-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.Weal.sachin.omcom.TabFragment"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs_delivery"
android:layout_width="match_parent"
android:layout_height="60dp"
app:tabTextColor="#d2cece"
app:tabSelectedTextColor="#fff"
android:background="#11977c"
/>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_delivery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<fragment android:name="com.Weal.sachin.omcom.TabFragment"
android:id="#+id/tabFragment"
android:layout_width="match_parent"
android:layout_height="100dp" />
</android.support.v4.view.ViewPager>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabdelivery"
android:layout_width="75dp"
android:layout_height="124dp"
android:layout_marginTop="50dp"
android:layout_gravity="bottom|end"
android:backgroundTint="#11977c"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/addplus"/>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>