I have a MainActivity XML that contains a bottomNavigationView (bar) with 5 tabs. Each tab calls a different fragment. Switching from a fragment with a collapsing toolbar in its XML (labelled A in picture) to a fragment with a plain fragment (labelled B in picture) causes the bottomNavigationView (bar) to hide partially off the screen.
Is there a way of preventing this?
Screen with fragment containing collapsing toolbar.
Screen of another tab with a plain fragment.
Fragment Class containing code for collapsing toolbar
public class Profile extends Fragment {
public Profile() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
Toolbar toolbar = rootView.findViewById(R.id.toolbar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbar = rootView.findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("test");
return rootView;
}
Corresponding xml containing collapsing toolbar code
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ani_dog_one"
android:contentDescription="whut"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Plain fragment class
public class Review extends Fragment {
public Review() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_review, container, false);
//setHasOptionsMenu(true);
return rootView;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_bar_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
XML for plain fragment class contains just a plain FrameLayout
MainActivity
public class MainActivity extends AppCompatActivity {
private Intent intent;
private android.support.v4.app.FragmentManager manager;
private android.support.v4.app.FragmentTransaction transaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
Discover discoverFragment = new Discover();
transaction.replace(R.id.container, discoverFragment, discoverFragment.getTag()).commit();
setupBottomNavigationView();
}
private void setupBottomNavigationView() {
BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottom_navigation);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
BottomNavigationViewHelper.enableNavigation(this, bottomNavigationViewEx);
}
public void goToOptions(MenuItem menu) {
intent = new Intent(this, Options.class);
overridePendingTransition(R.anim.left_in, R.anim.right_out);
startActivity(intent);
}
}
XML for MainActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.project_qwer.MainActivity">
<!-- main fragments goes here -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<!-- bottom navigation view -->
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/bottom_navigation_menu" >
</com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx>
</LinearLayout>
Try to remove
android:fitsSystemWindows="true"
on your CollapsingToolbarLayout.
try
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
when you switch to tab with collapsing toolbar and
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
when switch to another
Try to put
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
</android.support.design.widget.CoordinatorLayout>
as the root of all the other fragments you are loading from the BottomNavigationView.
If you use a custom toolbar in the profile fragment,you put this code to onCreateView method:
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
I had the same problem and this was solved.
Remove android:fitsSystemWindows="true"
from root CoordinatorLayout.
Related
I have been using the NavigationDrawer activity. I have been calling the fragment from the activity. It gets called successfully but the actionbar is not showing up.
Code: Gallery_Frag - an activity created to extend teh fragment that i want to show up.
public class Gallery_Frag extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_bar_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new GalleryFragment()).commit(); }
}
CODE: GalleryFragment
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
/* final TextView textView = root.findViewById(R.id.text_gallery);
galleryViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});*/
return root;
}}
CODE: manifest
<activity android:name=".Gallery_Frag"
android:label="My Bookings"
android:theme="#style/AppTheme.NoActionBar"/>
CODE:app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
CODE: content_main
<?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:showIn="#layout/app_bar_main">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</RelativeLayout>
You may have a container inside this content_main.xml;
if yes, pass that id to the add(R.id.container_id, new GalleryFragment())...
Otherwise change <include layout="#layout/content_main" /> to
<include layout="#layout/content_main"
android:id="#+id/fragment_container"/>
and use
add(R.id.fragment_container, new GalleryFragment())...
First of all, you have to set layout to Activity using setContentView which contain Toolbar
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set it first
setContentView(R.layout.your_activity);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(R.id.content, new GalleryFragment()).commit(); }
}
Got it
In your content_main.xml file, add an id to the parent relative layout like below,
<RelativeLayout
android:id="#+id/fragment_container"
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:showIn="#layout/app_bar_main">
And replace,
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new GalleryFragment()).commit();
with
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new GalleryFragment()).commit();
This will hopefully work.
I am building android shopping app that have the cart menu item and all products are opening in fragment and i want to set the MainActivity menu items on the product fragment and other fragments.
So the user is easy to open the cart from the product view fragment.below code
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.product_view_fragment, null);
setHasOptionsMenu(true);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
getActivity().invalidateOptionsMenu();
toolbar = (Toolbar)view.findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getActivity().onBackPressed();
}
});
this.context = getContext();
String id=getArguments().getString("message");
//String size=getArguments().getString("size");
viewPager = (ViewPager)view.findViewById(R.id.viewPager);
SessionManager sessionManager = new SessionManager(getContext());
email = sessionManager.getUserDetails().get("id");
sliderDotspanel = (LinearLayout)view.findViewById(R.id.SliderDots);
GetOneProduct(id);
return view;
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:segmentedgroup="http://schemas.android.com/tools"
android:orientation="vertical"
android:clickable="true"
android:background="#color/colorWhite"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#android:style/Animation.Activity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:navigationIcon="?attr/homeAsUpIndicator"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:titleTextColor="#color/Black" />
</android.support.design.widget.AppBarLayout>
Blockquote
I assume you use a single activity approach.
Then your activity should include Toolbar in its layout something like this.
<android.support.constraint.ConstraintLayout 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=".navigation.HomeActivity">
<androidx.appcompat.widget.Toolbar></androidx.appcompat.widget.Toolbar>
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/app_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView>
// if you have bottom nav.
</com.google.android.material.bottomnavigation.BottomNavigationView>
Then set up the toolbar as you wish in your activity.
navigation drawer with fragments
1 - home
2 - courses
3 - gallery
i created 3 fragments for home,courses,gallery resp.
when app open home fragment is going to show
when i click on courses from navigation drawer coursesFragment will open within this fragment i created tab layout and tab layout is showing correctly as i needed, but drawer toggle icon is not there but drawer is opening when i pull it from left side
MainActivity.java :
public class MainActivity extends AppCompatActivity {
DatabaseHelper databaseHelper;
protected DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container,new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home fragment");
NavigationView navigationView= (NavigationView) findViewById(R.id.navview);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.Home:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home fragment");
item.setChecked(true);
break;
case R.id.gallery:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new GalleryFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("gallery fragment");
item.setChecked(true);
break;
case R.id.courses:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new CoursesFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("courses fragment");
item.setChecked(true);
break;
}
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
}
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: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:layout_height="match_parent"
android:layout_gravity="left"
tools:context="com.navdrawer.navdrawer.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar_layout"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_container">
</FrameLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/navview"
android:layout_gravity="start"
app:menu="#menu/drawer_menu"
app:headerLayout="#layout/navigation_drawer_header"
android:scrollbars="vertical"
>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="-20dp"
android:layout_marginLeft="-20dp"
android:layout_gravity="bottom"
android:src="#drawable/iso_main1"
/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
CoursesFragment.java :
public class CoursesFragment extends Fragment {
public CoursesFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_courses, container, false);
Toolbar toolbar=(Toolbar)view.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Download");
TabLayout tabLayout=(TabLayout)view.findViewById(R.id.courses_tabl);
ViewPager viewPager=(ViewPager)view.findViewById(R.id.courses_viewpager);
ViewpagerAdapter viewpagerAdapter=new ViewpagerAdapter(getChildFragmentManager());
viewpagerAdapter.addFragments(new DownloadFragment(),"Download");
viewpagerAdapter.addFragments(new AlreadyDownlodedFragment(),"Downloaded");
viewPager.setAdapter(viewpagerAdapter);
tabLayout.setupWithViewPager(viewPager);
return view;
}
fragment_courses.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.navdrawer.navdrawer.CoursesFragment">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/courses_appbar_layout"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar_layout"
/>
</LinearLayout>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/courses_tabl"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorColor="#android:color/white"
>
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/courses_viewpager"
>
</android.support.v4.view.ViewPager>
</FrameLayout>
here is my fragment_gallery.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"
tools:context="com.navdrawer.navdrawer.GalleryFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Gallery"
android:gravity="center"
android:textSize="30sp"
/>
</FrameLayout>
GalleryFragment:
public class GalleryFragment extends Fragment {
public GalleryFragment() {
// 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_gallery, container, false);
}
}
here is screenshots :
Can i see Gallery fragment's xml ?
BTW, why are you using a toolbar again in your courses fragment ?
This must be the issue.. Remove the toolbar from there as it must be overriding the parent activity's toolbar.
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.navdrawer.navdrawer.CoursesFragment">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/courses_tabl"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorColor="#android:color/white"/>
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/courses_viewpager"/>
This is what you need here
Also make this change in your main_activity.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar_layout"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_container">
</FrameLayout>
You can set Home button as follows
getSupportActionBar().setHomeButtonEnabled(true);
add this line after setSupportActionBar(toolbar);
If you want set your Custom Icon then you can set as follows
toolbar.setNavigationIcon(R.drawable.myhome);//pass id of your navigation icon
I have a viewpager inside in a activity. This activity launch three fragements (not tabbed), as created three different class extends fragment. It is working fine with horizontal scrolling in between fragment.
Now I want to use collapsingToolbar with separate image display in fragment.
Activity - having viewpager, Fragment1 XML having collapsingToolbar, Fragement1 also have 'content_launch.xml' Fragement1 class - toolbar added but while fetching (CollapsingToolbar object it is returning null.
Activity XML as below :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" tools:context=".activity.LaunchActivity">
<android.support.v4.view.ViewPager
android:id="#+id/launchpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</LinearLayout>
Activity Class to launch viewpager
ViewPager viewPager = (ViewPager) findViewById(R.id.launchpager);
viewPager.setAdapter(new LaunchAdapter(getSupportFragmentManager
(),getApplicationContext()));
Fragement1 XML
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/quote_coordinator"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#android:color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="#dimen/detail_backdrop_height"
android:id="#+id/quote_appbar"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/quote_collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim ="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="#android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/quote_backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode ="parallax"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal" android:orientation="vertical">
<TextView android:id="#+id/quote_main_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_title"
android:textColor="#android:color/white"
android:textSize="#dimen/main_title"/>
<TextView android:id="#+id/quote_sub_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_sub_title"
android:textColor="#android:color/white"
android:textSize="#dimen/main_sub_title"/>
</LinearLayout>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/quote_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_quote"/>
</android.support.design.widget.CoordinatorLayout>
Fragment1 Class:
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar quoteToolbar = (Toolbar) getActivity().findViewById(R.id.quote_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(quoteToolbar);
final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) getActivity().findViewById(R.id.quote_collapsing_toolbar);
}
collapsingToolbar object returned as null, not able to find any suggestions, can someone suggest possible way to get object..
First thing: you should not use onCreate to initialize view elements in fragment, you should use onCreateView instead. Second one you don't use getActivity() context.
So instead of this code:
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar quoteToolbar = (Toolbar) getActivity().findViewById(R.id.quote_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(quoteToolbar);
final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) getActivity().findViewById(R.id.quote_collapsing_toolbar);
}
use this:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
// inflate your layout.
View view = inflater.inflate(R.layout.yourlayout, container, false);
Toolbar quoteToolbar = (Toolbar) view.findViewById(R.id.quote_toolbar);
((AppCompatActivity) view).setSupportActionBar(quoteToolbar);
final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) view.findViewById(R.id.quote_collapsing_toolbar);
return view
}
Best regards and happy new year! :)
Right now my app works fine at regular screen.
I am having a tough time figuring out how I can manage my current UI to handle the two panel layout.
Right now my DetailActivity has some UI elements to load, 3 fragments on separate tabs, controls what each FAB does, FAB animation and a fragment page adapter for the tab layout.
What should I do to manage all of these elements and logics in a separate class and also in the main activity for the two pane layout?
Here is my Main Activity
public class MainActivity extends AppCompatActivity {
public static boolean TWO_PANE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.movie_detail_container) != null) {
// The detail container view will be present only in the large-screen layouts
// (res/layout-sw600dp). If this view is present, then the activity should be
// in two-pane mode.
TWO_PANE = true;
if (savedInstanceState == null) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}else{
TWO_PANE = false;
}
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FragmentMain())
.commit();
}
Fragment Main displays a movie grid layout, when clicked if it's regular phone open another activity, otherwise the movie details should display at the right corner.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Defines the xml file for the fragment
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
imageAdapter = new ImageAdapter(getContext());
gridView = (GridView) rootView.findViewById(R.id.gridView);
gridView.setAdapter(imageAdapter);
//Start DetailActivity with the movie details.
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if (MainActivity.TWO_PANE){
Bundle bundle = new Bundle();
bundle.putParcelable(Constants.BUNDLE_CONSTANT, mMovieElements.get(position));
FragmentDetail fragment = new FragmentDetail();
fragment.setArguments(bundle);
getChildFragmentManager()
.beginTransaction()
.replace(R.id.movie_detail_container, fragment)
.addToBackStack(null)
.commit();
}else{
//Starting detail activity
Intent intent = new Intent(getContext(), DetailActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, mMovieElements.get(position));
startActivity(intent);
}
}
});
return rootView;
}
land\Activity-main:
<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:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
tools:context=".ui.ui.MainActivity">
<!--
This layout is activity_main two-pane layout for the Items master/detail flow.
-->
<FrameLayout
android:id="#+id/container"
class = "com.joaonogueira.nmovies.ui.ui.FragmentMain"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="#android:layout/list_content" />
<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:name="com.joaonogueira.nmovies.ui.ui.FragmentDetail"
android:layout_width="0dp"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_weight="4"
android:id="#+id/movie_detail_container"
tools:context="com.joaonogueira.nmovies.ui.ui.DetailActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:expandedTitleGravity="center"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:id="#+id/posterImage"
android:background="#drawable/nopicture"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/MovieContainer"
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_margin="#dimen/fab_margin"
android:src="#android:drawable/btn_star_big_off"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end"
app:backgroundTint="#color/colorPrimaryDark" />
</android.support.design.widget.CoordinatorLayout>
My Fragment Detail doesn't start, I am getting the following error:
No view found for id 0x7f0c0070 (com.joaonogueira.nmovies:id/movie_detail_container) for fragment FragmentDetail{1b71bc14 #0 id=0x7f0c0070 bundle}
You are trying to replace a fragment from the fragment itself
getChildFragmentManager()
.beginTransaction()
.replace(R.id.movie_detail_container, fragment)
.addToBackStack(null)
.commit();
But since movie_detail_container is not present in the fragment view and is a part of the activity layout instead, hence you are getting that error.
What you need to do is to have a listener interface implemented in your activity and make your fragment listen to that. Finally, you invoke that listener to make your activity replace a new fragment.
If you don't know how to do that read this.