How to put custom toolbar inside fragment? - java

I created custom toolbar inside fragment,but i got view like this:
In themes.xml i disabled ActionBar:
<style name="Theme.Project" parent="Theme.MaterialComponents.DayNight.NoActionBar">
This is my custom toolbar layout:
<?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"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/white"
>
<TextView
android:id="#+id/toolbar_title"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Home"
android:fontFamily="#font/google_sans"
android:textColor="#color/black"
android:textSize="20sp" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/fragment_home" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And this is HomeFragment.java:
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.toolbar_in_home, container, false);
return root;
}
}
How to remove this white thing on top of screen?
UPD:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
</androidx.constraintlayout.widget.ConstraintLayout>
This is fragment home(just ConstraintLayout)
UPD2:i also was trying use toolbar in activity_main and it works:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toolbar);
BottomNavigationView navView = findViewById(R.id.nav_view);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_examination, R.id.navigation_profile)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(navView, navController);
}
But i can't do changes if i want to go on other fragment.
I need to set another custom toolbar layout with button like this:

So this is how I did it. My app is based on single activity-multi-fragment model.
I had set activity theme to AppTheme.NoActionBar, so it enabled me to set a custom toolbar.
Later for each fragment I had a separate layout and within which added a custom toolbar like below
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary">
....
....
</androidx.appcompat.widget.Toolbar>
Within this layout I had added menu items.
If you don't want to follow this, may be you should look at getActionBar().setCustomView(). Keep your activity theme set, so getActionBar() is not null.
You can control action bar layout by calling getActionBar().setCustomView() in fragment onResume() or probably when you are adding or removing a fragment in your activity.

You can use your custom toolbar inside the main layout replace it from com.google.android.material.appbar.AppBarLayout.
Here is an example code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="#color/colorPrimary">
<ImageView
android:id="#+id/navi"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_marginStart="20dp"
android:contentDescription="#string/todo"
android:src="#drawable/navi" />
<ImageView
android:layout_width="180dp"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:contentDescription="#string/todo"
android:src="#drawable/header2" />
</RelativeLayout>
<include layout="#layout/fragment_home" />
</RelativeLayout>

Related

Fragment replaces entire screen

I have the following structures and fragment replace/add does not only replace its host view but whole screen. What can cause this?
Main Activity layout without data
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.MainActivity">
<FrameLayout
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="12"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="1"
app:labelVisibilityMode="unlabeled"
android:background="#drawable/transparent_button"
app:onNavigationItemSelected="#{viewModel::onNavigationItemSelected}"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
app:menu="#menu/bottom_navigation_menu" />
</LinearLayout>
Fragment layout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize"
tools:context=".fragment.HomeFragment">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="20dp"
android:background="#drawable/transparent_button"
android:onClick="#{viewModel::onFloatingButtonClicked}"
android:src="#drawable/ic_baseline_add_24" />
</FrameLayout>
Main activity onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mainViewModel = ViewModelProviders.of(this, new ViewModelFactory(getApplication())).get(MainViewModel.class);
activityMainBinding.setViewModel(mainViewModel);
activityMainBinding.executePendingBindings();
fragmentList.put(FragmentsEnum.HOME.getValue(), new HomeFragment());
fragmentList.put(FragmentsEnum.PROFILE.getValue(), new ProfileFragment());
Fragments.addFragment(getSupportFragmentManager(), R.id.nav_host_fragment, fragmentList.get(FragmentsEnum.HOME.getValue()), FragmentsEnum.HOME.getValue()); // DEFAULT FRAGMENT
}
Fragments util
public static void addFragment(FragmentManager manager, int containerId, Fragment fragment, String tag) {
if (!manager.isStateSaved()) {
manager.beginTransaction().add(containerId, fragment, tag).commit();
}
}
On onview of main activity I am setting Home Fragment active when the activity starts. But it hides my bottom nav bar and replaces entire screen.
I think that this is a problem with the usage of LinearLayout as the root element for the fragment and bottomnav. Try replacing it with either ConstraintLayout or CoordinatorLayout. If none of those work try removing the top padding from the Fragment.
Your FrameLayout will just overlap the BottomNavigationView. Furthermore you should use the FragmentContainerView for your nav host. You'd want to have your FragmentContainerView explicitly above your BottomNavigationView. I changed your LinearLayout to a ConstraintLayout with proper constraints:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#+id/bottom_nav"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#drawable/transparent_button"
app:onNavigationItemSelected="#{viewModel::onNavigationItemSelected}"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
app:menu="#menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>

Empty space on top of toolbar. Android, Java

