1

I'm working with the new android toolbar.

/* Jump into, after the user clicks on a listview item */
private void toogleToolbar() {
    if (isStandardToolbar)
        customToolbar();
    else
        originalToolbar();

    isStandardToolbar = !isStandardToolbar;
}

/* Called inside onCreate and if nothing was clicked */
private void originalToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
}


/* Called after an click event */
private void customToolbar() {
    LayoutInflater inflater = this.getLayoutInflater();
    Toolbar toolbar = (ToolBar) inflater.inflate(R.layout.newtoolbar, null);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
}

It works so far, but now I want to change the toolbar view, after clicking on a list element. That's my problem, because the last code snippet doesn't produce an exception or other issues, so it should be all ok. But I see only the "old" toolbar. I tried to set visibility to GONE or INVISIBLE to the old one, but it doesn't have any effect.

In my activity_main.xml, I include R.id.toolbar, but I think, the second code must overwrite the old one!?

EDIT: AFAIK, the new toolbar should replace the old actionBar. Toolbar is used to place navigation or other, specific content. I my case, I want to create a small action menu, where the user can edit or delete a list item.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
user3417078
  • 265
  • 4
  • 15
  • Add some code else. Whatever, I think you are not using correctly the call to Toolbar. Indeed, I bet for that. Try insert whole Toolbar and play with object.visibility(View.GONE) and View.VISIBLE. – Jnmgr Oct 19 '15 at 15:26
  • I think the toolbar should be part of activity layout. In other case it would not show up. Do you want to have specific layout for toolbar based on some conditions? – Abdullah Oct 19 '15 at 15:44
  • Yes, I want to have a other layout on some conditions. I made my first post up. – user3417078 Oct 19 '15 at 16:00

1 Answers1

1

I found the solution to your question (maybe its too late...) but below is the code I used to make it :

MainActivity.java :

package fr.zwedge.sot;

import android.app.*;
import android.os.*;
import android.support.v7.app.*;
import android.support.v7.widget.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;

public class MainActivity extends AppCompatActivity {

boolean isStandardToolbar = true;
android.support.v7.widget.Toolbar toolbar = null, newToolbar = null;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initToolbars();
    ((Button)toolbar.findViewById(R.id.tbt)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            toogleToolbar();
        }
    });
    ((Button)newToolbar.findViewById(R.id.tbt2)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            toogleToolbar();
        }
    });
}

/* Jump into, after the user clicks on a listview item */
private void toogleToolbar() {
    if (isStandardToolbar)
        customToolbar();
    else
        originalToolbar();
    isStandardToolbar = !isStandardToolbar;
}

private void initToolbars() {
    if (toolbar == null)
        toolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.toolbar);
    if (newToolbar == null)
        newToolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.newtoolbar);
}

/* Called inside onCreate and if nothing was clicked */
private void originalToolbar() {
    toolbar.setVisibility(View.VISIBLE);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
}


/* Called after an click event */
private void customToolbar() {
    toolbar.setVisibility(View.GONE);
    setSupportActionBar(newToolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
}
}

And here are the two toolbars in main.xml :

<LinearLayout 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.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:titleTextColor="@android:color/holo_green_dark"
    android:background="#FF008F12">
    <Button
        android:text="Bla bla bla"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/tbt" />
</android.support.v7.widget.Toolbar>

<android.support.v7.widget.Toolbar
    android:id="@+id/newtoolbar"
    android:minHeight="?attr/actionBarSize"  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:titleTextColor="@android:color/holo_green_dark"
    android:background="#FF8F0800">
    <Button
        android:text="Change once again"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tbt2" />
</android.support.v7.widget.Toolbar>

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/text_view" />

Hope it helps you, Darkball60

Mesabloo
  • 337
  • 4
  • 13