Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android eclipse button OnClick event

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 up home.xml. How can i do this? I don't know any 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. Please, if you reply, tell me where to add the code such as the directory or between code snippets.

like image 874
Jacob Anthony Tonna Avatar asked Mar 13 '13 04:03

Jacob Anthony Tonna


1 Answers

For managing click activity in android ,you can do as below

  1. Implement OnClickListener on YourActivity.java class like

    public class MainActivity extends Activity implements OnClickListener

  2. Then, declare your button in .java class like

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

  3. Then use button btn variable as below

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

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

you also need to register your activity(.java) in android manifest as below

<activity
    android:name=".Swipe"
    android:screenOrientation="landscape" >
</activity>
like image 117
Kirtikumar A. Avatar answered Sep 24 '22 01:09

Kirtikumar A.