Recyclerview overlay toolbar - android

I use new com.android.support:design:22.2.0 library. When recyclerview initialised it overlay toolbar. I think it's because of incorrect initialization recyclerview. I else try insert recyclerview in single file, but i cannot understand how to inflate it
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setHomeAsUpIndicator(R.drawable.menu_and);
ab.setDisplayHomeAsUpEnabled(true);
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
lNews.clear();
populatedata();
mLinearLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView = (RecyclerView)findViewById(R.id.recycleralda);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerviewAdapter = new RecyclerViewAdapter(getApplicationContext());
mRecyclerviewAdapter.loadNews(lNews);
mRecyclerView.setAdapter(mRecyclerviewAdapter);
home.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_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<include layout="#layout/app_bar"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleralda"
android:layout_below="#+id/app_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
xmlns:android="http://schemas.android.com/apk/res/android">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_view"/>
</android.support.v4.widget.DrawerLayout>
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"
/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
pls upload img
http://i.stack.imgur.com/LxcJF.jpg

Add RecyclerView in app_bar.xml file
before following line
</android.support.design.widget.CoordinatorLayout>
and after following line
</android.support.design.widget.AppBarLayout>

Related

how to have a custom drawer layout with recycler view?

i want to create a custom drawer layout and have a recycler view inside it, how can i do this so all recycler view rows show inside the drawer layout and make it scrollable, i.e i want to show the whole FoodListFragment in the drawer.
my drawer layout
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
my recycler view which is inside a fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
tools:context=".FoodListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
You can achieve this by adding your fragment inside DrawerLayout. Check below:
First, Create a container for your fragment inside DrawerLayout
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:layout_gravity="start"
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<FrameLayout
android:id="#+id/drawer_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
Then, Attach your fragment in this container using FragmentTransaction
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new FoodListFragment());
fragmentTransaction.commit();
add following code in your drawer layout
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
android:id="#+id/nv">
</android.support.design.widget.NavigationView>
in your MainActivity
private NavigationView nv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nv = (NavigationView)findViewById(R.id.nv);
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch(id)
{
case R.id.account:
Toast.makeText(MainActivity.this, "My Account",Toast.LENGTH_SHORT).show();break;
case R.id.settings:
Toast.makeText(MainActivity.this, "Settings",Toast.LENGTH_SHORT).show();break;
case R.id.mycart:
Toast.makeText(MainActivity.this, "My Cart",Toast.LENGTH_SHORT).show();break;
default:
return true;
}
return true;
}
});

android statusbar not showing on fragment

I am working on a to-do list app on android using a fragment on top of the main activity to add new items to the list. When the fragment is displayed on the screen it is shown below the status bar. I would like to be able to see the status bar and the fullscreen fragment. Here is the code:
Main Activity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static Context mContext;
private static ArrayList<Task> mTaskList = new ArrayList<>();
private static RecyclerView mRecyclerView;
private static TaskListAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this.getApplicationContext();
//TOOLBAR
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//FAB - ADD NEW TASK
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewTask();
}
});
//RECYCLER VIEW
setTaskData();
mRecyclerView = (RecyclerView) findViewById(R.id.rvTaskList);
mAdapter = new TaskListAdapter(getTaskData(), mContext);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void addNewTask() {
FragmentManager fragmentManager = getSupportFragmentManager();
AddTaskDialogFragment newFragment = new AddTaskDialogFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN );
transaction.add(android.R.id.content, newFragment).addToBackStack(null).commit();
}
activity_main.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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ezloop.sima.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.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"
app:srcCompat="#android:drawable/ic_input_add"
android:tint="#color/colorIcons"/>
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.ezloop.sima.MainActivity"
tools:showIn="#layout/app_bar_main">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvTaskList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
fragment_add_task_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:clickable="true"
android:focusable="true"
android:background="#color/colorIcons">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="false"
android:theme="#style/AppTheme">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<EditText
android:id="#+id/etNewTaskName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_task_name" />
<EditText
android:id="#+id/etNewTaskDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_task_desc"
android:inputType="textMultiLine" />
</LinearLayout>
Set id of content_main.xml
Like this
android:id="#+id/content"
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_main">
Then define it in onCreate of MainactivityClass
Like this
content = (ConstraintLayout)findViewById(R.id.content);
Then Remove
android.R.id.content, newFragment
Use it like this in addNewTask.
R.id.content, newFragment
private void addNewTask() {
FragmentManager fragmentManager = getSupportFragmentManager();
AddTaskDialogFragment newFragment = new AddTaskDialogFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN );
transaction.add(R.id.content, newFragment).addToBackStack(null).commit();
}
you can see status bar.

Toolbar in non-AppCompat project not inflating