I just included an options menu in my app, but in one of my activities it seems to create a white empty space- can anyone tell me why??
It work in other activities in the same app, but not this single activity.
I Hope anyone can help :-)
public class GifsActivity extends OptionsMenu {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gifs);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ImageView gifImageView = (ImageView) findViewById(R.id.iv_gif);
Glide.with(this)
.load("http://i.imgur.com/Vth6CBz.gif")
.asBitmap()
.placeholder(R.drawable.ic_cloud_off_red)
.error(R.drawable.ic_cloud_off_red)
.into(gifImageView);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center_vertical"
android:orientation="vertical"
tools:context="com.example.examand1.GifsActivity">
<include layout="#layout/toolbar_layout"
/>
<ImageView
android:id="#+id/iv_gif"
android:layout_width="match_parent"
android:layout_height="220dp" />
</RelativeLayout>
Picture of the activity
Remove android:gravity="center_vertical" from your layout. Then your layout will be as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/include"
layout="#layout/toolbar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<ImageView
android:layout_below="#id/include"
android:id="#+id/iv_gif"
android:layout_width="match_parent"
android:layout_height="220dp" />
</RelativeLayout>

Collapsing toolbar not showing Title Text

I'm facing a weird issue related to Collapsing toolbar. When I rotate the screen, the collapsing toolbar hides the toolbar title when the collapsing toolbar is contracted.
The toolbar title shows up when the collapsing toolbar is expanded
Please screen the screenshots here: https://imgur.com/a/99msG
<?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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<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:expandedTitleMarginEnd="#dimen/collapsing_toolbar_title_margin_end"
app:expandedTitleMarginStart="#dimen/collapsing_toolbar_title_margin_start"
app:expandedTitleTextAppearance="#style/CollapsedAppBarTextAppearance"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="#drawable/world_image"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/list_countries"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_color"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Java code:
public class CountryListFragment extends Fragment {
#BindView(R.id.list_countries)
RecyclerView mCountriesListRecyclerView;
#BindView(R.id.toolbar)
Toolbar mToolbar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_countries_list, container, false);
//must bind for View Injection
ButterKnife.bind(this, view);
// register event bus to receive events
EventBus.getDefault().register(this);
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
initRecyclerView();
populateRecyclerViewAdapter();
return view;
}
}
From the looks of it, the collapsing toolbar gets confused about the orientation of the device and animates the toolbar downwards instead of animating it upwards.

Activity behind DialogFragment still accepts touches

