5

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.

javathunderman
  • 1,074
  • 2
  • 13
  • 31
Jacob Anthony Tonna
  • 515
  • 3
  • 13
  • 27
  • 1
    Unless you use something like MonoDroid or some other app building alternative, Java is a pretty fundamental requirement for developing Android apps. If you don't know Java then I suggest you learn that first. – Michael Celey Mar 13 '13 at 16:33

6 Answers6

8

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>
Community
  • 1
  • 1
Kirtikumar A.
  • 4,140
  • 43
  • 43
2

You can use this code.

Android: OnClickListener

In our activity class we add the onclick method.

In our activity class we add the onclick method.

    //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();               
}

In the layout file we add a reference to the onclick handler in the activity. The app will automatically bind the onclick method to the view (in this case button1)

<?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>
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
1

create another class goto your project right click and click class and create Home. In that Home class file extends activity and add code like this

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

in splash activity class add this line

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

add Home activity class in your android manifest file

<activity android:name="com.example.idozer.Home"
    android:label="@string/app_name" >
</activity>
Austin Kregel
  • 715
  • 1
  • 8
  • 27
  • Don't use int, its a reserved word in Java and probably nearly every other language ;) – codeMagic Mar 13 '13 at 04:45
  • No problem, that's better :) – codeMagic Mar 13 '13 at 05:02
  • @codeMagic the op is off line and im having a bit of a problem with this . if u have team viwer can u help me out ? ill post my session information here – Jacob Anthony Tonna Mar 13 '13 at 05:07
  • I'm sorry, I can't right now. You should have enough to get you started. I can try and answer any questions you might have later – codeMagic Mar 13 '13 at 05:11
  • @codeMagic in splash activity class add this line . this confuses me because i dont know what u mean for me to do i dont have any file named that. – Jacob Anthony Tonna Mar 13 '13 at 05:21
  • @user1978141 That's because I didn't write this answer. I merely made a comment on it to make it more correct. Please see my answer which I believe gives necessary links to help you understand what to do and descriptive information – codeMagic Mar 13 '13 at 12:52
0

android:onClick was added in API level 4 to make it easier, more Javascript-web-like, and drive everything from the XML. What it does internally is add an OnClickListener on the Button, which calls your home method.

<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);
}

In your activity class

Using java code you can do button click by getting the id of the button from 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
  }
});   

Both are exactly the same

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Close, but you wouldn't want `android:onClick="home" if you are implementing the `onClickListener` programmatically. It will be looking for a function with the signature `public void home(View v)` – codeMagic Mar 13 '13 at 04:53
  • I gave 2 ways do the same. Either use the 1st one or the second. However i recommend doing it the 2nd way (programatically calling button listener) – Raghunandan Mar 13 '13 at 04:58
  • I understand but you have `android:onClick="home"` in both. I'm saying if you do the second way(programmatically) then you don't want that line in the xml. Which way to do it really depends on what you are doing with it. Yes, it seems to be the most popular and probably normally the "best" choice but I use both depending on what I am doing with it – codeMagic Mar 13 '13 at 05:02
  • @codeMagic agreed i removed the line form xml for the 2nd way. – Raghunandan Mar 13 '13 at 05:07
0

I will give you just a little bit to get you started as this answer may help others that are having trouble with using onClick() this way but you really need to learn Java and go through the Android Docs so you can ask better questions

You need to read Here about Actviities and how to create them. Then in your code you will have a function

 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);
}

You also need to add the HomeActivity in your manifest as you have for the other Activities.

But you really need to go through the docs and do some tutorials to get an idea of how the Android framework operates and you need to learn Java to make your life a whole lot easier. Besides the previous two links I have given, also see this post about click events as there are different ways to use onClick()

I hope this is enough to get you started and I really hope you go through the docs to get a better understanding of what you are doing. Good luck to you!

Another important link to get started

Intents

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
0
Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
        }
    });
Omid Farvid
  • 785
  • 7
  • 13