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.
Using an OnClickListener Inside your Activity instance's onCreate() method you need to first find your Button by it's id using findViewById() and then set an OnClickListener for your button and implement the onClick() method so that it starts your new Activity . Button yourButton = (Button) findViewById(R. id.
To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
In the manifest, add android:noHistory="true" as an attribute of the login activity. Save this answer. Show activity on this post. Calling finish() after startActivity() is the way to go.
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>
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With