Toolbar title disappearing when changing orientation in collapsed state - android

I am trying to create a collapsible toolbar like the one on chesesquare repository, but I am facing this problem:
https://www.youtube.com/watch?v=THdxcyEc1CA&feature=youtu.be
Anyone know how to solve this problem?

You can check the state of AppBarLayout on configuration change(orientation), store it. Then you can set that state for the AppBarLayout after configuration change applied.
public class CheeseDetailActivity extends AppCompatActivity {
public static final String EXTRA_NAME = "cheese_name";
AppBarLayout appBarLayout;
boolean isCollapsed = false;
//state change listener
AppBarLayout.OnOffsetChangedListener toolbarStateListener = new AppBarLayout.OnOffsetChangedListener() {
#Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (verticalOffset == 0) {
// Collapsed
isCollapsed = true;
} else {
// Not collapsed
isCollapsed = false;
}
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Intent intent = getIntent();
final String cheeseName = intent.getStringExtra(EXTRA_NAME);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(cheeseName);
//getting app bar layout
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
//set listener to listen state change of app bar layout
appBarLayout.addOnOffsetChangedListener(toolbarStateListener);
loadBackdrop();
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
//save state on orientation change
savedInstanceState.putBoolean("isCollapsed", isCollapsed);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//get state on orientation change
isCollapsed = savedInstanceState.getBoolean("isCollapsed");
}
#Override
protected void onResume() {
super.onResume();
//set state of app bar layout
appBarLayout.setExpanded(isCollapsed);
}
private void loadBackdrop() {
final ImageView imageView = (ImageView) findViewById(R.id.backdrop);
Glide.with(this).load(Cheeses.getRandomCheeseDrawable()).centerCrop().into(imageView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sample_actions, menu);
return true;
}
}
I have edited CheeseDetailActivity in CheeseSquare repository.

On the onConfigurationChanged you could try to run
handler.postDelayed(new Runnable(){
#Overrride
public void run(){
toolbar.invalidate();
}
},[Try different time lapses(miliseconds)]);

Related

textView inside bottoSheet doesn't change

I try to change textView text every time i click my marker. But the the textView doesn't change the text. The log shows the text but the setText
doesn't work
public boolean onMarkerClick(Marker marker) {
Halte h = arrayListHalte.get(Integer.parseInt(marker.getSnippet()));
Log.d(TAG, "onMarkerClick: " + h.getNamaHalte());
tvNamaHalte.setText(h.getNamaHalte());
mBottomSheet.setState(BottomSheetBehavior.STATE_EXPANDED);
fab.hide();
return false;
}
this is my oncreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
tvNamaHalte = (TextView) findViewById(R.id.nama_bs);// i get my textView from bottomSheet
}
Just change the code as follows and try...
public boolean onMarkerClick(Marker marker) {
Halte h = arrayListHalte.get(Integer.parseInt(marker.getSnippet()));
Log.d(TAG, "onMarkerClick: " + h.getNamaHalte());
mBottomSheet.setState(BottomSheetBehavior.STATE_EXPANDED);
tvNamaHalte.setText(h.getNamaHalte());
fab.hide();
return false;
}

Disable CollapsingToolbarLayout

my application provide different contents, some of them is with image and others not.
For those which have image i am using CollapsingToolbarLayout it works fine but for those which have not image I want collapse CollapsingToolbarLayout and prevent expanding, disable it.
Here is my item activity code:
public class ItemActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setStatusBarScrimResource(R.color.black_transparent_190);
collapsingToolbar.setContentScrimResource(R.color.gray_transparent_150);
collapsingToolbar.setExpandedTitleTextAppearance(R.style.MyTheme_Toolbar_Title_Expanded);
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
appBarLayout.setExpanded(false);
loadData();
}
public void loadData() {
if(haveImage) {
// loading data and image
appBarLayout.setExpanded(true, true);
} else {
appBarLayout.setExpanded(false);
}
}
}
I have already tried
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) collapsingToolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
collapsingToolbar.setLayoutParams(params);
and
CollapsingToolbarLayout.LayoutParams mParams = new CollapsingToolbarLayout.LayoutParams(collapsingToolbar.getLayoutParams());
mParams.setCollapseMode(CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_OFF);
mToolbar.setLayoutParams(mParams);
It works, it's prevent collapsing!
But collapsing toolbar is expanded!
How can I collapse toolbar and disable it for scrolling events?
You use setExpanded(false) on appBarLayout as normal, but you need to create a custom CollapsingToolbarLayout as below
I followed the idea in this link.
Override CollapsingToolbarLayout and override dispatchTouchEvent returning true like this
public class MyCustomCollapsingToolbarLayout extends CollapsingToolbarLayout {
public MyCustomCollapsingToolbarLayout(Context context) {
super(context);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return true;
}
}

App crashes on orientation change in android

My application crashes when I flip orientation. If the commented out line in onCreate runs I can't flip orientation at all. without it I can flip but can't change my background image but when it flips back it still crashes.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private DrawerLayout mDrawerLayout;
private ListView mListView;
private ImageView bgimage;
int[] drwables = {R.drawable.cindy, R.drawable.fred, R.drawable.kate, R.drawable.keith, R.drawable.matt, R.drawable.rickey};
private ActionBarDrawerToggle toggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null){
Toast.makeText(this, "made it", Toast.LENGTH_SHORT).show();
int n = savedInstanceState.getInt("imageId");
Toast.makeText(this, ""+n, Toast.LENGTH_SHORT).show();
bgimage = (ImageView) findViewById(R.id.imgOne);
int[] drwables = {R.drawable.cindy, R.drawable.fred, R.drawable.kate, R.drawable.keith, R.drawable.matt, R.drawable.rickey};
Toast.makeText(this, ""+drwables.length, Toast.LENGTH_SHORT).show();
//This code here
//bgimage.setImageResource(drwables[n]);
}
else{
bgimage = (ImageView) findViewById(R.id.imgOne);
}
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mListView = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
String[] names = getResources().getStringArray(R.array.bandImages);
ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, R.layout.nav_list_row, R.id.textView, names);
mListView.setAdapter(itemsAdapter);
mListView.setOnItemClickListener(this);
toggle = new ActionBarDrawerToggle(this, mDrawerLayout, (Toolbar) findViewById(R.id.toolbar), R.string.open, R.string.closed) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(getTitle());
invalidateOptionsMenu();
}
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
getSupportActionBar().setTitle(getTitle());
invalidateOptionsMenu();
}
};
mDrawerLayout.addDrawerListener(toggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toggle.syncState();
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
int n = Integer.parseInt(bgimage.getTag().toString());
savedInstanceState.putInt("imageId", n);
super.onSaveInstanceState(savedInstanceState);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mListView);
menu.findItem(R.id.action_about).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#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);
return true;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
bgimage = (ImageView) findViewById(R.id.imgOne);
bgimage.setImageResource(drwables[position]);
bgimage.setTag(position);
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(R.id.action_about == id){
Toast.makeText(this, "Lab 2 Spring 2016, Zack G Johnson", Toast.LENGTH_SHORT).show();
return true;
}
else{
return super.onOptionsItemSelected(item);
}
}
}
Try to place setContentView method before finding and using layout elements.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//put it here
setContentView(R.layout.activity_main);
if(savedInstanceState != null){
Toast.makeText(this, "made it", Toast.LENGTH_SHORT).show();
int n = savedInstanceState.getInt("imageId");
Toast.makeText(this, ""+n, Toast.LENGTH_SHORT).show();
bgimage = (ImageView) findViewById(R.id.imgOne);
int[] drwables = {R.drawable.cindy, R.drawable.fred, R.drawable.kate, R.drawable.keith, R.drawable.matt, R.drawable.rickey};
Toast.makeText(this, ""+drwables.length, Toast.LENGTH_SHORT).show();
//This code here
//bgimage.setImageResource(drwables[n]);
}
else{
bgimage = (ImageView) findViewById(R.id.imgOne);
}
...

Android Back Arrow icon on ActionBar not working

