I would like to have an activity which would consist of a close button i.e \'x\' at the right top corner closing the activity. Any examples on this will be very much helpful
In your xml add one button view
syntax:
<Button android:id="+@id/close"
android:text="close"
android:layout_margin="5sp"
android:layout_height="25sp"
android:layout_width="25sp"
android:onClick="closeActivity" />
and inside your activity or java class
syntax:
public void closeActivity(View v){
Intent intent=new Intent(currentclass.this,statingclass.class);
startActivity(intent);
finish();
Maybe an "Up" button is what you really need instead of a close button. From the official Android documentation: "Your app should make it easy for users to find their way back to the app's main screen. One simple way to do this is to provide an Up button on the app bar for all activities except the main one. "
Details are described here: https://developer.android.com/training/appbar/up-action.html
Use juzt have an x like image at the right top of corner. By pressing x image u can finish the activity.
XML:
<ImageView
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:align_parentRight="true"
android:src="@drawable/closeimage"
/>
In Java Code:
ImageView view=(ImageView)findViewById(R.id.close);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(currentclass.this,statingclass.class);
startActivity(intent);// here u can start another activity or just call finish method to close the activity.
finish();
}
});
make an Activity
with Transparent theme in manifest like this:
<activity android:name=".MyDialogActivity" android:theme="@style/Theme.Transparent" />
and then define layout like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_margin="15dp" android:padding="2dp">
<!-- Contents will go here..-->
</RelativeLayout>
<Button android:layout_alignParentRight="true" android:text="X"
android:textColor="#FFF" android:background="@drawable/round_button_background"
android:gravity="center_vertical|center_horizontal"
android:layout_margin="5dp" android:layout_height="25dp"
android:layout_width="25dp" android:textSize="12sp" android:textStyle="bold"
android:onClick="cancelActivity" />
</RelativeLayout>
and define a background for your close Button
in drawable like this:
round_button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#9F2200" />
<stroke android:width="2dp" android:color="#FFF" />
</shape>