简体   繁体   中英

Android start new activity when clicking widget

I am new with coding Java and xml for android applications and I wanted to know how I start/open a new activity when clicking on something. In this case I am using a relative layout to keep a image and text together as one object. What I want to do is when you click on it it will start/open the new activity. How do I do this? Could someone tell me step by step since I am quite new to this.

First of all, if you want your layout to act ( RelativeLayout ) like a button (do not handle onClick on layout child components) firstly set in your xml layout file RelativeLayout attribute

android:clickable="true"

Or you can do this directly in your code (in onCreate method)

relativeLayout.setClickable(true);

Than you need to set onClickListener for your layout.
You can do this simply by creating anonymous class

relativeLayout.setOnClickListener(new OnClickListener() 
         {

            @Override
            public void onClick(View v)
             {
              Intent startActivityIntent = new Intent(getApplicationContext(),YourDesiredActivity.class);
              startActivity(startActivityIntent);
             }
          }

UPDATE

Layout is defined in xml file, of course in Android you can do this in code ,but it is better to use xml file.

In your IDEA you have folder res->layout here you should place your layout files. For example layout with name `

relative_root_layout.xml

    <xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/relative_layout"
android:layout_height="wrap_content" android:layout_width="wrap_content">
        <ImageView
          android:id="@+id/image_view">
          android:layout_width="wrap_content"
          android:src="@drawable/icon"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
        ImageView>
        <TextView
          android:id="@+id/text_view"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="20sp"
          android:layout_toRightOf="@+id/image_view"
          android:text="Relative layout">
       TextView>
    RelativeLayout>

But in case you have only text and image it is better to use

<Button
 android:id="@+id/button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:drawableLeft="@android:drawable/btn_image"
 android:text="Button with Image"
 android:gravity="left"
 android:drawablePadding="10dp">
Button>

How you can access your widgets ?
This is very basic thing you have to know if you are developing for android, this is essential part. Please read documentation, read books, watch tutorial or whatever.

In short you need to inflate layout in activity onCreate() method RelativeLayout mRelativeLayout;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.relative_root_layout);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
        mRelativeLayout.setOnClickListener(.....)

}

But again this very basic things you must know.

You could set onClickListener for any of your views.

ImageView image = (ImageView) findViewById(R.id.image);
image.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(Youractivity.this, Moveactivity.class));
    }
});

Starting a new activity is done by creating an Intent and then calling startActivity , eg

Intent intent = new Intent(context, AnotherActivity.class);
context.startActivity(intent);

You can wrap this code in an OnClickListener as other answerers already suggested.

A second option is to add an android:onClick attribute to your RelativeLayout

<RelativeLayout ...
                android:onClick="clickMe">
    <ImageView .../>
    <TextView .../>
</RelativeLayout>

and in your activity

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void clickMe(View notused) {
        Intent intent = new Intent(this, AnotherActivity.class);
        this.startActivity(intent);
    }
}

See startactivity for a complete example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM