I have a collapsing toolbar. When it is collapsed, i set a app bar title, but once its expanded, i'm not able to remove the app bar title. The expand condition (verticalOffset == 0) is getting executed, but the title seem not getting changed.
public class MovieDetailsActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener{
private CollapsingToolbarLayout collapsingToolbarLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
appBarLayout.addOnOffsetChangedListener(this);
...
setTitle("");
...
}
#Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
//Closed
setTitle("Tmovies");
Log.i("test","Closed");
} else if (verticalOffset == 0) {
// Expanded
setTitle("");
Log.i("test","Expanded");
} else {
// Somewhere in between
}
}
}
use toolbar.setTitle(...) instead of setTitle(...)
You should prefer getSupportActionBar().setTitle("My Title"); instead of tool.setTitle("My Title"); as the latter might not work after setSupportActionBar .
Related
i am creating a CAROUSEL view, that can be swipe left or right, but the problem that i am having is, i have 5 images and 1 XML view to be need to put in the carousel function, the 5 images is already working but for the last bullet in the carousel a view should be loaded on it, and it can still be swipe if they want to go back to the 5th images or to the first image.
here is the code:
public class AboutHelp extends AppCompatActivity {
CarouselView carouselView;
int[] sampleImages = {R.drawable.help_1, R.drawable.help_2, R.drawable.help_3, R.drawable.help_4, R.drawable.help_5,R.layout.activity_send_feed_back};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_help);
androidx.appcompat.widget.Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// add back arrow to toolbar
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
carouselView = findViewById(R.id.aboutCarouselHelp);
carouselView.setPageCount(sampleImages.length);
carouselView.setImageListener(imageListener);
}
ImageListener imageListener = new ImageListener() {
#Override
public void setImageForPosition(int position, ImageView imageView) {
imageView.setImageResource(sampleImages[position]);
}
};
is this possible? can i load an XML to the last bullet? any help would be really appreciated.
You should use ViewListener instead of ImageListener. Try this
carouselView.setViewListener(viewListener);
ViewListener viewListener = new ViewListener() {
#Override
public View setViewForPosition(int position) {
if (position < 5) {
ImageView imageView = new ImageView(AboutHelp.this);
imageView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)); //setting image position
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(sampleImages[position]);
return imageView;
} else {
View customView = getLayoutInflater().inflate(sampleImages[5], null);
return customView;
}
}
};
I managed to create a drawer and have the hamburger sign but the hamburger is not working when tapped. Also how can I change the code so that my app has a transparent notification bar so that the color is same(or preferably a little darker) and one can see the app drawer opened in status bar. Something like this: Transparent status bar
FirstActivity.java:
public class FirstActivity extends AppCompatActivity {
DrawerLayout mDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me_clicked);
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false); //removes the package name from toolbar
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Window w = getWindow(); // in Activity's onCreate() for instance //Integration of app into status bar
// w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// }
// These lines are needed to display the top-left hamburger button
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Make the hamburger button work
mDrawer = (DrawerLayout) findViewById(R.id.DL);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawer,R.string.app_name,R.string.app_name){
#Override
public void onDrawerClosed(View drawerView) {
}
#Override
public void onDrawerOpened(View drawerView) {
}
};
mDrawer.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
// toasts the message when ListView item is clicked
ListView mDrawerListView = (ListView) findViewById(R.id.left_drawer);
mDrawerListView.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String drawerstring = ("Menu Item at position " + position + " clicked.");
mDrawer.closeDrawer(GravityCompat.START);
Toast.makeText(getApplicationContext(),drawerstring,Toast.LENGTH_SHORT).show();
}
});
}
You have to override onOptionsItemSelected and handle the home item to open the drawer. Its not done for you, because Android doesn't know what you're using that button for (home? back? hamburger? Something else?). The ActionDrawerToggle knows how to handle it if you want to just delegate it.
So, I've this little activity and I want to add a back button (the standard arrow one) I search for the answers here and I found this little code:
public class PozosFluyentes extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pozos_fluyentes);
ActionBar actionBar = getSupportActionBar();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
}
This crash my app, so anyone could help me with this please! I know is something pretty usual but I new to android development. Thanks in advance.
It's probably you add an android.support.v7.widget.Toolbar and not using a Theme that includes NoActionBar. Theme.AppCompat.Light.NoActionBar like theme will fix the issue. But if it does not, share your xml, styles.xml and logcat to check error. With little info the first thing to look is this. Also you can check here and see you need to disable theme provided ActionBar
Replcae your code with below.
ActionBar actionBar = getSupportActionBar();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (actionBar!= null) { // you have to use actionBar (ActionBar Object) instead.
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
}
let me know it working or not.....Hope this will helps you...(:
I would like to be able to hide the Toolbar on swipe to a specific tab and then lock it. Expanding it and then locking would also work for me although i prefer the first.
I have tried doing stuff like in the code below, but it gives rise to some snappy behavior because the page is scrolled of the screen originally. As soon as i set the scroll flags to 0, the whole page snaps back up and then locks the screen with the toolbar expanded which makes sense because with scroll flags set to zero, the page should not be able to scroll off the screen at all so it just snaps back up.
The page I am tending to is a chat page and in order to have a static text input bar at the bottom I really need to disable the scrolling for that page while making it possible for the others.
Can you guys think of some way to accomplish this?
Activity:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group);
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// add all the needed fragments for the tabs inside a Vector
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(pagerAdapter);
viewPager.setOffscreenPageLimit(4);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.clearOnPageChangeListeners();
viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListenerGroups(tabLayout, this, fab, appBarLayout));
viewPager.setCurrentItem(CURRENT_TAB);
}
public void disableToolbarScrolling()
{
logger.d("disabling scrolling toolbar");
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0); // clear all scroll flags
toolbar.setLayoutParams(params);
}
public void enableToolbarScrolling()
{
logger.d("enabling scrolling toolbar");
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); // clear all scroll flags
toolbar.setLayoutParams(params);
}
And then using a PageChangeListener to handle the event:
public TabLayoutOnPageChangeListenerGroups(TabLayout tabLayout, Activity activity, FloatingActionsMenu fab, AppBarLayout appBarLayout)
{
...
this.appBarLayout = appBarLayout;
...
}
private void enableScrolling()
{
((GroupActivity) mActivity).enableToolbarScrolling();
toolbarState = toolbarState_enabled;
}
private void disableScrolling()
{
((GroupActivity) mActivity).disableToolbarScrolling();
toolbarState = toolbarState_disabled;
}
#Override
public void onPageSelected(int position)
{
if(position == 0)
{
logger.d("pos 0");
disableScrolling();
appBarLayout.setExpanded(false);
}
else
{
logger.d("pos != 0");
enableScrolling();
}
}
EDIT 1:
I have tried setting toolbar visibility to both GONE or INVISIBLE. But they only make the toolbar white, giving a white bar at the top of the screen. Still allowing for the scrolling behaviour.
add this line to your code in onCreate
setSupportActionBar(toolbar); and then add
toolbar.setVisibility(View.GONE);
For hiding the toolbar you can just try :
getSupportActionBar().hide();
I had made a common layout to include V7 toolbar in every activity , and its common class file CommonToolbar.java to handle its properties.
layout_common_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_common"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:background="?attr/colorPrimaryDark"/>
CommonToolbar.java
public class CommonToolbar {
Activity activity;
Toolbar toolbar;
CommonToolbar(Activity activity, View view, String title) {
super();
this.activity = activity;
toolbar = (Toolbar)view.findViewById(R.id.toolbar_common);
toolbar.setTitle(title);
toolbar.setTitleTextColor(activity.getResources().getColor(R.color.white));
}
void setNav() {
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
activity.onBackPressed();
}
});
}
void setMenuListner() {
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
/* case R.id.searchItem:
Toast.makeText(getApplicationContext(), "Search", Toast.LENGTH_SHORT).show();
break;
case R.id.filterItem:
Toast.makeText(getApplicationContext(), "Filter", Toast.LENGTH_SHORT).show();
break;*/
}
return true;
}
});
toolbar.inflateMenu(R.menu.common_toolbar_menu);
}
}
Using it like:
In Layout
<include android:layout_height="#dimen/abc_action_bar_default_height_material"
android:id="#+id/includeSurgicalDetail"
android:layout_width="match_parent"
layout="#layout/layout_common_actionbar"/>
In Activity
View includedLayout = findViewById(R.id.includeSurgicalDetail);
CommonToolbar commonToolbar = new CommonToolbar(SurgicalDetail.this, includedLayout, pName);
commonToolbar.setMenuListner();
commonToolbar.setNav();
But I get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.greyline.eswasthyaprojectv3/com.eswasthyaV3.SurgicalDetail}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on a null object reference
If you do not want to write the same option menu code, you should extend the Activity instead.
Write a BaseActivity class like:
public class BaseActivity extends Activity{
protected Toolbar toolbar;
public void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstanceState);
//initialize the Toolbar, set the menu listener, nav, etc
toolbar = (Toolbar)findViewById(R.id.toolbar_common);
}
}
Then for all the Activity that you want to have the share code, extends BaseActivity instead of the Activity. So you do not need to write the same option menu code over and over.