Oscar Arranz
Oscar Arranz

Reputation: 714

Delete button programatically on custom Action Bar

I'm wondering how can I delete a button programatically on my app custom action bar.

This action bar has 2 buttons, a back button and a settings button.

I want to hide (or delete, the user shouldn't be able to interact with the button or see it) the settings button at the settings activity.

At the home activity I want to hide the back button (as you can't go back).

Could anyone help?

Custom bar xml:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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="wrap_content"
    android:theme="@style/CustomToolBarStyle">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorDarkBlue">

        <Button
            android:id="@+id/backButton"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:background="@drawable/back_icon"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button3"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:background="@drawable/settings_icon"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.95"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:gravity="center"
            android:text="@string/app_name"
            android:textColor="@color/colorWhite"
            android:textSize="20sp"
            app:fontFamily="sans-serif"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.46"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.571" />
    </android.support.constraint.ConstraintLayout>


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

Upvotes: 0

Views: 302

Answers (2)

Ashvin Bhagat
Ashvin Bhagat

Reputation: 212

Insert this wherever you want to hide your button -

backButton.setVisibility(View.GONE);

Upvotes: 0

Ahmet
Ahmet

Reputation: 48

Whenever you want to hide

button3.setVisibility(View.GONE);

Whenever you want to show

button3.setVisibility(View.VISIBLE);

Upvotes: 1

Related Questions