I tried to implement my toolbar with my activity but it's not inflating and keeps showing an error. My project does not use AppCompat but I'm not sure whether or not that is also the vause of the error.
Error inflating class android.widget.Toolbar
Java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar customToolbar = findViewById(R.id.toolbar_1line);
setActionBar(customToolbar);
//add back arrow to toolbar
if (getActionBar() != null){
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(true);
}
TextView mTitle = this.findViewById(R.id.toolbar_title);
mTitle.setText(getString(R.string.select_a_destination_station));
mTitle.setTextColor(Color.WHITE);
mTitle.setEllipsize(TextUtils.TruncateAt.MARQUEE);
mTitle.setMarqueeRepeatLimit(-1);
mTitle.setSingleLine(true);
mTitle.setSelected(true);
}
}
toolbar layout
<?xml version="1.0" encoding="utf-8"?>
<android.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_1line"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize">
<LinearLayout
android:id="#+id/singleline_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Material.Widget.ActionBar.Title"/>
</LinearLayout>
</android.widget.Toolbar>
activity_main.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:id="#+id/detail_container">
<include layout="#layout/toolbar_singleline"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/list_objects"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Try this.
In the Toolbar_layout
One way
change
<android.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_1line"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize">
</android.widget.Toolbar>
to
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_1line"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize">
</android.support.v7.widget.Toolbar>
In the java code
change
Toolbar customToolbar = findViewById(R.id.toolbar_1line);
setActionBar(customToolbar);
//add back arrow to toolbar
if (getActionBar() != null){
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(true);
}
to
Toolbar customToolbar = (Toolbar) findViewById(R.id.toolbar_1line);
setSupportActionBar(customToolbar);
//add back arrow to toolbar
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
Another way
change
<android.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_1line"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize">
</android.widget.Toolbar>
to
<Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_1line"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize">
</Toolbar>
It's easy.

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.

CollapsingToolbarLayout not showing Title

In my case, the toolbar disappears when I scroll through the list. I am using a CollapsingToolbarLayout and I need to set the title text. But in my case the title text is not showing, even though I've set it (See code below). What is wrong?
Layout code:
<?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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|enterAlways">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="0dp"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/app_nav_header_main"
app:menu="#menu/main_drawer" />
</android.support.v4.widget.DrawerLayout>
Activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_activity_with_left_panel);
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mCollapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
setSupportActionBar(mToolbar);
setTitle(getIntent().getStringExtra(TITLE));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public void setTitle(CharSequence title) {
if (title != null && !title.toString().isEmpty()) {
mTitle = title.toString();
mCollapsingToolbarLayout.setTitle(mTitle);
}
}
Remove this
#Override
public void setTitle(CharSequence title) {
if (title != null && !title.toString().isEmpty()) {
mTitle = title.toString();
mCollapsingToolbarLayout.setTitle(mTitle);
}
}
and add this on your OnCreate().
mCollapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
mCollapsingToolbarLayout.setTitleEnabled(false);
mToolbar.setTitle("title");
This disables the default title with collapsing behaviour and adds the static title to the toolbar.
For who searches for disabling the title just add
app:titleEnabled="false"
Then the title of the toolbar itself would appear so we will disable it with
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
or with those line in the style xml
<item name="android:displayOptions">showHome|useLogo</item>
<item name="displayOptions">showHome|useLogo</item>
Searched too much to make this search summary, wish it helps.
The above answers are correct, but it took me multiple tries to get it right. I ended up setting app:titleEnabled="false" for my CollapsingToolbarLayout in the xml, setting nothing for the Toolbar itself.
In the code, I set supportActionBar.setDisplayShowTitleEnabled(true), and supportActionBar.title = "my title".
My code:
layout.xml:
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/fragment_main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:toolbarId="#id/toolbar"
app:titleEnabled="false">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:menu="#menu/menu_main" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
fragment.kt:
val toolbar = view.findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbar)
myTitle = "This is my app title"
(activity as AppCompatActivity).let { myActivity ->
myActivity.setSupportActionBar(toolbar)
myActivity.supportActionBar?.setDisplayShowTitleEnabled(true)
myActivity.supportActionBar?.title = myTitle
// these are not needed:
//collapsingToolbar.title = myTitle
//collapsingToolbar.isTitleEnabled = true
//toolbar.title = myTitle
//myActivity.title = myTitle
}
In my case the problem was due to setting Toolbar layout_height to "wrap_content". After setting this property to a specific value like ?attr/actionBarSize, CollapsingToolbarLayout title become visible.
use this :
app:expandedTitleTextAppearance="#android:color/transparent" with your CollapsingToolbarLayout

Categories

Resources