Wes Lange
Wes Lange

Reputation: 71

Adding a Button to a Pre-existing ToolBar from the Theme

So far, it seems that I need to provide a custom Toolbar in order to add any Button. From my meager understanding, I can only get "3 dots" to show in the right-side of the Toolbar given by the Theme. On top of that, the "3 dots" button appears to only allow a menu of items.

The following code causes a crash:

toolbar = findViewById( R.id.notes_toolbar );
setSupportActionBar( toolbar );

And thus, I get:

Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

Avoiding a re-write of several of several class files, is there a simple way of addressing this?

Thank you kindly.

styles.xml :

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

Course_NoteTaking_Activity.java :

public class Course_NoteTaking_Activity extends AppCompatActivity {

    private Button saveButton;

    private EditText notes_EditText;

    private Toolbar toolbar;


    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_course_notetaking );




        toolbar = findViewById( R.id.notes_toolbar );





        //TODO  CAUSES A CRASH FROM PRE-EXISTING TOOLBAR.
        //setSupportActionBar( toolbar );





        //getSupportActionBar().setDisplayHomeAsUpEnabled( false );
        //getSupportActionBar().setTitle( "Notes for the Course" );


        Intent intent = getIntent();
        String testString = intent.getStringExtra( "notes" );


        saveButton = findViewById( R.id.notes_saveButtonXML );
        saveButton.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick( View view ) {

                Intent returnIntent = new Intent();

                String transferString = "Just testing... 1, 2, and a 3!!!";

                returnIntent.putExtra( "return_Notes", transferString );

            }
        });


        notes_EditText = findViewById( R.id.notes_EditTextXML );
        notes_EditText.setText( testString );

    }

}

activity_course_notetaking.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >


    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/notes_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:elevation="4dp"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="8dp"
            android:text="Test Button"
            android:layout_gravity="right"
            />

    </android.support.v7.widget.Toolbar>


    <LinearLayout
        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:gravity="center_horizontal"
        android:orientation="vertical"

        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:scrollbars="vertical"
        android:showDividers="none"

        tools:context="com.weslange.Term_Scheduling.Course_ChangingDetails_Activity"
        >


        <Button
            android:id="@+id/notes_saveButtonXML"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Return to the Course's Details (Not Saved Yet)"

            android:layout_marginVertical="2dp"
            />


        <EditText
            android:id="@+id/notes_EditTextXML"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName|textCapWords"

            android:imeOptions="actionDone"

            android:layout_marginVertical="2dp"
            />

    </LinearLayout>

</RelativeLayout>

Upvotes: 0

Views: 34

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006539

it seems that I need to provide a custom Toolbar in order to add any Button

You do not need a custom Toolbar to add action bar items that show up as buttons in the action bar. You are welcome to use a Toolbar, of course, but that is not necessary.

From my meager understanding, I can only get "3 dots" to show in the right-side of the Toolbar given by the Theme

Or, you can inflate a menu resource, and add action bar items that either show up in the overflow ("3 dots") or as buttons (if there is room). This is the classic way of adding things to the action bar, and you can do it with Toolbar as well.

is there a simple way of addressing this?

In terms of the crash, don't use a theme that has an action bar. For example, you could replace parent="Theme.AppCompat.Light.DarkActionBar" with parent="Theme.AppCompat.Light.NoActionBar".

Upvotes: 1

Related Questions