OnClick Android Eclipse Button

I have 2 files: main_activity.xml and home.xml. I made a button in main_activity.xml

Here is the code snippet:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:background="@drawable/splash_background"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >

<Button
    android:id="@+id/Home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="43dp"
    android:onClick="home"
    android:text="Home" />

</RelativeLayout>

And then I have my home.xml. I want the button to open home.xml. How can i do this? I do not know java and I am new to Android development.

Here is my home.xml below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/app_bg"
    android:layout_height="match_parent"
    android:orientation="vertical" >


</LinearLayout>

And below is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.idozer"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

 <application
    android:allowBackup="false"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.idozer.SplashActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.example.idozer.MainActivity"
        android:label="@string/app_name" >
    </activity>
</application>
</manifest>

And that’s all I have. If you answer, tell me where to add code, such as a directory or code snippets.

+5
source share
5 answers

To control click activity in android you can do as below

  • Deploy OnClickListenerin YourActivity.java class , e.g.

    public class MainActivity extends Activity implements OnClickListener

  • Then declare your button in a .java class like

    Button btn = (Button) findViewById(R.id.btnPlay);

  • btn,

    btn.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View v) {
            myClick(v); /* my method to call new intent or activity */
        }
    });
    
  • click:

    public void myClick(View v) {
        Intent intent = new Intent(**this, Swipe.class**);
        startActivity(intent);// for calling the activity
    }
    

(.java) android manifest,

<activity
    android:name=".Swipe"
    android:screenOrientation="landscape" >
</activity>
+8

.

Android: OnClickListener

onclick.

onclick.

    //On click event for button1
public void button1OnClick(View v) {
    //Inform the user the button has been clicked
    Toast.makeText(this, "Button1 clicked.", Toast.LENGTH_SHORT).show();               
}

​​ onclick . onclick ( 1)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button1"
            android:onClick="button1OnClick"/>
</LinearLayout>
+2

, "". Home ,

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

Intent intent = new Intent(SplashActivity.this,Home.class);
startActivity(intent);

Android.

<activity android:name="com.example.idozer.Home"
    android:label="@string/app_name" >
</activity>
+1

android:onClick API 4, , Javascript-- XML. OnClickListener Button, .

<Button
  android:id="@+id/Home"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_alignParentTop="true"
  android:layout_marginRight="43dp"
  android:onClick="home"
  android:text="Home" />

.

public void home(View view){
  Intent intent=new Intent (view.getContext(),Luton.class);
  this.startActivity(intent);
}

java-, , xml.

<Button
  android:id="@+id/myHomeButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_alignParentTop="true"
  android:layout_marginRight="43dp"
  android:text="Home" />

.

Button button= (Button) findViewById(R.id.myHomeButton);
button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
     //do whatever you want on button click
  }
});   

0

, , , onClick() , Java Android Docs,

Actviities , .

 public void home(View v)  //the name of this function comes from where you declared in your manifest `android:onClick="home"
{
     Intent intent (MainActivity.this, HomeActivity.class); //MainActivity is the name of current activity and HomeActivity is the name of the activity you want to start
     can add intent extras/flags/categories here
     startActivity(intent);
}

HomeActivity manifest, Activities.

But you really need to go through the documents and make some tutorials to understand how the Android platform works, and you need to learn Java to make your life a lot easier. In addition to the previous two links that I gave, also see this post about click events, as there are various ways to useonClick()

I hope this is enough to get you started, and I really hope that you go through the documents to better understand what you are doing. Good luck to you!

Another important link to get started

Intentions

0
source

All Articles