Android Button Onclick

JavaAndroid

Java Problem Overview


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" />

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

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

    }
 });

Solution 4 - Java

Use something like this :

   public void onClick(View v) {
            // TODO Auto-generated method stub
           startActivity(new Intent("com.droidnova.android.splashscreen.MyApp"));
        }

Solution 5 - Java

Here is some sample code of how to add a button named Add. You should declare the variable as a member variable, and the naming convention for member variables is to start with the letter "m".

Hit Alt+Enter on the classes to add the missing references.

Add this to your activity_main.xml:

 <Button
        android:id="@+id/buttonAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD"
     />

Add this to your MainActivity.java:

public class MainActivity extends AppCompatActivity {

    Button mButtonAdd; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButtonAdd = findViewById(R.id.buttonAdd);

        mButtonAdd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // do something here
            }
        });
    }
}

Solution 6 - Java

It would be helpful to know what code you are trying to execute when the button is pressed. You've got the onClick property set in your xml file to a method called setLogin. For clarity, I'd delete this line android:onClick="setLogin" and call the method directly from inside your onClick() method.

Also you can't just set the display to a new XML, you need to start a new activity with an Intent, a method something like this would be appropriate

 private void setLogin() {
 
 Intent i = new Intent(currentActivity.this, newActivity.class);
 startActivty(i);
 
 }

Then set the new Activity to have the new layout.

Solution 7 - Java

There are two solutions for this are :-

(1) do not put onClick in xml

(2) remove

button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
            // TODO Auto-generated method stub
    }
});

and put

public void setLogin(View v) {
    // Your code here
}

Solution 8 - Java

this will sort it for you

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    Button but1=(Button)findViewById(R.id.button1);

    but1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent int1= new Intent(MainActivity.this,xxactivity.class);
            startActivity(int1);
        }
    });
}

You just need to amend the xxactivity to the name of your second activity

Solution 9 - Java

Use Layout inflater method in your button click. it will change your current .xml to targeted .xml file. Google for layout inflater code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1344259View Question on Stackoverflow
Solution 1 - Javaalishaik786View Answer on Stackoverflow
Solution 2 - Javaישו אוהב אותךView Answer on Stackoverflow
Solution 3 - JavaHalf MoonView Answer on Stackoverflow
Solution 4 - JavaErwaldView Answer on Stackoverflow
Solution 5 - Javalive-loveView Answer on Stackoverflow
Solution 6 - JavaSam ClewlowView Answer on Stackoverflow
Solution 7 - JavaRudra SaraswatView Answer on Stackoverflow
Solution 8 - JavaSuper TurtleView Answer on Stackoverflow
Solution 9 - JavaShrawanView Answer on Stackoverflow