Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have activity in Android with a close button at the top corner?

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.

like image 289
Karthik Avatar asked Jan 24 '12 06:01

Karthik


People also ask

How do I make a button go to another activity?

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.

How do you add functionality to a button in android?

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.

How do I skip back pressed activity on android?

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.


2 Answers

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>
like image 178
Adil Soomro Avatar answered Oct 20 '22 00:10

Adil Soomro


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();
like image 44
Ranjit Mishra Avatar answered Oct 20 '22 01:10

Ranjit Mishra