Muhammad Irfan
Muhammad Irfan

Reputation: 1457

I want to start another activity when it is clicked?

Well I have an activity which contains nothing but a relative layout with brown background, and I want to start another activity if the users clicks anywhere on the screen, how would i do that ??

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainscreen);
    }

Upvotes: 0

Views: 89

Answers (3)

user948620
user948620

Reputation:

First, give ID to your RelativeLayout by putting android:id="@+id/relativeLayout" in your layout file then.

RelativeLayout l = (RelativeLayout) findViewById(R.id.relativeLayout1);
l.setOnClickListener(new View.OnClickListener()
{

    public void onClick(View v)
    {
       // startActivity here

    }
});

or without using your RelativeLayout just implement the onTouchEvent(MotionEvent) of activity

public boolean onTouchEvent(MotionEvent event)
{
    if(event.getAction() == MotionEvent.ACTION_UP)
    {
        // start activity
    }
    return super.onTouchEvent(event);
}

Note: Not yet tried this code.

Upvotes: 2

Abhi
Abhi

Reputation: 9005

I think your Mainscreen has Parent layout either Linear,Relative,Frame. Take reference of that and handle click listener.

Ex: If your Parent layout is Linear.

LinearLayout linear=(LinearLayout)findViewById(R.id.linear);

 linear.setOnClickListener(this);

Upvotes: 1

meh
meh

Reputation: 22469

Add onClick attribute to your layout xml, and implement onClick method in your activity to start a new activity.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick"
android:orientation="vertical" >

In your activity add an onClick method.

public void OnClick(View v) {
  startActivity(new Intent(this,MyNewActivity.class));
}

Upvotes: 1

Related Questions