I\'ve been searching for a while as shown on the following images but unfortunately I was not able to find anything similar
edit menu item is moving to
In XML layout:-
Menu item XML:-
In activity:-
public class ScrollingActivity extends AppCompatActivity {
private AppBarLayout app_bar;
private boolean appBarExpanded;
private Menu itemMenu;
private CollapsingToolbarLayout toolbar_layout;
private ImageView img_edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = findViewById(R.id.toolbar);
app_bar = findViewById(R.id.app_bar);
toolbar_layout = findViewById(R.id.toolbar_layout);
img_edit = findViewById(R.id.img_edit);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
app_bar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
// Vertical offset == 0 indicates appBar is fully expanded.
if (Math.abs(verticalOffset) > 200) {
appBarExpanded = false;
img_edit.setVisibility(View.GONE);
invalidateOptionsMenu();
} else {
img_edit.setVisibility(View.VISIBLE);
appBarExpanded = true;
invalidateOptionsMenu();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_scrolling,menu);
itemMenu = menu;
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (itemMenu != null
&& (!appBarExpanded || itemMenu.size() != 1)) {
//collapsed
itemMenu.add("Edit")
.setIcon(android.R.drawable.ic_menu_edit)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
} else {
//expanded
}
return super.onPrepareOptionsMenu(itemMenu);
}
}
I have not added subtitle with the toolbar. If you need subtitle you have to use some custom view class or library.