OK I'm new to android dev's and Java, so I'm having problems with on click method here's my code. I know I've gotta be close, thanks in advance. All I want my button to do is, when its clicked on the phone to switch the layout view from main.xml to xx.xml
package my.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ExperiencerlActivity extends Activity {
    /** Called when the activ`enter code here`ity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
            }
        });
    }
}
Here is my button code
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linearLayout1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="56dp"
    android:onClick="setLogin"
    android:text="Login" />
                If you write like this in Button tag in xml file : android:onClick="setLogin" then
Do like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn"
    android:onClick="onClickBtn" />
</LinearLayout>
and in Code part:
public class StartUpActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);    
    }
    public void onClickBtn(View v)
    {
        Toast.makeText(this, "Clicked on Button", Toast.LENGTH_LONG).show();
    } 
}
and no need all this:
 Button button = (Button) findViewById(R.id.button1);
 button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub
    }
 });
Check it once;
You need to make the same method name both in layout XML and java code.
If you use android:onClick="setLogin" then you need to make a method with the same name, setLogin:
// Please be noted that you need to add the "View v" parameter
public void setLogin(View v) {
}
ADVICE:
Do not mix layout with code by using android:onClick tag in your XML. Instead, move the click method to your class with OnClickListener method like:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    // TODO Auto-generated method stub
  }
 });
Make a layout just for layout and no more. It will save your precious time when you need to refactoring for Supporting Multiple Screens.
Method 1:
public void onClick(View v) {
          Intent i = new Intent(currentActivity.this, SecondActivity.class);
         startActivty(i);
        }
Method 2:
Button button = (Button) findViewById(R.id.mybutton);
 button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
         Toast.makeText(this, "Button Clicked", Toast.LENGTH_LONG).show();
    }
 });
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With