For some reason this activity has the back arrow in the actionbar, but when it is clicked it has no reaction, doesn't even seen to recognize the click. I have other activities that are similar that work fine though. Here is the code with some stuff removed:
public class LanguageActivity extends ActionBarActivity {
private static final String TAG = "LanguageActivity";
#InjectView(R.id.listView)
RecyclerView mRecyclerView;
#State
String selectedLang;
LangViewHolder selectedHolder = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Icepick.restoreInstanceState(this, savedInstanceState);
setContentView(R.layout.recyclerview);
ButterKnife.inject(this);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle(R.string.ad_title_settings_lang);
selectedLang = PreferencesFacade.getInstance().getCurrentLang();
mRecyclerView.setLayoutManager(new TrueWrapContentLinearLayoutManager(this));
List<Pair<String, String>> langList = new LinkedList<>();
langList.add(Constants.Languages.US);
langList.add(Constants.Languages.LATIN_AMERICA_SPANISH);
mRecyclerView.setAdapter(new RecycleViewMappedArrayAdapter(R.layout.view_language_item, new LangViewHolder(mRecyclerView), langList));
}
#Override
protected void onSaveInstanceState(Bundle outState) {
Icepick.saveInstanceState(this, outState);
super.onSaveInstanceState(outState);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void finish() {
super.finish();
PreferencesFacade.getInstance().setCurrentLang(selectedLang);
Log.v(TAG, "Finishing?");
}
public class LangViewHolder extends RecycleViewMappedArrayAdapter.ViewHolder<Pair<String, String>>{
...
}
}
Manifest snippet:
<activity android:name=".activities.LanguageActivity"
android:parentActivityName=".activities.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.MainActivity"/>
</activity>
This seems to be working for me:
Toolbar tBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tBar); // sets the Toolbar as the actionbar
tBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp));
tBar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed(); // calls the System onBackPressed method
}
});
Update:
getDrawable(int id) is depreciated so you should change this line of code:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= Build.VERSION_CODES.LOLLIPOP){
tBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp, getApplicationContext().getTheme()));
} else {
tBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp));
}
why don't You extend AppCompatActivity(i think actionbaractivity is deprecated...)
?
and
Toolbar toolbar = (Toolbar) findViewById(R.id.the_id);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//try this instead
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});

Add toolbar dynamically to an abstract activity

I have a base activity that all of my activities extend from. This class includes basics like a toolbar and a navigation drawer.
I am looking for a solution that will allow me to override a method in extended activities so I can change the type of toolbar that activity will use.
public abstract class BaseActivity extends AppCompatActivity {
private Toolbar mToolbar;
private FrameLayout mContentFrame;
private ActivityFragment mActivityFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(setLayout());
initViews();
initToolbar();
}
protected int setLayout() {
return R.layout.activity_base;
}
//I want to override this and provide a different toolbar layout!
protected int setToolbarLayout(){
return R.layout.toolbar;
}
protected void initViews() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mContentFrame = (FrameLayout) findViewById(R.id.contentFrame);
}
private void initToolbar() {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mToolbar.setElevation(10f);
}
}
protected void setContentFragment(ActivityFragment fragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(getContentFrame().getId(), fragment).commit();
supportInvalidateOptionsMenu();
this.mActivityFragment = fragment;
setTitle(fragment.getTitle(this));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public Toolbar getToolbar() {
return mToolbar;
}
public FrameLayout getContentFrame() {
return mContentFrame;
}
public ActivityFragment getActivityFragment() {
return mActivityFragment;
}
I have tried inflating the toolbar after setContentView() and setting the actionbar to it but it appeared with no icons or widgets!
Ok everyone (whoever it concerns) I figured it out. All I had to do was inflate my contentView prior to calling setContentView(). Once I have that inflated I add the inflated toolbar view!
public abstract class BaseActivity extends AppCompatActivity {
private Toolbar mToolbar;
private FrameLayout mContentFrame;
private ActivityFragment mActivityFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(inflateLayout());
initViews();
initToolbar();
}
protected int getLayoutRes(){
return R.layout.activity_base;
}
protected int getToolbarLayout(){
return R.layout.toolbar;
}
private View inflateLayout() {
View contentView = getLayoutInflater().inflate(getLayoutRes(), null, false);
attachToolbarToLayout(getLayoutInflater(), (ViewGroup) contentView);
return contentView;
}
private void attachToolbarToLayout(LayoutInflater inflater, ViewGroup parent){
mToolbar = (Toolbar) inflater.inflate(getToolbarLayout(),parent,false);
parent.addView(mToolbar,0);
}
protected void initViews() {
mContentFrame = (FrameLayout) findViewById(R.id.contentFrame);
}
private void initToolbar() {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mToolbar.setElevation(10f);
}
}
protected void setContentFragment(ActivityFragment fragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(getContentFrame().getId(), fragment).commit();
supportInvalidateOptionsMenu();
this.mActivityFragment = fragment;
setTitle(fragment.getTitle(this));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public Toolbar getToolbar() {
return mToolbar;
}
public FrameLayout getContentFrame() {
return mContentFrame;
}
public ActivityFragment getActivityFragment() {
return mActivityFragment;
}

Categories

Resources