So I set in styles.xml for AppTheme NoActionBar, because I want Toolbar only in a few Activites and also I create post_details_menu with two items. But toolbar not showing at all.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Then in Manifest.xml, I have the theme: AppTheme
<application
android:theme="#style/AppTheme"
...>
</application>
Then i created my_toolbar:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"/>
</LinearLayout>
And in im PostDetailsActivity i want use my Toolbar:
public class PostDetailActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_detail);
Toolbar toolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.post_details_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.post_Edit:
showMessage("post_edit clicked");
return true;
case R.id.post_Delete:
showMessage("post_delete clicked");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And finally this is my layout for ActivityPostDetails:
<androidx.core.widget.NestedScrollView
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=".PostDetailActivity"
android:background="#fff">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView ... />
<TextView ... />
<androidx.recyclerview.widget.RecyclerView .../>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
It's too simple. set your app bar layout in the XML file where you want to show the toolbar.
check below XML code:-
<androidx.core.widget.NestedScrollView
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=".PostDetailActivity"
android:background="#fff">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/AppTheme.ActionBar"
app:navigationIcon="#drawable/ic_back"
app:subtitleTextAppearance="#style/CustomSubTitleTextAppearance"
app:titleTextColor="#color/black" />
</com.google.android.material.appbar.AppBarLayout>
<ImageView ... />
<TextView ... />
<androidx.recyclerview.widget.RecyclerView .../>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
In your java code, you have to bind toolbar programmatically in PostDetailsActivity.java.
toolbar = findViewById(R.id.toolbar)
toolbar!!.setNavigationIcon(R.drawable.ic_back)
if (toolbar != null) {
setSupportActionBar(toolbar)
}
Xml structure something like this should solve the problem
<LinearLayout>
<Toolbar/>
<NestedScrollView>
//one parent view inside nestedscrollview
<AnylayoutType>
// other required views
</AnylayoutType>
</NestedScrollView>
<LinearLayout/>
With this approach your Toolbar won't move and menu option will always be available and nested sroll view will work as required.
And if you are sure your view hierarchy is not nested its better to use LinearLayout inside nested scrollview
Related
I have a custom created toolbar. But when I run it, the toolbar on the menu (toolbar_menu.xml) is overlapping with my custom created toolbar.
My Custom toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Currency Star"
android:textColor="#FFFFFF"
android:textSize="20dp"/>
</android.support.v7.widget.Toolbar>
My menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/swapper"
android:icon="#drawable/baseline_swap_vertical_circle_white_18"
android:title="Swap currency"
app:showAsAction="ifRoom"/>
<item android:id="#+id/settings"
android:title="Settings"
app:showAsAction="never"/>
</menu>
I have set NoActionBar in my manifest and also in styles.xml.
MainActivity.java
Toolbar myToolbar = (Toolbar)findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.toolbar_menu, menu);
return true;
}
There are two things you can do.
1- Remove TextView from inside the Toolbar in XML code and the title will be automatically set for you (Just you need to set it in java code to what you want)
2- If you want to have that TextView in XML code then you need to set title to empty in java code like below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tes);
Toolbar myToolbar = (Toolbar)findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
// Here set the title to empty so that the one set automatically app name will be empty
setTitle("");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.toolbar_menu, menu);
return true;
}
Try this way..
make separate layout for custom toolbar ..
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tool="http://schemas.android.com/tools"
android:background="#null"
android:layout_width="match_parent"
app:elevation="0dp"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetStart="0dp"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_5sdp"
>
<ImageView
android:id="#+id/ivLogo"
android:layout_width="#dimen/_25sdp"
android:layout_height="#dimen/_25sdp"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher_background"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="#+id/tvHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/black"
android:textSize="#dimen/_12sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageView
android:id="#+id/ivUserProfile"
android:layout_width="#dimen/_25sdp"
android:layout_height="#dimen/_25sdp"
android:src="#drawable/user_profile_icon"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
after that include this layout into activity-main..
<?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"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">
<include
android:id="#+id/amToolbar"
layout="#layout/app_toolbar"
/>
<FrameLayout
android:id="#+id/activity_main_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintTop_toBottomOf="#+id/amToolbar"
app:layout_constraintBottom_toTopOf="#id/bottom_navigation"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconTint="#drawable/bottom_navigation_colors"
app:itemTextColor="#drawable/bottom_navigation_colors"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
after that put this line..
Toolbar myToolbar = (Toolbar)findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
make sure you hide default actionbar and also make your change in this code..
i provide only my code..
As i see that you have the same problem with me. Here is my solution for this :
//your custom toolbar
Toolbar toolbar = binding.get().toolBarContainer.findViewById(R.id.toolbar);
//your custom title
TextView mTitle = toolbar.findViewById(R.id.toolbar_title);
mTitle.setText(...);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
//set displayshowtitle to false to disable title toolbar
activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
//set navigation if you need
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_left));
toolbar.setNavigationOnClickListener((View v) -> onBackClicked());
I wonder why such a strange behavior in the app attribute: elevation = "0dp" and how to solve the problem. I use the androidx library. The toolbar is transparent. Shadow remains. When I write an app: elevation = "0dp", the shadow disappears, but the toolbar buttons, including the "back" buttons, stop responding.
I tried to solve it in other ways: android: elevation = "0dp"
programmatically set getSupportActionBar (). setElevation (0); Does not help.
layout:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#color/back_gray">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:background="#color/transparent"
app:popupTheme="#style/AppTheme.PopupOverlay">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/iv_like"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#android:color/transparent"
android:src="#drawable/heart_outline"
android:visibility="visible"/>
<ImageButton
android:id="#+id/iv_shared"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#android:color/transparent"
android:src="#drawable/shared"
android:visibility="visible" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
.....>
<TextView
........./>
<TextView
........../>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
fragment.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate( R.layout.fragment_data, container, false );
toolbar = (Toolbar) rootView.findViewById( R.id.toolbar );
MainActivity activity = (MainActivity) getActivity();
if ( activity != null ) {
activity.setSupportActionBar( toolbar );
// activity.getSupportActionBar().setElevation(0);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled( true );
activity.getSupportActionBar().setHomeButtonEnabled( true );
activity.getSupportActionBar().setDisplayShowTitleEnabled( false );
toolbar.setNavigationOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
}
} );
}
}
Try the below code snippet instead of toolbar.setNavigationOnClickListener
#Override public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
getActivity().onBackPressed();
}
return super.onOptionsItemSelected(item);
}
hope this will solve your problem.
thanks
This is just because you have used FrameLayout as a parent - and also removed elevation from AppBarLayout, and due to this, Your ScrollView is overlapping your AppBarLayout as it is written below AppBarLayout. change your root to something else, for eg.
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:theme="#style/AppTheme.AppBarOverlay">
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/appBarLayout">
</ScrollView>
</RelativeLayout>
Background
I'm trying to implement a Drawer Navigation inside my fragment. I would prefer to have it there rather than in my main activity because I'm trying to handle toolbars per fragment and having the Drawerlayout coupled would make it easier to setup.
Problem
When I click on the hamburger home button, no drawer layout is displayed. How do I resolve this issue while keeping the drawerlayout inside my fragment.xml file instead of moving it to activity?
Things I've Tried
Followed the Android Navigation Drawer Guide
Made sure my xml layout hierarchy matches the requirements
fragment.xml
<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_article_topics"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/toolbar" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/articles_swipe_to_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/articles"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
<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:menu="#menu/navigation_article_topic" />
</android.support.v4.widget.DrawerLayout>
activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.main.MainActivity">
<LinearLayout
android:id="#+id/main_content_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Fragment.java
#BindView(R.id.drawer_article_topics)
DrawerLayout articleTopicDrawerLayout;
// Other code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_articles_refresh:
articleListingViewModel.startRefresh();
refresh();
return true;
case android.R.id.home:
articleTopicDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
setSupportActionBar(toolbar);
setDrawerIcon();
setupRecyclerView();
setupSwipeRefreshLayout();
}
private void setDrawerIcon() {
if (isAdded()) {
ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
}
}
The issue seemed to lie with DrawerLayout.openDrawer() and the Android Material Components library. Because I was inheriting Theme.MaterialComponents.Light.NoActionBar in my AppTheme, the Navigation Drawer did not open. Until they add a fix in, the workaround is to add this to your App Theme:
<item name="navigationViewStyle">#style/Widget.Design.NavigationView</item>
More information here:
https://github.com/material-components/material-components-android/issues/133
How can one combine the Toolbar and Tablayout when in landscape mode as shown in the youtube app? I can't seem to find a definitive answer for this.
My current layout:
<LinearLayout 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"
android:orientation="vertical"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark"/>
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabMode="fixed"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:background="#color/transparent"/>
</LinearLayout>
As mentioned above, Toolbar is a ViewGroup so a TabLayout can be made a child of this.
<LinearLayout 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"
android:orientation="vertical"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark"
android:title="Viewer">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:layout_gravity="center_horizontal"
app:tabMode="fixed"
app:tabGravity="center"/>
</android.support.v7.widget.Toolbar>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:background="#color/transparent"/>
</LinearLayout>
You can add a layout with components to your ActionBar (the classic one, not the
android.support.v7.widget.Toolbar)
First create a menu resource menu\top_menu.xml to be your actionbar menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/topBarTabLayout"
android:title=""
app:actionLayout="#layout/top_bar_tab_layout"
app:showAsAction="always" />
<!-- Other menu items here -->
</menu>
Then, create a layout called layout/top_bar_tab_layout.xml containing just one TabLayout component (you can add other components too!):
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab_layout_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="0dp"
android:paddingTop="0dp" />
To access this TabLayout component, create a reference inside your activity onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.top_menu, menu);
topBarTabLayoutMenuItem = menu.findItem(R.id.topBarTabLayout);
topBarTabLayout = (TabLayout) topBarTabLayoutMenuItem.getActionView();
//ADD #1 TAB WITH A ICON RESOURCE
topBarTabLayout.addTab(topBarTabLayout.newTab().setIcon(R.drawable.file));
//ADD #2 TAB WITH A ICON RESOURCE
topBarTabLayout.addTab(topBarTabLayout.newTab().setIcon(R.drawable.folder));
//This may be necessary to force tablayout to fill all space.
topBarTabLayout.setTabGravity(topBarTabLayout.GRAVITY_FILL);
//Add listener to do stuff!
topBarTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
//do whatever you want here, like selecting a viewpager item or open an intent
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
//do whatever you want here
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
//do whatever you want here
}
});
return true;
}
}
This should be enough to add a tabLayout to your actionBar. It will work with other components like spinner or textViews.
Also, if you are planning on setting this tablayout visible only when orientation is landscape, you have to do something like that:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {
topBarTabLayoutMenuItem.setVisible(true);
} else {
topBarTabLayoutMenuItem.setVisible(false);
}
}
I am using the new android design CollapsingToolbarLayout. However, I am unable to get the menu to appear. Also, the homeasup indicator does not appear.
Can someone point me in the right direction? Or is this simply not possible?
EDIT
So, I finally got my laptop back. Here is the code.
The Activity
public class CheeseDetailActivity extends AppCompatActivity {
public static final String EXTRA_NAME = "cheese_name";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Intent intent = getIntent();
final String cheeseName = intent.getStringExtra(EXTRA_NAME);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(cheeseName);
loadBackdrop();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
private void loadBackdrop() {
final ImageView imageView = (ImageView) findViewById(R.id.backdrop);
Glide.with(this).load(Cheeses.getRandomCheeseDrawable()).centerCrop().into(imageView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_player, menu);
return true;
}
}
The Layout
<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/appbar"
android:layout_width="match_parent"
android:layout_height="#dimen/detail_backdrop_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="24dp"
app:expandedTitleMarginEnd="48dp">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="#dimen/toolbar_top_margin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin"
app:borderWidth="#dimen/fab_border" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="24dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/card_margin">
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Info"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/cheese_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
The menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="ifRoom" />
</menu>
As we can see, there is no overflow, nothing. But when I press the physical menu button, the menu pops up fine.
So, I guess I was really really dumb. I used the wrong margin for the toolbar. android:layout_marginTop="#dimen/toolbar_top_margin"
The margin was supposed to be -48dp, but I was using 48dp.
I had this in my dimens. I forgot to change to toolbar_top_margin_neg after I refactored.
<dimen name="toolbar_top_margin">48dp</dimen>
<dimen name="toolbar_top_margin_neg">-48dp</dimen>