I try to hide menu items when the CollapsingToolbarLayout is open.
I've do this: XML
<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" android:fitsSystemWindows="true"
tools:context="com.compafone.app.ViewNews">
<android.support.design.widget.AppBarLayout android:id="#+id/app_bar"
android:fitsSystemWindows="true" android:layout_height="#dimen/app_bar_height"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout android:id="#+id/toolbar_layout"
android:fitsSystemWindows="true" android:layout_width="match_parent"
android:scaleType="centerCrop" android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize" android:layout_width="match_parent"
app:layout_collapseMode="pin" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_view_news" />
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin" app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end" android:src="#drawable/chatboxes" />
And this is the XML menu:
<menu 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" tools:context="com.compafone.app.ViewNews">
<item
android:id="#+id/action_comments"
android:title="Comments"
android:orderInCategory="200"
android:icon="#drawable/chatboxes"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_item_share"
android:title="Partager"
android:icon="#android:drawable/ic_menu_share"
android:orderInCategory="100"
app:showAsAction="ifRoom"/>
Then, this is the Java activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_news);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.arrowleft);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//On récupère l'intent
Intent i = getIntent();
id = i.getStringExtra("ID");
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));
collapsingToolbarLayout.setTitle("Compafone");
appbar = (AppBarLayout) findViewById(R.id.app_bar);
new NewsListActivityLoad().execute();
title = (TextView) findViewById(R.id.news_title);
content = (TextView) findViewById(R.id.news_content);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu resource
getMenuInflater().inflate(R.menu.menu_view_news, menu);
MenuItem menuItem = menu.findItem(R.id.menu_item_share);
//mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
this.overridePendingTransition(0, R.anim.abc_fade_out);
return true;
case R.id.action_comments:
Intent intent = getIntent();
//Intent intent3 = new Intent(this, comments.class);
try {
//intent3.putExtra(Intent_ID, intent.getStringExtra(EXTRA_PROJET));
//intent3.putExtra(Intent_SUCCES, "null");
//startActivity(intent3);
} catch (Exception e) {
Log.i("notFoundProject", "Le projet n'existe pas");
}
return true;
case R.id.menu_item_share:
//doShare(shareintent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So, I want to hide items from menu and display items only when users scroll down (When toolbar is in normal size, so display items at the same time when toolbar title is show in the toolbar).
Sorry for bad english, thanks in advance.
Related
I had a menu in my app and it works correctly, now I add a TabLayout and the menu options does not respond to user click now no more.
Here is my activity_main.xml code:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="#layout/toolbar"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPaper"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
My Activity onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
itemAdapter = new ClientAdapter(this, null);
tab = (TabLayout)findViewById(R.id.tab);
paper = (ViewPager)findViewById(R.id.viewPaper);
paperAdapter = new ViewPaperAdapter(getSupportFragmentManager());
HomeFragment homeFragment = new HomeFragment();
DoneFragment doneFragment = new DoneFragment();
paperAdapter.addFrament(homeFragment, "Home");
paperAdapter.addFrament(doneFragment, "Done");
paper.setAdapter(paperAdapter);
tab.setupWithViewPager(paper);
}
onCreateOptionMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
searchView = (SearchView)MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
And onOptionItemSelected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.refresh:
//Check the internet connection
if(Util.isConnected(this)){
// showProgress();
//Check if there is not pending item
sincronize(this);
dbManager.borrar();
new HomeFragment().getTarjetas();
}else{
Toast.makeText(this, "No hay conexion a internet.", Toast.LENGTH_LONG).show();
}
break;
}
return super.onOptionsItemSelected(item);
}
The menu.xml
<menu 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"
tools:context="developer.technologies.agilisa.acardmobile.MainActivity">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="100"
android:title="#string/Search"
app:showAsAction="always|collapseActionView"
android:elevation="8dp"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="#+id/refresh"
android:title="#string/refresh"
android:icon="#drawable/refresh"
android:orderInCategory="200"
app:showAsAction="always|collapseActionView"/>
<item
android:id="#+id/cerrar"
android:orderInCategory="10"
app:showAsAction="never"
android:title="#string/cerrar_session"/>
Can some one help me to solve that?
Try this changes:
Change Your Root layout To LinearLayout or Coordinator Layout-- As framelayout sometimes consume the click event of items under it.
1.in onCreateOptionsMenu()
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Your code
return true;
}
2.And in onOptionsItemSelected() add return true
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.refresh:
// your code
return true; //add this
default:
return super.onOptionsItemSelected(item);
}
}
I'm using the Android Studio template for the Drawer Navigation. I wanted to add a click event to the drawer's header, but when I try to findViewById in the onCreate() method, I get a null exception. I'm guessing it's because it hasn't been inflated yet.
Interesting to me is that findViewById on the drawer_layout and the nav_view returned an object. I tried to do this:
drawer.findViewById;
and this:
navigationView.findViewById;
But that still gives me an exception.
So my question is two fold:
Why is it that findViewById on drawer_layout and nav_view returned an object while findViewById on iv_profile did not?
Where and how do I avoid the NullPointerException in this case?
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ImageView profile = (ImageView) navigationView.findViewById(R.id.iv_profile);
profile.setOnClickListener(new View.OnClickListener() { // Null Pointer Exception
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Profile", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
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"
android:fitsSystemWindows="true"
tools:context="com.example.heng.progressnfitness.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"
android:src="#android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>
nav_header_main.xml
<?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/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
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">
<ImageView
android:id="#+id/iv_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#android:drawable/sym_def_app_icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com"/>
</LinearLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.heng.progressnfitness.MainActivity"
tools:showIn="#layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</RelativeLayout>
All you need to replace is:
ImageView profile = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.iv_profile);
and everything will work fine.
Pay attention to the navigationView.getHeaderView(0) part... Where you get a reference to the headerLayout you set in xml.
I have a collapsing toolbar in MainActivity with a SearchView in toolbar. When the searchview icon is clicked the searchview gets collapsed in the toolbar. It works fine when the toolbar is collapsed. But if i click on the search icon and scroll up, the collapsed searchview will hide. I want to the collapsed searchview to be visible when scrolled up. I am following
this link MaterialSearchView
This is the screen when the collapsing toolbar is collapsed
Now if i click on the search icon the screen looks like this
Till now the searchview is working fine. Now if i scroll up the screen looks like this
Now if i click the search icon, the searchview gets collapsed but it gets hided. So i want the searchview to be collapsed in the toolbar even when the collapsing toolbar is not collapsed.
This is the screen shot which i want when the toolbar is not collapsed and the search icon is clicked
This is my activity_main.xml
<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.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="#android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="150dp"
/>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
app:tabIndicatorColor="#color/pink"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_margin="16dp"
android:src="#drawable/ic_launcher_white"
android:layout_gravity="end|bottom"
app:rippleColor="#android:color/white"
app:backgroundTint="#color/primary"
app:elevation="10dp"
/>
and this is my MainActivity.java
public class MainActivity extends AppCompatActivity {
private MaterialSearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout)
findViewById(R.id.collapsing_toolbar);
AppBarLayout appBarLayout = (AppBarLayout)
findViewById(R.id.appbar);
collapsingToolbar.setTitle(" ");
appBarLayout.setExpanded(true);
// hiding & showing the title when toolbar expanded & collapsed
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
#Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
toolbar.setBackgroundColor(getResources().getColor(R.color.black_tint));
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbar.setTitle(getString(R.string.app_name));
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
isShow = true;
toolbar.setBackgroundColor(getResources().getColor(R.color.primary));
} else if (isShow) {
collapsingToolbar.setTitle(" ");
isShow = false;
toolbar.setBackgroundColor(getResources().getColor(R.color.black_tint));
}
}
});
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
searchView = (MaterialSearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//Do some magic
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//Do some magic
return false;
}
});
searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
#Override
public void onSearchViewShown() {
//Do some magic
}
#Override
public void onSearchViewClosed() {
//Do some magic
}
});
}
}
I solve this with edittext and textchangedlistener
in xml
<RelativeLayout
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">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorTransparent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="#color/colorBlack_50"
android:theme="#style/AppTheme.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true"
android:gravity="bottom"
app:contentScrim="#color/colorBlack_50"
app:expandedTitleMargin="10dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
//some views
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
in activity
editTextSearch.setVisibility(View.VISIBLE);
editTextSearch.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
searchAudio(editTextSearch.getText().toString());
}
#Override
public void beforeTextChanged(CharSequence cs, int s, int b, int c) {}
#Override
public void onTextChanged(CharSequence query, int s, int b, int c) {}
});
I use the setActionBarTitle(); to change the title of the action bar it work fine in onCreate() method but it did not work when i want to implement it on button click listener
public class ScrollingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
toolbar = (Toolbar) findViewById(R.id.toolbar);
CollapsingToolbarLayout mToolbarlayout= (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
setSupportActionBar(toolbar);
Button newbutton= (Button) findViewById(R.id.changetitle);
if (newbutton != null) {
newbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ScrollingActivity.this, "Hi", Toast.LENGTH_SHORT).show();
setActionBarTitle("HI");
}
});
}
}
public void setActionBarTitle(String title) {
//noinspection ConstantConditions
getSupportActionBar().setTitle(title); //check not working
}
}
The xml which is used for the activity in which i want to change the title
<?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"
android:fitsSystemWindows="true"
tools:context="appcom.taskremainder.calender.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_scrolling" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_menu_edit"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout
>
Yes you are right that in onCreate() the Title of ActionBar/ToolBar changes , and if we call the method to change the Title on Button click it doesn't.
The button is defined in the:
<include layout="#layout/content_scrolling"/>
I tried many different ways and stumbled upon this issue https://code.google.com/p/android/issues/detail?id=77763
I have a work around if you would like to try (to change title)
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
android.support.v7.app.ActionBar actionBar;
CollapsingToolbarLayout mToolbarlayout;
Button newButton;
// On every click I will change the value of i
static int i=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbarlayout= (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
newButton= (Button) findViewById(R.id.changetitle);
setSupportActionBar(toolbar);
actionBar=getSupportActionBar();
newButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//oldWay();
newWay();
}
});
}
private void oldWay() {
Toast.makeText(MainActivity.this, ""+actionBar.getTitle(), Toast.LENGTH_SHORT).show();
actionBar.setTitle("Something Else");
Toast.makeText(MainActivity.this/, ""+actionBar.getTitle(), Toast.LENGTH_SHORT).show();
//Toast says that value has changed, but it doesn't on screen
}
private void newWay() {
mToolbarlayout.setTitle("" + i++);
}
}
Let me know if this helps or not.
I am trying to implement MiniDrawer using mikepenz/MaterialDrawer github library. I could get some result as below picture. but the top part of MiniDrawer is hidden by Toolbar. How can i solve this problem.
This is MainActivity.Java
public class MainActivity extends AppCompatActivity {
private Drawer result = null;
private MiniDrawer miniResult = null;
private Crossfader crossFader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
result = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withTranslucentNavigationBar(false)
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.drawer_item_compact_header).withIcon(GoogleMaterial.Icon.gmd_wb_sunny).withIdentifier(1),
new PrimaryDrawerItem().withName(R.string.drawer_item_action_bar_drawer).withIcon(FontAwesome.Icon.faw_home).withBadge("22").withBadgeStyle(new BadgeStyle(Color.RED, Color.RED)).withIdentifier(2).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_multi_drawer).withIcon(FontAwesome.Icon.faw_gamepad).withIdentifier(3),
new PrimaryDrawerItem().withName(R.string.drawer_item_non_translucent_status_drawer).withIcon(FontAwesome.Icon.faw_eye).withIdentifier(4),
new PrimaryDrawerItem().withDescription("A more complex sample").withName(R.string.drawer_item_advanced_drawer).withIcon(GoogleMaterial.Icon.gmd_adb).withIdentifier(5),
new SectionDrawerItem().withName(R.string.drawer_item_section_header),
new SecondaryDrawerItem().withName(R.string.drawer_item_open_source).withIcon(FontAwesome.Icon.faw_github),
new SecondaryDrawerItem().withName(R.string.drawer_item_contact).withIcon(GoogleMaterial.Icon.gmd_format_color_fill).withTag("Bullhorn"),
new DividerDrawerItem(),
new SwitchDrawerItem().withName("Switch").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener),
new ToggleDrawerItem().withName("Toggle").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener)
) // add the items we want to use with our Drawer
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if (drawerItem instanceof Nameable) {
Toast.makeText(MainActivity.this, ((Nameable) drawerItem).getName().getText(MainActivity.this), Toast.LENGTH_SHORT).show();
}
return false;
}
})
.withGenerateMiniDrawer(true)
.withSavedInstance(savedInstanceState)
.buildView();
miniResult = result.getMiniDrawer();
int firstWidth = (int) UIUtils.convertDpToPixel(300, this);
int secondWidth = (int) UIUtils.convertDpToPixel(72, this);
crossFader = new Crossfader()
.withContent(findViewById(R.id.main_content))
.withFirst(result.getSlider(), firstWidth)
.withSecond(miniResult.build(this), secondWidth)
.withSavedInstance(savedInstanceState)
.build();
miniResult.withCrossFader(new CrossfadeWrapper(crossFader));
//define a shadow (this is only for normal LTR layouts if you have a RTL app you need to define the other one
crossFader.getCrossFadeSlidingPaneLayout().setShadowResourceLeft(R.drawable.material_drawer_shadow_left);
}
private OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(IDrawerItem drawerItem, CompoundButton buttonView, boolean isChecked) {
if (drawerItem instanceof Nameable) {
Log.i("material-drawer", "DrawerItem: " + ((Nameable) drawerItem).getName() + " - toggleChecked: " + isChecked);
} else {
Log.i("material-drawer", "toggleChecked: " + isChecked);
}
}
};
#Override
protected void onSaveInstanceState(Bundle outState) {
//add the values which need to be saved from the drawer to the bundle
outState = result.saveInstanceState(outState);
//add the values which need to be saved from the crossFader to the bundle
outState = crossFader.saveInstanceState(outState);
super.onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
//handle the back press :D close the drawer first and if the drawer is closed close the activity
if (crossFader != null && crossFader.isCrossFaded()) {
crossFader.crossFade();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
menu.findItem(R.id.menu_1)
.setIcon(new IconicsDrawable(this, FontAwesome.Icon.faw_repeat)
.color(Color.WHITE).actionBar());
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_1:
crossFader.crossFade();
return true;
case R.id.act_settings:
return true;
default:
}
return super.onOptionsItemSelected(item);
}
}
This is activity_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"
android:fitsSystemWindows="true"
tools:context="com.sldroids.minidrawer_v3.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"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
#sendtodilanka the problem is that you add the Crosssfader as the parent of the main_content view, which is a direct child of your CoordinatorLayout.
As the CoordinatorLayout is similar to the FrameLayout and does overlap views your Crossfader will simply be displayed below the AppBarLayout which you have defined to be over the main_content.
To get the Crossfader below the Toolbar you should add a new View around your main_content, let's choose a FrameLayout which has the attribute (don't forget to define the appBarLayout id to your AppBarLayout)
xml
app:layout_behavior="#string/appbar_scrolling_view_behavior"
After this the Crossfader will get inflated as a child of the FrameLayout we added above, which is displayed below the AppBarLayout.
A more detailed information regarding this can be found here.
So your layout will look something like this:
<?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"
android:fitsSystemWindows="true"
tools:context="com.sldroids.minidrawer_v3.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>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<include layout="#layout/content_main" />
</FrameLayout>
<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>