Hello,
I have an activity with ViewPager in its layout. The ViewPager has 5 Fragments. The Fragments has a CollapsingToolbar with an image and a custom ListView (impementing NestedScrollChild). Now i want to show an option menu on Toolbar, but i can't get it to work. The menu isn't showing.
While searching for a solution (also here) i found out, that it is possible to inflate the menu in one of three codepositions:
1: in the onCreate(Bundle savedInstanceState) of the Fragment
2: in the View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) of the Fragment
3: in the onActivityCreated(Bundle savedInstanceState) of the Fragmnent
I have tried all of them, but the result is allways the same --> no option menu shown. I have placed a Log in the onCreateOptionsMenu(Menu menu, MenuInflater inflater) but the logcat never show the message, so the function was never reached.
Can somebody help me to find the right answer how to show the option menu?
Here the sources:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="de.example.test.MainActivity">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/part_navigation_toplevel"
android:id="#+id/pMain_INC_Toplevel" />
<android.support.v4.view.ViewPager
android:id="#+id/pMain_VP_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/pMain_INC_Toplevel"
android:background="#color/colorBackground" />
</RelativeLayout>
fragment_list.xml:
<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"
tools:context="de.example.test.MainActivity"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="#+id/pFragmentList_CC"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/pFragmentList_ABL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:background="#drawable/draw_bg_standard_element"
android:fitsSystemWindows="true"
android:layout_gravity="center_vertical"
app:contentScrim="#color/colorPrimary"
android:id="#+id/pFragmentList_CTB">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:background="#drawable/img_theme_blank"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<de.example.test.pakMainFragments.NestedScrollingListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="de.example.test.MainActivity"
android:id="#+id/pFragmentList_LV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="5.0sp"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:scrollbars="vertical"
android:paddingTop="#dimen/padding_normal" />
</android.support.design.widget.CoordinatorLayout>
menu_main_feed.xml:
<menu 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"
tools:context=".MainActivity">
<item
android:id="#+id/action_reload"
android:orderInCategory="100"
android:icon="#mipmap/ic_reload"
android:title="Reload"
app:showAsAction="ifRoom|collapseActionView"
/>
<item
android:id="#+id/action_length"
android:orderInCategory="200"
android:title="timeperiode"
app:showAsAction="ifRoom|collapseActionView"
/>
<item
android:id="#+id/action_showrelationships"
android:orderInCategory="300"
android:title="show relationships"
android:checkable="true"
app:showAsAction="ifRoom|collapseActionView"
/>
<item
android:id="#+id/action_showcomments"
android:orderInCategory="400"
android:title="show comments"
android:checkable="true"
app:showAsAction="ifRoom|collapseActionView"
/>
</menu>
ListFragment.java (in parts):
package de.example.test.pakMainFragments;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import de.example.test.R;
public class ListFragment extends Fragment {
// tested posibility 1:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_list,container, false);
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) v.findViewById(R.id.pFragmentList_CTB);
collapsingToolbarLayout.setTitle(getString(R.string.sTitleFeed));
NestedScrollingListView lvList = (NestedScrollingListView) v.findViewById(R.id.pFragmentList_LV);
/* removed for better reading */
// tested posibility 2: in onCreate()
setHasOptionsMenu(true);
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_main_feed, menu);
Log.e("ListFragment", "onCreateOptionsMenu");
super.onCreateOptionsMenu(menu, inflater);
}
// tested posibility 3:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
public static ListFragment newInstance(String sText) {
FeedFragment fList = new ListFragment();
Bundle bList = new Bundle();
bList.putString("msg", sText);
fList.setArguments(bList);
return fList;
}
}
Thank you!
EDIT
i use a style without ActionBar. Now i try a style with actionBar and i can see the option menu in the ActionBar of the MainActivity, but i want it in the Collapsing Toolbar of the Fragment... How i can solve this problem?
style.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
Style for Toolbar in Fragment is overwrítten in Fragment layout xml
Related
I have followed the material guideline here but for some reason, the bottom navigation is not working for me. My app simply displays a white frame where the bottom navigation should be. What am I doing wrong? Please note that I commented out the onCreateOptionsMenu in my MainActivity.java shown below. If I uncomment this code, the items in my menu_bottom_navigation.xml show up in the app bar menu but I want to have them show up in the bottom navigation bar.
Build.gradle
...
implementation 'com.google.android.material:material:1.1.0'
...
Menu resource:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/discover"
android:enabled="true"
android:title="#string/title_activity_discover_movies"
android:icon="#drawable/ic_search_24px"/>
<item
android:id="#+id/favorites"
android:enabled="true"
android:title="#string/title_activity_favorite_movies"
android:icon="#drawable/ic_favorite_24px" />
</menu>
Layout resource:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".features.movieoptions.movielist.MainActivity">
<include
layout="#layout/toolbar_discover_movies"
android:id="#+id/toolbar_movie_list" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
app:itemBackground="#color/colorPrimary"
app:itemTextColor="#color/colorTextIcons"
app:menu="#menu/menu_bottom_navigation"/>
</FrameLayout>
</LinearLayout>
MainActivity.java
package edu.bu.metcs.activitylifecycle.features.movieoptions.movielist;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import edu.bu.metcs.activitylifecycle.R;
import edu.bu.metcs.activitylifecycle.features.movieoptions.movielist.discover.DiscoverFragment;
import edu.bu.metcs.activitylifecycle.features.movieoptions.movielist.favorites.FavoritesFragment;
import edu.bu.metcs.activitylifecycle.shared.FragmentUtility;
/**********************************************************************************************************************
* This activity manages the apps fragments and decides which fragment should be displayed to the user.
*
* #author mlewis
* #version June 5, 2020
*********************************************************************************************************************/
public class MainActivity extends AppCompatActivity {
// Invariant of the MovieListActivity.java class
// 1. A shareActionProvider sends an implicit intent to apps capable of handling the text/plain MIME type.
// 2. The TAG is used by the Logcat for informational purposes.
private DiscoverFragment discoverFragment;
private FavoritesFragment favoritesFragment;
private static final String TAG = MainActivity.class.getSimpleName();
/**
* protected void onCreate(Bundle savedInstanceState)
* Performs initial Activity set up.
*
* #param savedInstanceState A bundle for any saved state from prior sessions that were destroyed.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
discoverFragment = DiscoverFragment.newInstance();
favoritesFragment = FavoritesFragment.newInstance();
// Show discover fragment by default
setUpFragment(discoverFragment);
}
setUpBottomNavigation();
setUpToolbar();
}
// #Override
// public boolean onCreateOptionsMenu(Menu menu) {
// super.onCreateOptionsMenu(menu);
// getMenuInflater().inflate(R.menu.menu_bottom_navigation, menu);
// return true;
// }
private void setUpFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentUtility.fragmentTransaction(fragmentManager, R.id.fragment_container, fragment);
}
private void setUpBottomNavigation() {
Log.d(TAG, "Setting bottom nav.");
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.discover:
Log.d(TAG, "discover was clicked.");
setUpFragment(discoverFragment);
return true;
case R.id.favorites:
Log.d(TAG, "favorites was clicked.");
setUpFragment(favoritesFragment);
return true;
}
return false;
}
});
}
private void setUpToolbar() {
Toolbar toolbar = findViewById(R.id.toolbar_movie_list);
setSupportActionBar(toolbar);
}
}
Please see the white section at the bottom of the screen to see the issue:
As i Checked your layout. i found the mistake
1. Fragment Container(Frame layout) having wrong margin bottom
2. Need to add wight for fragment container(Frame Layout)
Corrected code as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".features.movieoptions.movielist.MainActivity">
<include
layout="#layout/toolbar_discover_movies"
android:id="#+id/toolbar_movie_list" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:itemBackground="#color/colorPrimary"
app:itemTextColor="#color/colorTextIcons"
app:menu="#menu/menu_bottom_navigation"/>
</FrameLayout>
</FrameLayout>
</LinearLayout>
I am following Android tutorials by Google at Udacity. I tried to make a simple viewpager app with tabs, but tabs' names are not displayed. What's curious is that I did exactly the same coding with the tutorial, and these add code alone creates tabs on tutorial app just fine. Please help me. Thanks!
MainActivity:
package com.example.mari.viewpagerpracsec;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
CustomPagerAdapter adapter = new CustomPagerAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
Viewpager Adapter:
package com.example.mari.viewpagerpracsec;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
public class CustomPagerAdapter extends FragmentPagerAdapter {
private Context mContext;
public CustomPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
#Override
public int getCount() {return 3;}
#Override
public Fragment getItem(int position) {
if (position==0) {
return new FirstFragment();
}else if (position==1) {
return new SecondFragment();
}else if (position==2){
return new ThirdFragment();
}else {
return new FirstFragment();
}
}
#Override
public CharSequence getPageTitle(int position) {
if (position==0) {
return mContext.getString(R.string.first);
} else if (position==1) {
return mContext.getString(R.string.second);
} else {
return mContext.getString(R.string.third);
}
}
}
Fragment 1
package com.example.mari.viewpagerpracsec;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
public FirstFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
Fragment2
package com.example.mari.viewpagerpracsec;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment {
public SecondFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false);
}
}
Fragment 3
package com.example.mari.viewpagerpracsec;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ThirdFragment extends Fragment {
public ThirdFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_third, container, false);
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#color/colorPrimary"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="#style/ImageTab"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
fragment 1
<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"
tools:context="com.example.mari.viewpagerpracsec.FirstFragment">
<ImageView
android:id="#+id/image_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/first_image"
android:scaleType="centerCrop"/>
</FrameLayout>
fragment 2
<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"
tools:context="com.example.mari.viewpagerpracsec.SecondFragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/second_image"
android:scaleType="fitCenter"/>
</FrameLayout>
fragment 3
<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"
tools:context="com.example.mari.viewpagerpracsec.ThirdFragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/third_image"
android:scaleType="centerCrop"/>
</FrameLayout>
colors
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
strings
<resources>
<string name="app_name">View Pager Prac sec</string>
<string name="first">first</string>
<string name="second">second</string>
<string name="third">third</string>
</resources>
styles
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="actionBarStyle">#style/MiwokAppBarStyle</item>
<item name="android:windowContentOverlay">#null</item>
</style>
<!-- App bar style -->
<style name="MiwokAppBarStyle" parent="style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<!-- Remove the shadow below the app bar -->
<item name="elevation">0dp</item>
</style>
<!-- Style for a tab that displays a category name -->
<style name="ImageTab" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#android:color/white</item>
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextAppearance">#style/ImageTabTextAppearance</item>
<item name="tabBackground">#color/colorPrimary</item>
</style>
<!-- Text appearance style for a category tab -->
<style name="ImageTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textColor">#A8A19E</item>
</style>
</resources>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.mari.viewpagerpracsec"
minSdkVersion 22
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:design:25.3.1'
testCompile 'junit:junit:4.12'
}
In activity_main, your ViewPager have layout_height:match_parent. It's make your TabLayout doesn't have space on screen.
Your ViewPager should be like:
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
or You can just move the TabLayout to above of ViewPager
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#color/colorPrimary"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="#style/ImageTab"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Try with this layout insted of your activity_main layout.
<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"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Your viewpager's height is taking the whole screen space. Try to make it smaller not match_parent. Even you could add margin bottom like this.
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_marginBottom="60dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I have an activity with frame layout. When the options are clicked from the navigation drawer, it opens the specific fragment. I was trying to implement observablescrollview in my fragment. https://github.com/ksoichiro/Android-ObservableScrollView. I am able to implement it but I am not getting the hamburger icon in the actionbar. I realized it has to be bundled with the drawertoggle but that resides in my mainactivity. How do i get it in my fragment? Users may not even realize the presence of a navigation drawer without it. It was available when I was using a toolbar in app_bar_main.xml. I hope I have made myself clear. Please let me know if you require any other information. Help a newbie out. Thanks in advance.
Here's the screenshots.
Here's my code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="net.simplifiedcoding.navigationdrawerexample.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="net.simplifiedcoding.navigationdrawerexample.MainActivity"
tools:showIn="#layout/app_bar_main">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
fragment_menu_1.xml
<FrameLayout 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">
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="#dimen/parallax_image_height"
android:scaleType="centerCrop"
android:src="#drawable/example" />
<View
android:id="#+id/anchor"
android:layout_width="match_parent"
android:layout_height="#dimen/parallax_image_height"
android:minHeight="#dimen/parallax_image_height" />
<TextView
android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/anchor"
android:background="#android:color/white"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:text="#string/lipsum" />
</RelativeLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
<include layout="#layout/gradient_header" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/Theme.AppCompat.Light.DarkActionBar"
app:theme="#style/Toolbar" />
</FrameLayout>
Menu1.java
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollState;
import com.github.ksoichiro.android.observablescrollview.ScrollUtils;
import com.nineoldandroids.view.ViewHelper;
public class Menu1 extends Fragment implements ObservableScrollViewCallbacks {
private View mImageView;
private View mToolbarView;
private ObservableScrollView mScrollView;
private int mParallaxImageHeight;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
View v = inflater.inflate(R.layout.fragment_menu_1, container, false);
((AppCompatActivity)getActivity()).setSupportActionBar((Toolbar)v.findViewById(R.id.toolbar));
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
mImageView = v.findViewById(R.id.image);
mToolbarView = v.findViewById(R.id.toolbar);
mToolbarView.setBackgroundColor(ScrollUtils.getColorWithAlpha(0, getResources().getColor(R.color.primary)));
mScrollView = (ObservableScrollView) v.findViewById(R.id.scroll);
mScrollView.setScrollViewCallbacks(this);
mParallaxImageHeight = getResources().getDimensionPixelSize(R.dimen.parallax_image_height);
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("Menu 1");
}
#Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
int baseColor = getResources().getColor(R.color.primary);
float alpha = Math.min(1, (float) scrollY / mParallaxImageHeight);
mToolbarView.setBackgroundColor(ScrollUtils.getColorWithAlpha(alpha, baseColor));
ViewHelper.setTranslationY(mImageView, scrollY / 2);
}
#Override
public void onDownMotionEvent() {
}
#Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
}
}
MainActivity.java
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//add this line to display menu1 when the activity is loaded
displaySelectedScreen(R.id.nav_menu1);
}
#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);
}
private void displaySelectedScreen(int itemId) {
//creating fragment object
Fragment fragment = null;
//initializing the fragment object which is selected
switch (itemId) {
case R.id.nav_menu1:
fragment = new Menu1();
break;
case R.id.nav_menu2:
fragment = new Menu2();
break;
case R.id.nav_menu3:
fragment = new Menu3();
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
//calling the method displayselectedscreen and passing the id of selected menu
displaySelectedScreen(item.getItemId());
//make this method blank
return true;
}
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="Toolbar" parent="Theme.AppCompat">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primaryDark</item>
<item name="colorAccent">#color/accent</item>
</style>
</resources>
On your MainActivity#onCreate, before calling toggle.syncState(), call toggle.setDrawerIndicatorEnabled(true)
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.setDrawerIndicatorEnabled(true)
toggle.syncState();
I have a Navigation Drawer in which I have plenty of Menu Items. I have made a fragment "Home" and then on clicking on the Home menu item the Home Fragment should open.But it's not opening.
My java code is:
package com.example.hsports.weddingplanner.Activities;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import com.example.hsports.weddingplanner.Fragments.Home;
import com.example.hsports.weddingplanner.R;
public class FrontPage extends AppCompatActivity {
NavigationView navigationView;
DrawerLayout drawer;
Toolbar toolbar;
String nameOnTitleBar[];
int indexSelected=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_front_page);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer=(DrawerLayout)findViewById(R.id.drawer_layout);
navigationView=(NavigationView)findViewById(R.id.nav_view);
nameOnTitleBar=getResources().getStringArray(R.array.nav_list_items);
setupNavigationView();
}
private void setupNavigationView() {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.Home:
indexSelected=0;
markTheItemSelected(indexSelected);
changeNameofTitleBar(indexSelected);
fragmentTodisplay(indexSelected);
break;
case R.id.SignIn:
indexSelected=1;
markTheItemSelected(indexSelected);
changeNameofTitleBar(indexSelected);
fragmentTodisplay(indexSelected);
break;
case R.id.SignOut:
indexSelected=2;
markTheItemSelected(indexSelected);
changeNameofTitleBar(indexSelected);
fragmentTodisplay(indexSelected);
break;
case R.id.AboutUs:
indexSelected=3;
markTheItemSelected(indexSelected);
changeNameofTitleBar(indexSelected);
fragmentTodisplay(indexSelected);
break;
case R.id.ContactUs:
indexSelected=4;
markTheItemSelected(indexSelected);
changeNameofTitleBar(indexSelected);
fragmentTodisplay(indexSelected);
break;
}
drawer.closeDrawers();
return true;
}
});
ActionBarDrawerToggle actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawer,toolbar,R.string.openDrawer,R.string.closeDrawer){
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
actionBarDrawerToggle.syncState();
}
private void fragmentTodisplay(int indexSelected) {
switch (indexSelected)
{
case 0:
Home obj=new Home();
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frame,obj,"HOME");
transaction.commit();
break;
}
}
private void markTheItemSelected(int indexSelected) {
navigationView.setCheckedItem(indexSelected);
}
private void changeNameofTitleBar(int indexSelected) {
toolbar.setTitle(nameOnTitleBar[indexSelected]);
}
}
And the fragment code is as follows:
Home.java
package com.example.hsports.weddingplanner.Fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.hsports.weddingplanner.R;
/**
* Created by I324671 on 11/27/2016.
*/
public class Home extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragmentaboutus,container,false);
}
}
my navoigation_drawer.xml is this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single"
>
<item
android:id="#+id/Home"
android:title="HOME"
/>
<item
android:id="#+id/SignIn"
android:title="SIGN-IN"
/>
<item
android:id="#+id/SignOut"
android:title="SIGN-OUT"
/>
</group>
<item android:title="OTHER">
<menu>
<item
android:id="#+id/AboutUs"
android:title="ABOUT US"/>
<item
android:id="#+id/ContactUs"
android:title="CONTACT US"
/>
<item
android:id="#+id/Share"
android:title="SHARE"
/>
</menu>
</item>
</menu>
fragmentaboutus.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home"
/>
</LinearLayout>
Now when I click on Home icon in the navigation drawer the particular Home fragment doesn't gets displayed.
This is app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
>
<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:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbar"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:background="#color/blue"
>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frame"
></FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Activity_front_page.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="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:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/navigation_drawer"
android:id="#+id/nav_view"
></android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Try replacing
transaction.add(R.id.frame, obj, "HOME");
with
transaction.replace(R.id.frame, obj, "HOME");
I am facing a problem with android 5.0 and above. Whenever I align any view to parent bottom, Content is getting hidden behind the soft navigation button.
Above attached is the screenshot of the screen generated after creating new project from Android Studio 1.5. I just added an edit text at the bottom of the fragment.
as you can see, half of the edit text is behind the navigation bar.
Though codes are auto generated by Android Studio, I am still posting the codes below.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trial.exps.tabbedtrial">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.trial.exps.tabbedtrial.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
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|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</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" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.trial.exps.tabbedtrial.MainActivity$PlaceholderFragment">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Put Some Text"
android:layout_alignParentBottom="true"
android:background="#drawable/curved_edges"/>
</RelativeLayout>
curved_edges.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff" />
<stroke android:width="1dp" android:color="#454545" />
<padding android:left="6dp" android:top="6dp" android:right="6dp" android:bottom="6dp" />
<corners android:radius="6dp" />
</shape>
MainActivity.java
package com.trial.exps.tabbedtrial;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
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();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
P.S. If removing or replacing the drawable is part of your solution, then I have already tried it. And I couldnt find any proper solution in Android guidelines too.
android:fitsSystemWindows="true"
This worked for me. The view no longer overlaps with soft navigation.
I don't have a off-the-shelve solution, but this might be of help. The activity_main.xml root element states it should fit the system window: android:fitsSystemWindows="true".
I guess you'll have to find a way to detect whether the bottom navbar is on screen, and if true, add extra margin-bottom to your activity layout.
android:fitsSystemWindows="true" did not solve on my layout. I had to add respective attributes to the partially hidden top and bottom views.
android:paddingTop="?android:attr/actionBarSize"
android:paddingBottom="?android:attr/actionBarSize"
E.g on top:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/myid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:paddingTop="?android:attr/actionBarSize">
...