toolbar not appearing in pre lollipop devices - android

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.

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"

Not show androidx.appcompat.widget.Toolbar

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:

Android toolbar change layout

I got an activity with many fragments. When i change fragments i need to change also and my toolbar declared in MainActivity.class. My problem is that i include a layout and didnt find a method to change this layout. How to change toolbars layout in this case?
MainActivity.class
#ContentView(R.layout.activity_main)
public class MainActivity extends RoboActionBarActivity {
#InjectView(R.id.tool_bar)
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setToolBar();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new SplashFragment())
.commit();
} else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new HomeFragment())
.commit();
}
}
private void setToolBar() {
setSupportActionBar(toolbar);
}
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
String currentFragmentClass = fragment.getClass().getSimpleName();
if (currentFragmentClass.equals(getString(R.string.info_fragment))) {
//here i need to change and set onclicks
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar" />
<View
android:id="#+id/shadow_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/tool_bar"
android:background="#ad8c22" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/shadow_view"
tools:context=".activities.MainActivity"
tools:ignore="MergeRootFrame" />
</RelativeLayout>
tool_bar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorToolBar"
>
<RelativeLayout
android:id="#+id/toolbar_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="toggleSlidingMenu"
android:src="#mipmap/icon_sliding_menu" />
<ImageView
android:id="#+id/app_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#mipmap/jammboree" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/app_logo"
android:padding="5dp">
<ImageView
android:id="#+id/hotlist_bell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="bell"
android:gravity="center"
android:padding="7dp"
android:src="#mipmap/icon_my_cart" />
<TextView
android:id="#+id/hotlist_hot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#id/hotlist_bell"
android:layout_alignTop="#id/hotlist_bell"
android:background="#drawable/rounded_square"
android:gravity="center"
android:minWidth="17sp"
android:padding="2dp"
android:text="2"
android:textColor="#ffffffff"
android:textSize="12sp" />
<TextView
android:id="#+id/myMoneyInMyPocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/hotlist_bell"
android:text="2000$"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
If I understood right, you want to change the toolbar whenever you change the Fragment the user is currently seeing, isn´t that right?
There is a very simple way to achieve this trough your XML. Let's say you want three different styled toolbars.
1) First, you create XML files for every toolbar you need.
2) Then you include every toolbar in to your activity_main.xml, like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity">
<include
android:id="#+id/toolbar1"
layout="#layout/tool_bar1"
android:visibility="visible" />
<include
android:id="#+id/toolbar2"
layout="#layout/tool_bar2"
android:visibility="gone" />
<include
android:id="#+id/toolbar3"
layout="#layout/tool_bar3"
android:visibility="gone" />
// Here is the rest of your XML code
</RelativeLayout>
See how all three toolbar includes have the visibility property?
Well, now you can toy with this property, and show/hide the desired toolbar whenever you want/need to.
For example:
RelativeLayout mLayout = (RelativeLayout) getActivity().findViewById(R.id.my_main_layout);
Toolbar mToolbar1 = (Toolbar) mLayout.findViewById(R.id.toolbar1);
mToolbar1.setVisibility(View.GONE);
Toolbar mToolbar2 = (Toolbar) mLayout.findViewById(R.id.toolbar2);
mToolbar2.setVisibility(View.GONE);
Toolbar mToolbar3 = (Toolbar) mLayout.findViewById(R.id.toolbar3);
mToolbar3.setVisibility(View.VISIBLE);
And just change which one are you making visible, according to your needs.
Hope this helps.
I don`t think the method of #herrmartell is nice;
In my way,I have three Fragment in a Activity,I defined some different layout resources for a common toolbar xml,and load them in toolbar when I click different button; my code :
common_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/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<!-- your custom layout -->
</android.support.v7.widget.Toolbar>
activity_main_toolbar_common_view.xml
<TextView
android:id="#+id/action_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="迪士尼...ˇ"
android:textColor="#color/white"
android:textSize="#dimen/larget_text_size"
android:paddingLeft="10dp"
android:layout_alignParentStart="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
activity_main_toolbar_mine_view.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mine"
android:textSize="#dimen/big_text_size"
android:textColor="#color/white"
android:gravity="center"
android:layout_centerInParent="true"/>
<ImageView
android:id="#+id/setting"
android:layout_width="#dimen/toolbar_icon_size"
android:layout_height="#dimen/toolbar_icon_size"
android:src="#drawable/toolbar_setting"
android:layout_alignParentRight="true"
/>
switchToolbar() method in Activity:
public void switchToolbar(int layout) {
if(currentToolbarLayout == layout){
return;
}
currentToolbarLayout = layout;
View v = getLayoutInflater().inflate(layout,null);
toolbar.removeAllViews();
toolbar.addView(v);
switch (layout){
case R.layout.activity_main_toolbar_common_view:
v.findViewById(R.id.action_location).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到位置", Toast.LENGTH_SHORT).show();
}
});
break;
case R.layout.activity_main_toolbar_mine_view:
v.findViewById(R.id.setting).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到设置", Toast.LENGTH_SHORT).show();
}
});
break;
}
}
I render my toolbar view in the onCreate() method of Activity:
ButterKnife.bind(this);
setSupportActionBar(toolbar);
switchToolbar(R.layout.activity_main_toolbar_common_view);
when I switch toolbar, I call:
switchToolbar(R.layout.activity_main_toolbar_mine_view);

Use Custom Layout in NavigationDrawer With Header And list

How to do add custom layout in NavigationView and design my create custom NavigationView use material design,i want put my drawer icon to right and text left of it something like this
I Search too much and this is my experience that works fine
at first create layout for header. its name is nav_header_main.xml then put it in layouts folders in res and put this code in it..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="#dimen/nav_header_height"
android:background="#drawable/header"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:gravity="top">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/cv_nave_profile_image"
android:layout_width="#dimen/nav_profile_image"
android:layout_height="#dimen/nav_profile_image"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/profile"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/cv_nave_profile_image"
android:layout_alignParentTop="true"
android:padding="#dimen/activity_horizontal_margin"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_nav_name"
android:textStyle="bold"
android:typeface="sans"
android:textColor="#ffffff"
android:gravity="right"
android:layout_gravity="right"
android:text="رخداد جدید"
android:paddingBottom="5dp"
android:textSize="#dimen/body"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:typeface="sans"
android:textColor="#ffffff"
android:id="#+id/tv_nav_phone"
android:layout_alignParentLeft="true"
android:text="0370077315"
/>
</RelativeLayout>
</LinearLayout>
then i include it as child of NavigationView and For menu item i use RecyclerView to show menu and icon so my NavigationView is
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="spydroid.ir.dorobar.Activities.SearchActivity">
<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.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_search" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView android:id="#+id/nav_view"
android:layout_width="fill_parent" android:layout_height="match_parent"
android:layout_gravity="right" android:fitsSystemWindows="true"
android:layout_marginLeft="#dimen/nav_margin"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<include layout="#layout/nav_header_main" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/drawer_slidermenu"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"/>
</RelativeLayout>
</LinearLayout>
</android.support.design.widget.NavigationView>
just you have to remember put your NavigationView in DrawerLayout
then i create layout for menu item with ImageView and TextView this layout and this name is card_drawer_item.xml and its code is here
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">
<ImageView
android:id="#+id/drawer_icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="#drawable/ic_about"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/drawer_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/drawer_icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:typeface="sans"
android:paddingRight="40dp"/>
</RelativeLayout>
then i create ViewHolder folder for this layout.
public class DrawerItemHolder extends RecyclerView.ViewHolder {
public ImageView itemIcon;
public TextView itemText;
public DrawerItemHolder(View itemView) {
super(itemView);
itemIcon= (ImageView) itemView.findViewById(R.id.drawer_icon);
itemText= (TextView) itemView.findViewById(R.id.drawer_text);
}
}
now i define text of menu items as string array and array that contains menu icons in menu in strings.xml
<string-array name="drawer_items">
<item>setting</item>
<item>add record</item>
<item>ads</item>
<item>about</item>
<item>call</item>
<item>help</item>
<item>privacy</item>
</string-array>
<array name="drawers_icons">
<item>#drawable/ic_action_settings</item>
<item>#drawable/ic_plus</item>
<item>#drawable/ic_ads</item>
<item>#drawable/ic_about</item>
<item>#drawable/ic_phone</item>
<item>#drawable/ic_help</item>
<item>#drawable/ic_policy</item>
</array>
then we just need an Adapter like this
public class DrawerItemAdapter extends RecyclerView.Adapter<DrawerItemHolder> {
// slide menu items
private List<DrawerItem> items;
private List<Integer> drawerIcons;
public DrawerItemAdapter(List<DrawerItem> items) {
super();
this.items = items;
}
#Override
public DrawerItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.card_drawer_item, parent, false);
return new DrawerItemHolder(itemView);
}
#Override
public void onBindViewHolder(DrawerItemHolder holder, int position) {
holder.itemIcon.setImageResource(items.get(position).getIconId());
holder.itemText.setText(items.get(position).getText());
}
#Override
public int getItemCount() {
return items.size();
}
}
every thing is ok .. just now we have to set NavigationView in Activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
recList = (RecyclerView) findViewById(R.id.drawer_slidermenu);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
String []itemsTitle=getResources().getStringArray(R.array.drawer_items);
TypedArray icons=getResources().obtainTypedArray(R.array.drawers_icons);
List<DrawerItem>drawerItems= new ArrayList<DrawerItem>();
for(int i=0;i<itemsTitle.length;i++){
drawerItems.add(new DrawerItem(icons.getResourceId(i,-1),itemsTitle[i]));
}
DrawerItemAdapter ad= new DrawerItemAdapter(drawerItems);
recList.setAdapter(ad);
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
return;
}
super.onBackPressed();
}

ActionBar Flickering while hiding/showing in RecyclerView

So in my case I am using a RecyclerView to display a list of items but when I try to hide the action bar while scrolling I get this annoying flickering of the screen. I have searched the web for an answer to my problem but so far there has been no luck. So can anyone tell me what I am doing wrong?
here is my java for hiding the action bar:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.container = container;
this.inflater = inflater;
timePassed = System.currentTimeMillis();
if(hasMoreResults) {
rootView = inflater.inflate(R.layout.home_zone, container, false);
rootView.findViewById(R.id.imgAdsMap).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(context, AdsMap.class));
}
});
recyclerViewZone = (RecyclerView) rootView.findViewById(R.id.recyclerViewZone);
recyclerViewZone.setVerticalScrollBarEnabled(true);
// 2. set layoutManger
manager = new LinearLayoutManager(context);
recyclerViewZone.setLayoutManager(manager);
recyclerViewZone.setItemAnimator(new DefaultItemAnimator());
recyclerViewZone.setOnScrollListener(new RecyclerView.OnScrollListener()
{
int mLastFirstVisibleItem = 0;
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE && !generatedAdsFromScroll)
{
generatedAdsFromScroll = true;
runSplitRequest(10);
}
}
//Hiding the Action Bar for better view of the listed items
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
final int currentFirstVisibleItem = manager.findFirstVisibleItemPosition();
if (currentFirstVisibleItem > this.mLastFirstVisibleItem)
{
ActionBar bar = getActivity().getActionBar();
bar.hide();
myButton = (RadioGroup)getActivity().findViewById(R.id.radioButtonGroup);
myButton.setVisibility(View.GONE);
}
else if (currentFirstVisibleItem < this.mLastFirstVisibleItem)
{
ActionBar bar = getActivity().getActionBar();
bar.show();
myButton = (RadioGroup)getActivity().findViewById(R.id.radioButtonGroup);
myButton.setVisibility(View.VISIBLE);
}
this.mLastFirstVisibleItem = currentFirstVisibleItem;
}
});
runSplitRequest(20);
}
else
rootView = inflater.inflate(R.layout.error_internet_connection, container, false);
return rootView;
}
This is the XML of the recyclerview:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/recyclerViewZone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"
tools:context=".MainActivity"
android:scrollbars="vertical" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgAdsMap"
android:src="#drawable/map_button_map_down"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
and the style of the action bar:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:windowActionBarOverlay">true</item>
</style>
<dimen name="image_detail_height">200dp</dimen>
So as you can see I have enabled the action bar to overlay. I have also tried with calling the overlay of the action bar in the activity I call the recyclerview class but the problem remains... no luck the flickering is still there and even if I add padding to the recyclervie XML i just get a black rectangle with the size of the action bar.
Here is the xml of the activity I display the recyclerview:
<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:background="#android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#353535"/>
<RadioGroup
android:id="#+id/radioButtonGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
z
<RadioButton
android:button="#null"
android:checked="true"
android:contentDescription="ZONA"
android:background="#drawable/tab_back_selector"
android:textColor="#color/tab_text_color"
android:textSize="12sp"
android:layout_weight="1"
android:id="#+id/tabZoneHome"
android:padding="14dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/home_tab_zone"
android:gravity="center"/>
<RadioButton
android:button="#null"
android:contentDescription="VISITADAS"
android:background="#drawable/tab_back_selector"
android:textColor="#color/tab_text_color"
android:textSize="12sp"
android:layout_weight="1"
android:id="#+id/tabVisitedHome"
android:padding="14dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/home_tab_visited"
android:gravity="center"
android:checked="false" />
<RadioButton
android:button="#null"
android:contentDescription="SUGERENCIAS"
android:background="#drawable/tab_back_selector"
android:textColor="#color/tab_text_color"
android:textSize="12sp"
android:layout_weight="1"
android:id="#+id/tabSuggestionsHome"
android:padding="14dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/home_tab_suggestions"
android:gravity="center"
android:checked="false" />
</RadioGroup>
<RelativeLayout
android:id="#+id/ad_detailholder"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="#drawable/quickview_top_gradient"/>
</RelativeLayout>
<!-- Framelayout to display Fragments -->
</LinearLayout>
<!-- Listview to display slider menu -->

Categories

Resources