Not show androidx.appcompat.widget.Toolbar - android

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:

Related

No MaterialCardView background in darkview

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"

How to start Drawer Layout below the toolbar in Android?

Here is my main xml file:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="#+id/nav_drawer">
<android.support.design.widget.CoordinatorLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.amit.rssreader.MainActivity">
<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>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
This is what is happening
I want to have the drawer below the Action bar .This wasnt a navigation Drawer Activity, the navigation drawer has been added later by xml.
I think this might help you:
Modify the xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Create theme for activity without toolbar in values/styles.xml and apply it to your activity in AndroidManifest file:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And to make it working, make the onCreate method of your activity to look like this:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
final DrawerLayout drawerLayout = (DrawerLayout)
findViewById(R.id.drawerLayout);
ActionBar supportActionBar = getSupportActionBar();
if(supportActionBar != null) {
supportActionBar.setDisplayHomeAsUpEnabled(true);
supportActionBar.setHomeButtonEnabled(true);
}
setSupportActionBar(toolbar);
final ListView drawerList = (ListView)
findViewById(R.id.drawerList);
drawerList.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
new String[] { "Item 1", "Item 2", "Item 3" } ));
drawerLayout.setScrimColor(Color.TRANSPARENT);
ActionBarDrawerToggle actionBarDrawerToggle =
new ActionBarDrawerToggle(this, drawerLayout,
toolbar, R.string.app_name, R.string.app_name);
actionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(drawerLayout.isDrawerOpen(drawerList)) {
drawerLayout.closeDrawers();
} else {
drawerLayout.openDrawer(drawerList);
}
}
});
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
And this is how does it work.

toolbar not appearing in pre lollipop devices

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.

Navigation Drawer not working my Code in Android?

I am working with Navigation Drawer in Android.I have code from some examples.In my code working to change the drawer open and drawer close title but the panel is not open.I not insert the ListView array. My main Problem is not open the panel.Is anything I missed in the code?
layout_mynavigation.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_height="150dp"
android:layout_width="match_parent"
android:id="#+id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
MyNavigation.java
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
public class MyDrawer extends AppCompatActivity {
private ActionBarDrawerToggle mActionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_mydrawer);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
DrawerLayout mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.drawer_open,R.string.drawer_closed) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle( R.string.drawer_open );
invalidateOptionsMenu();
}
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle( R.string.drawer_closed );
invalidateOptionsMenu();
}
}
};
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged( newConfig );
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return mActionBarDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Customize your theme here. -->
</style>
DrawerLayout expects it's first child to be the actual layout and the second one to be the drawer. So, to make it work you should change your layout to (assuming that your entire drawer consists of only one ListView):
<android.support.v4.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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_height="150dp"
android:layout_width="match_parent"
android:id="#+id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize"/>
</LinearLayout>
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="#000000"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
It looks like your xml layout needs to be re-arranged. The DrawerLayout should wrap two items; 1) a View or ViewGroup to set as the page content (e.g., RelativeLayout containing a Toolbar and Fragment container); 2) a View or ViewGroup that holds your NavigationDrawer items (e.g., ListView).
<android.support.v4.widget.DrawerLayout
android:id="#+id/navigation_drawer_layout"
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/your_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/your_toolbar_color"
android:minHeight="?attr/actionBarSize"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/your_toolbar"/>
</RelativeLayout>
<ListView
android:id="#+id/your_drawer_list"
android:layout_width="#dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#color/your_drawer_color"
android:divider="#null"/>
</android.support.v4.widget.DrawerLayout>
You can get a more detailed explanation and examples here:
https://developer.android.com/training/implementing-navigation/nav-drawer.html
If your list is empty and you have the height as wrap_content, the height will be 0. Changing it to match_parent will have it fill the screen regardless of the content.
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="#000000"
android:layout_gravity="start"/>

Why does Toolbar in AppCompat not show when using as an ActionBar?

Google mentioned in their Android blog that there are two possibilities if one wants to use the new Toolbar using AppCompat. One is by using it as an ActionBar and one using it standalone, but as I am using ActionBarDrawerToggle, I need the first option. So I have to extend ActionBarActivity and my theme must inherit Theme.Appcompat (or one of it's variations). Also, to use the first option I had to inherit the NoActionBar variation. And finally, I had to call setSupportActionBar(mToolbar), which I did. However, the ActionBar still does not show. Below is my code.
I have already seen this post, but I guess in my case it's SwipeRefreshLayout that ruins it. I tried different places to include the toolbar, but none of them works. How can I include the toolbar without ruining the rest of the layout? Thanks.
MainActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orders_list);
context = getApplicationContext();
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.refreshable_content);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorSchemeResources(R.color.refresh_one,
R.color.refresh_four,
R.color.sun_flower,
R.color.carrot);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
getSupportActionBar().setTitle(R.string.title_activity_orders_list);
drawerToggleLeft = new ActionBarDrawerToggle(this, drawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close)
{
#Override
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
getSupportActionBar().setTitle(R.string.title_activity_orders_list);
findViewById(R.id.action_add_order).setVisibility(View.VISIBLE);
invalidateOptionsMenu();
syncState();
}
#Override
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("");
findViewById(R.id.action_add_order).setVisibility(View.INVISIBLE);
invalidateOptionsMenu();
syncState();
}
};
drawerLayout.setDrawerListener(drawerToggleLeft);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerToggleLeft.syncState();
ordersList = (ListView) findViewById(android.R.id.list);
}
theme.xml
<?xml version="1.0" encoding="utf-8"?>
<resources >
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/turquoise</item>
<item name="colorPrimaryDark">#color/green_sea</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#color/white</item>
</style>
</resources>
custom_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/turquoise" />
activity_order_list.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<include layout="#layout/custom_toolbar" /> // here is the toolbar
<android.support.v4.widget.SwipeRefreshLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OrdersListActivity"
android:background="#color/white"
android:id="#+id/refreshable_content">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#null">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
<LinearLayout
android:id="#+id/drawer_left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
android:orientation="vertical">
<include layout="#layout/custom_toolbar" /> // here is the toolbar
<fragment
android:id="#+id/drawer_fragment_left"
android:name="com.ordr.view.DrawerFragmentLeft"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>

Categories

Resources