I have the following DialogFragment
public class DetailItemFragment extends AppCompatDialogFragment {
#BindView(R.id.task_detail_name)
AppCompatTextView taskDetailNameText;
#BindView(R.id.task_detail_description)
AppCompatTextView taskDetailDescriptionText;
#BindView(R.id.task_detail_priority)
AppCompatTextView taskDetailPriorityText;
#BindView(R.id.toolbar)
Toolbar toolbar;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true); //doesn't work
dialog.setCancelable(true); //doesn't work
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_detail_view, container, false);
ButterKnife.bind(this, view);
//Get the Json back and parse it as a Task
Bundle bundle = getArguments();
final String taskAsJson = bundle.getString("task");
Task task = new Gson().fromJson(taskAsJson, Task.class);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.close);
}
setHasOptionsMenu(false);
taskDetailNameText.setText(task.getName());
taskDetailDescriptionText.setText(task.getDescription());
taskDetailPriorityText.setText(task.getPriority().toString());
return view;
}
I perform the Fragment transaction like so
//Put the clicked item into a Bundle for the next fragment to consume
DialogFragment detailItemFragment = new DetailItemFragment();
Bundle args = new Bundle();
args.putString("task", new Gson().toJson(mTaskListAdapter.getItem(position)));
detailItemFragment.setArguments(args);
getActivity().getSupportFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.add(R.id.task_detail_fragment_container, detailItemFragment)
.addToBackStack(null)
.commit();
I'm trying to accomplish the following:
* On a tablet/large screen, this displays as a Dialog popup
* On a phone, it will display a fullscreen dialog (pretty much trying to replicate how Google Calendar handles opening event details)
This is what my xlarge/activity_main.xml looks like
<?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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/task_detail_fragment_container"
android:layout_width="800dp"
android:layout_height="400dp"
android:layout_centerInParent="true"
android:elevation="16dp" />
<android.support.design.widget.AppBarLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
style="#style/ToolbarStyle"
android:background="#color/colorPrimary"
app:title="#string/app_name"
app:titleTextColor="#color/white" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/task_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="#dimen/fab_normal_size"
android:layout_height="#dimen/fab_normal_size"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="#dimen/fab_padding"
android:layout_marginEnd="#dimen/fab_padding"
android:src="#drawable/add_black"
app:elevation="#dimen/fab_elevation"
app:fabSize="normal"
app:layout_anchorGravity="bottom|right|end"
app:pressedTranslationZ="#dimen/fab_pressed_elevation" />
</RelativeLayout>
You'll notice I have a FrameLayout that is centered on the screen for loading the DetailItemFragment into. Maybe this isn't the correct way to do this--but I want to bring it up in case that turns out to be the issue.
This is what the fragment_detail_view.xml layout looks like
<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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/task_detail_background"
android:clickable="true"
android:elevation="8dp"
tools:context="io.havoc.todo.view.fragments.DetailItemFragment">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_tall_height"
android:background="?attr/colorPrimary">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/task_detail_name"
style="#style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:gravity="start|bottom"
android:padding="8dp"
android:text="Task detail"
android:textColor="#color/white"
android:textSize="24sp" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginStart="86dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.AppCompatTextView
style="#style/TextAppearance.AppCompat.Body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_margin"
android:text="#string/hint_task_description"
android:textColor="#color/colorAccent" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/task_detail_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Description text"
android:textColor="#color/text_primary"
android:textSize="18sp" />
<android.support.v7.widget.AppCompatTextView
style="#style/TextAppearance.AppCompat.Body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_margin"
android:text="#string/hint_task_priority"
android:textColor="#color/colorAccent" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/task_detail_priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Priority"
android:textColor="#color/text_primary"
android:textSize="18sp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
The issue
When the DialogFragment is open on a tablet, the Activity in the background still accepts touches (i.e. I can interact with the Activity as if the dialog wasn't even present); furthermore, I have no idea how to get the DialogFragment to dismiss when touching outside of it. I've tried the following:
dialog.setCanceledOnTouchOutside(true); (in the onCreateDialog() method)
dialog.setCancelable(true); (in the onCreateDialog() method)
dialog.setCanceledOnTouchOutside(false); (in the onCreateDialog() method)
getDialog().setCanceledOnTouchOutside(true); setCancelable(true); (leads to NullPointerExceptions--performed in onCreateView() for the DialogFragment)
I've gone through all the other questions like this I could find and nothing has worked for me--so clearly I'm doing something wrong.
I appreciate any and all help.
Change to .dialog.setCancelable(false);.
If it's a dialog with OK and Cancel buttons, you can use:
.setCancelable(closeActivity == true ? false : true)
The FrameLayout (task_detail_fragment_container) you are adding your DetailItemFragment TO has a different size then the parent, which means it will not cover the entire layout:
...
android:layout_width="800dp"
android:layout_height="400dp"
...
To fix this, make task_detail_fragment_container the same size as the parent layout (match_parent) and cap the size of the dialog in the dialog layout by introducing another Layout after your container layout:
<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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:clickable="true"
android:elevation="8dp"
tools:context="io.havoc.todo.view.fragments.DetailItemFragment">
<FrameLayout
andorid:width="800dp"
android:height="400dp"
android:layout_centerInParent="true">
<android.support.design.widget.AppBarLayout ...
//rest of your xlm here
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Change this
dialog.setCanceledOnTouchOutside(true);
with
dialog.setCanceledOnTouchOutside(false);

Toolbar not showing up?

I am trying to implement a toolbar in my activity but it doesn't seem to work. My MainActivity is a ViewPager, which contains Fragments 1 and 2. I tried to implement my toolbar directly into the Viewpager, this didn't work so instead I tried to wrap my Viewpager in a RelativeLayout and include the toolbar in that RelativeLayout, but this also doesn't work.
The layout for my toolbar (app_bar.xml):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/app_bar"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:theme="#style/CustomToolBarStyle"
android:title="Something"
android:logo="#drawable/ic_launcher">
</android.support.v7.widget.Toolbar>
this is how I've implemented the toolbar in my activity (activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="#+id/app_bar" layout="#layout/app_bar"/> <-----------
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</android.support.v4.view.ViewPager>
</RelativeLayout>
and this is what I've done in my activity's Java file to set the toolbar as the actionbar (MainActivity.java):
public class MainActivity extends AppCompatActivity {
ViewPager pager;
android.support.v4.app.FragmentManager fragmentManager;
FloatingActionButton fab;
Toolbar tb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.pager);
fab = (FloatingActionButton) findViewById(R.id.fab_main);
tb = (Toolbar) findViewById(R.id.app_bar);
fragmentManager = getSupportFragmentManager();
pager.setAdapter(new myAdapter(fragmentManager));
AdView mAdView = (AdView) findViewById(R.id.adView);
//AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice("YOUR_DEVICE_HASH")
//.build();
//do not make visible while testing!!
//mAdView.loadAd(adRequest);
setSupportActionBar(tb); <------------------
it would really appreciate it if someone is able to help me out. Have a nice day!
Vidal
Try to remove id from include tag and add android:layout_below="#+id/app_bar" to your ViewPager.
Change your toolbar height from wrap_content to you ?attr/actionBarSize as shown below:
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/app_bar"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/CustomToolBarStyle"
android:title="Something"
android:logo="#drawable/ic_launcher">
</android.support.v7.widget.Toolbar>
and position your viewpager below Toolbar as shown below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="#+id/app_bar" layout="#layout/app_bar"/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/pager"
android:layout_below="#id/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
Hope this helps!

Categories

Resources