Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android How to make back button without Actionbar and Toolbar?

I made a theme like this in res/values/style.xml and applied to remove ActionBar.

<style name="AppTheme.NoActionbar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">false</item>
    </style>

And I made a LinerLayout looks like a ActionBar with title and a button(it will have back function).

I'd like to upload the pictures, but I can't upload them yet.

This is a part of the layout.xml

<LinearLayout
        android:id="@+id/ActionBarProductInfo"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@color/colorPrimary"
        android:orientation="horizontal">

            <ImageButton
                android:id="@+id/backToMain"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_gravity="center"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:background="@drawable/image_back_34dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="210dp"
                android:layout_marginStart="210dp"
                android:text="Input product information"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                android:textStyle="bold" />

    </LinearLayout>

How can I put a back function in the button(id:backToMain)?

like image 478
JungHa Avatar asked Oct 15 '25 14:10

JungHa


1 Answers

Simply bind your id of ImageButton in your java and set clicklistner on that id like this

ImageButton back = (ImageButton)findViewById(R.id.backToMain);
back.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onBackPressed();
    }
});
like image 99
Amrish Kakadiya Avatar answered Oct 18 '25 14:10

Amrish Kakadiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!