I have recently moved from material-1.0 to 1.2 (the problem is in place with 1.1 too!)
Now, every thing is ok in Light view, but in Dark view, there is no card background. See the attached pictures to see what I mean.
I am attaching my java and xml files so that you can kindly check.
MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
}
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
String theme = sharedPref.getString("theme", "Default");
// Toast.makeText(this, theme, Toast.LENGTH_LONG).show();
if (theme.equals("Dark")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else if (theme.equals("Light")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
}
}
super.onCreate(savedInstanceState);
mContext = getApplicationContext();
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Navigation Drawer
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
if (drawer != null) {
drawer.addDrawerListener(toggle);
}
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
if (navigationView != null) {
navigationView.setNavigationItemSelectedListener(this);
}
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
// Handle navigation view item clicks here.
switch (item.getItemId()) {
case R.id.nav_manage:
// Handle the tools action (for now display a toast).
drawer.closeDrawer(GravityCompat.START);
Intent intentSetting = new Intent(this, SettingsActivity.class);
startActivity(intentSetting);
return true;
case R.id.about_phocast:
// Handle the share action (for now display a toast).
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://rudrab.github.io/thePhocast")));
drawer.closeDrawer(GravityCompat.START);
// displayToast(getString(R.string.chose_share));
return true;
default:
return false;
}
}
}
activity_main.xml
appbar_main.xml
<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="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:title="#string/app_name" />
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="-2dp"
android:background="?attr/colorPrimary"
android:theme="#style/Theme.MaterialComponents.DayNight"
app:cardCornerRadius="4dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:contentDescription="#string/cityimg_desc"
android:scaleType="fitXY"
app:srcCompat="#drawable/property_image_3" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/myrect"
android:elevation="8dp"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingEnd="0dp"
android:text="#string/location_not_found"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
style.xml=style-night.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" />
</resources>
And I cant find out where the error is.
Kindly help/
Update: after #Soumik and #Gabriele's post
My updated theme is:
<?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:theme="#style/Theme.MaterialComponents.DayNight"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:title="#string/app_name" />
<com.google.android.material.card.MaterialCardView
style="#style/Widget.MaterialComponents.CardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="-2dp"
app:cardCornerRadius="4dp"
app:cardBackgroundColor="#color/colorPrimary"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:contentDescription="#string/cityimg_desc"
android:scaleType="fitXY"
app:srcCompat="#drawable/property_image_3" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/myrect"
android:elevation="8dp"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingEnd="0dp"
android:text="#string/location_not_found"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
The result of the changed xml file is updated, and a screenshot is added. Kindly check.
Use
app:cardBackgroundColor="#color/colorPrimary"
instead of
android:background="?attr/colorPrimary"
Related
My toggle button doesn't work, when I press him I see like it is pressed but there is not action.
I can open my drawer layout, it changes when swipe, but there is not action on its, why?
I set custom toolbar in the fragment, I know it could be the problem there but I try to solve it for weeks and still nothing :)
Thanks
Code in fragment
private fun drawerUsage() {
act = activity as AppCompatActivity
act.setSupportActionBar(layout.appBarLayout)
// act.supportActionBar!!.elevation = 0F;
val drawer = layout.drawerLayout
val navigationView = layout.navView
toggle = ActionBarDrawerToggle(
requireActivity(),
drawer,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
drawer.addDrawerListener(toggle)
toggle.syncState()
act!!.supportActionBar?.setDisplayHomeAsUpEnabled(true)
drawer.setViewScale(Gravity.START, 0.9f);
//set height scale for main view (0f to 1f)
drawer.setViewElevation(Gravity.START, 20f);
//set main view elevation when drawer open (dimension)
drawer.setViewScrimColor(Gravity.START, Color.TRANSPARENT);
//set drawer overlay coloe (color)
drawer.setDrawerElevation(Gravity.START, 20f);
//set drawer elevation (dimension
drawer.setRadius(Gravity.START, 25f);
//set end container's corner radius (dimension)
navigationView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.t1 -> Toast.makeText(requireContext(), "Clicked Item 1", Toast.LENGTH_SHORT)
.show()
R.id.t2 -> Toast.makeText(requireContext(), "Clicked Item 2", Toast.LENGTH_SHORT)
.show()
R.id.t3 -> Toast.makeText(requireContext(), "Clicked Item 3", Toast.LENGTH_SHORT)
.show()
}
true
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (toggle.onOptionsItemSelected(item)) {
return true
}
return super.onOptionsItemSelected(item)
}
}
fragment layout
<?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">
<com.infideap.drawerbehavior.Advance3DDrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".fragments.ViewPagerFragment"
tools:openDrawer="start">
<!-- <!–Toolbar–>-->
<!-- <include-->
<!-- android:id="#+id/toolbar"-->
<!-- layout="#layout/toolbar" />-->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#layout/layout_default_toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<!-- <com.google.android.material.appbar.MaterialToolbar-->
<!-- android:id="#+id/appBar"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="?attr/actionBarSize"-->
<!-- app:popupTheme="#style/Widget.AppCompat.PopupMenu.Overflow"-->
<!-- />-->
</com.google.android.material.appbar.MaterialToolbar>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/green"
android:fitsSystemWindows="true"
app:elevation="0dp"
app:itemIconPadding="10dp"
app:itemIconTint="#android:color/white"
app:itemTextColor="#android:color/white"
app:headerLayout="#layout/item_header"
app:menu="#menu/menu_drawer"
></com.google.android.material.navigation.NavigationView>
</com.infideap.drawerbehavior.Advance3DDrawerLayout>
</RelativeLayout>
default layout toolbar
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.MaterialToolbar
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:background="#color/white"
android:layout_width="match_parent"
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/text_size_17"
app:titleTextAppearance="#style/SemiBold.17"
android:textColor="#color/text_black"
tools:text="Learning Manager"
android:layout_gravity="center"
android:id="#+id/tvToolbarTitle" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="#+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackgroundBorderless"
android:src="#drawable/ic_icon_arrow_back_black"
tools:ignore="TouchTargetSizeCheck" />
</com.google.android.material.appbar.MaterialToolbar>
themes
<style name="Theme.LearningManager" parent="Theme.MaterialComponents.Light.NoActionBar">
Android Studio 3.4
styles.xml
<style name="Main.Theme.Tango" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here -->
<item name="colorPrimary">#color/colorPrimary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<!-- theme UI controls like buttons, checkboxes and text fields or cursor -->
<item name="colorAccent">#color/colorAccent</item>
</style>
in manifest:
<application
android:name=".main.MainApp"
android:allowBackup="true"
android:icon="#drawable/ic_app"
android:label="#string/application_name"
android:logo="#drawable/ic_app_logo"
android:theme="#style/Main.Theme.Tango" >
In activity:
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
public class MainNavigationDrawerFragmentActivity extends AppCompatActivity{
private void initialize(Bundle savedInstanceState) {
mTitle = mDrawerTitle = getTitle();
mToolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
mToolbar, R.string.application_name,
R.string.application_name) {
public void onDrawerClosed(View view) {
mToolbar.setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
mToolbar.setTitle(mDrawerTitle);
invalidateOptionsMenu();
loadDraftsTotalNumber();
loadActiveCart();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
and here layout:
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="0dp"
android:layout_height="56dp"
android:background="#dc2700"
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="288dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#null"
android:listSelector="#drawable/list_selector"
android:paddingLeft="16dip"
android:paddingRight="16dip"/>
</androidx.drawerlayout.widget.DrawerLayout>
but toolbar not show:
Try this for your layout. It works for me.
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/color3"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="0dp"
android:layout_height="56dp"
android:background="#dc2700"
/>
</com.google.android.material.appbar.AppBarLayout>
Try setting android:layout_width="match_parent" for toolBar
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
I found solution
The toolbar AND FrameLayout must be inside LinearLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="56dp"/>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
and here result:
Problem:
When I open my application and click on the ActionBarDrawerToggle, it doesn't open the navigation drawer. However, when i swipe right and then open and close the navigation drawer, after this the ActionBarDrawerToggle repsonds perfectly fine, like it should, by opening the drawer.
My HomeScreen.java
public class HomeScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle toggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
instantiateViews();
setSupportActionBar(toolbar);
navigationView.setNavigationItemSelectedListener(this);
toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
public void instantiateViews(){
toolbar = findViewById(R.id.drawer_menu_toolbar);
drawerLayout = findViewById(R.id.home_screen_main_drawer_layout);
navigationView = findViewById(R.id.home_screen_navigation_view);
}
#Override
public void onBackPressed(){
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_home:
break;
case R.id.nav_account:
break;
case R.id.nav_share:
break;
case R.id.nav_logout:
session.logoutUser();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
I have tried everything from the following thread: Navigation Drawer ActionBar button not working even though for that person the ActionBarDrawerToggle wasn't working under all circumstances but mine isn't working till the time navigation drawer is opened. After that mine works perfectly
What I have tried doing:
1.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
2.
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
3.
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
None of these work for me
My home_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/home_screen_main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomeScreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Top Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="120dp"
android:background="#color/blue">
<android.support.v7.widget.Toolbar
android:id="#+id/drawer_menu_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<FrameLayout
android:id="#+id/drawer_menu_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.Toolbar
android:id="#+id/search_toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="10dp"
android:background="#drawable/seachbar_homescreen"
<EditText
android:id="#+id/searchHere"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/seachbar_homescreen"
android:hint="Search Here"
android:textSize="15sp" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/home_screen_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_gravity="start"
android:visibility="gone"
app:headerLayout="#layout/home_screen_menu_drawer_header"
app:menu="#menu/home_screen_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
}
Try this, or Try to take out your Toolbar from Relative Layout.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
I have done some minor modification in your layout i hope it will help you.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/home_screen_main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomeScreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Top Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="120dp"
android:background="#color/blue">
<android.support.v7.widget.Toolbar
android:id="#+id/drawer_menu_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<FrameLayout
android:id="#+id/drawer_menu_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.v7.widget.Toolbar
android:id="#+id/search_toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="10dp"
android:background="#drawable/seachbar_homescreen">
<EditText
android:id="#+id/searchHere"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/seachbar_homescreen"
android:hint="Search Here"
android:textSize="15sp"/>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/home_screen_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_gravity="start"
app:headerLayout="#layout/home_screen_menu_drawer_header"
app:menu="#menu/home_screen_menu_drawer"/>
</android.support.v4.widget.DrawerLayout>
Toolbar is not appearing on pre lollipop devices. I'm using drawer layout with toolbar. The same toolbar works on other activities but not when used along with drawer layout.
Activity with drawer layout and toolbar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
{
/*supportChatImageButton = (ImageButton) findViewById(R.id.homeSupportChatImageButton);
orderImageButton = (ImageButton) findViewById(R.id.homeOrderImageButton);*/
headerTextView = (TextView) findViewById(R.id.homeHeaderTextView);
slidingTabLayout = (SlidingTabLayout) findViewById(R.id.homeSlidingTabLayout);
viewPager = (ViewPager) findViewById(R.id.homeViewPager);
headerBackgroundImageView = (ImageView)findViewById(R.id.homeHeaderBackgroundImageView);
mainContentLayout = (FrameLayout) findViewById(R.id.homeScreenMainContentLayout);
hamburgerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
hamburgerLinearLayout = (LinearLayout) findViewById(R.id.hamburgerProfileLayout);
hamburgerListView = (ListView) findViewById(R.id.hamburgerListView);
hamburgerMenu = (RelativeLayout) findViewById(R.id.hamburgerMenu);
GetHamburgerListItems();
HamburgerListAdapter hamburgerListAdapter = new HamburgerListAdapter(this, hamburgerListItems);
hamburgerListView.setAdapter(hamburgerListAdapter);
userProfileImageView = (ImageView) findViewById(R.id.userProfileImageView);
userProfileNameTextView = (TextView) findViewById(R.id.userProfileNameTextView);
if(StorageManager.read(this, "userProfile", new TypeToken<UserProfile>() {}.getType()) != null) {
UserProfile userProfile = (UserProfile) StorageManager.read(this, "userProfile", new TypeToken<UserProfile>() {}.getType());
userProfileNameTextView.setText(userProfile.getFullName());
}
else {
userProfileNameTextView.setText("Login / Sign up");
}
Typeface typeface = Typeface.createFromAsset(this.getAssets(), "fonts/GothamRnd-Medium.otf");
slidingTabLayout.setTypeface(typeface);
slidingTabLayout.setTextColor("#FFFFFF");
}
if(Utilities.IsCitySelected(this)) {
selectedCity = Utilities.GetSelectedCity(this);
}
else {
Intent intent = new Intent(HomeScreenActivity.this, ErrorInternetActivity.class);
intent.putExtra("error", "error");
startActivity(intent);
finish();
}
toolbar = (Toolbar) findViewById(R.id.main_toolbar);
toolbar.setNavigationIcon(R.drawable.ic_action_image_dehaze);
toolbar.inflateMenu(R.menu.home_screen_menu);
toolbar.setBackgroundColor(Color.TRANSPARENT);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, hamburgerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
syncState();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
syncState();
}
};
hamburgerLayout.setDrawerListener(actionBarDrawerToggle);
hamburgerLayout.setDrawerListener(this);
hamburgerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
publ
ic void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
SelectItemFromDrawer(position);
}
});
hamburgerLinearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(Utilities.IsUserLoggedIn(HomeScreenActivity.this)) {
hamburgerLayout.closeDrawer(hamburgerMenu);
isLinkClicked = true;
position = 2;
}
else {
hamburgerLayout.closeDrawer(hamburgerMenu);
isLinkClicked = true;
position = 3;
}
}
}
});
Activity - 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"
tools:context=".HomeScreenActivity">
<FrameLayout
android:id="#+id/homeScreenMainContentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/main_toolbar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/homeHeaderBackgroundImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:gravity="right">
<!--<ImageButton
android:id="#+id/homeSupportChatImageButton"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginRight="20dp"
android:background="#drawable/ic_help"
android:visibility="visible" />
<ImageButton
android:id="#+id/homeOrderImageButton"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginRight="16dp"
android:background="#drawable/ic_menu_orders" />-->
</LinearLayout>
<com.misc.TextView
android:id="#+id/homeHeaderTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="#string/home_screen_header_text"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold"
app:fontName="#string/gotham_bold_reg" />
<com.misc.SlidingTabLayout
android:id="#+id/homeSlidingTabLayout"
fontPath="fonts/GothamRnd-Medium.otf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingTop="10dp"></com.misc.SlidingTabLayout>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:background="#FFFFFF">
<android.support.v4.view.ViewPager
android:id="#+id/homeViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"></android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
</FrameLayout>
<!-- The navigation drawer -->
<RelativeLayout
android:id="#+id/hamburgerMenu"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<!-- Profile Box -->
<LinearLayout
android:id="#+id/hamburgerProfileLayout"
android:layout_width="match_parent"
android:layout_height="144dp"
android:background="#color/lo_orange"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="#+id/userProfileImageView"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginLeft="16dp"
android:scaleType="fitXY"
android:src="#drawable/ic_person_blue" />
<com.misc.TextView
android:id="#+id/userProfileNameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
app:fontName="#string/gotham_medium_reg"
android:textSize="14sp"
android:text=""
android:textColor="#FFFFFF"/>
</LinearLayout>
<!-- List of Actions (pages) -->
<ListView
android:id="#+id/hamburgerListView"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_below="#id/hamburgerProfileLayout"
android:background="#FFFFFF"
android:choiceMode="singleChoice" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
main_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/lo_orange"
android:minHeight="?attr/actionBarSize"
android:fitsSystemWindows="true"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.misc.TextView
android:id="#+id/toolBarTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fontName="#string/gotham_medium_reg"
android:maxLines="2"
android:textSize="16sp"
android:textColor="#FFFFFF"/>
<com.misc.TextView
android:id="#+id/toolBarSubTitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fontName="#string/gotham_light_reg"
android:maxLines="1"
android:textSize="12sp"
android:visibility="gone"
android:textColor="#FFFFFF"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:textColorHintInverse">#color/lo_orange</item>
<!--<item name="colorPrimaryDark">#color/dark_orange</item>-->
<item name="colorPrimary">#color/lo_orange</item>
<item name="colorAccent">#color/lo_orange</item>
<!-- API 14 theme customizations can go here. -->
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
This is a common issue becoming less apparent when testing on Lollipop or later.
When using standard layouts (such as FrameLayout, RelativeLayout etc.) the default behavior is that child views get drawn in order they are added or inflated.
The fix would look similar to this:
<FrameLayout>
<!-- Content will be drawn first - below the toolbar. -->
<FrameLayout android:id="#+id/main_content" />
<!-- Toolbar will be drawn next - above the content. -->
<android.support.v7.widget.Toolbar android:id="#+id/toolbar" />
</FrameLayout>
However since Lollipop the android:elevation attribute can override this behavior. Since elevation defines precise position along the Z axis views with higher elevation values will be drawn above those with lower elevation values.
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>