Question

I'm recently started to program and faced with this problem. My button is not clicking in emulator, however, I wrote onClickListener in java. It still doesn't work.

Here is my xml code:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/SD"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:background="@color/blue_gray"
android:visibility="visible"
android:onClick="onClick"/>
<Button
    android:id="@+id/btn2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/SD2"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="70dp"
    android:background="@color/follow"
    android:visibility="gone"
    android:onClick="onClick"/>
</FrameLayout>

Here is my java code:

enter code here package com;

           import android.app.Activity;
           import android.os.Bundle;
           import android.view.View;
           import android.widget.Button;
           import android.widget.ListView;
           import android.widget.TextView;
           import com.example.app.R;

           /**
           * Created by ww on 12.02.14.
           */



           public class fragment_main extends Activity  {

           Button i1;
           Button i2;


            protected void onCreate (Bundle SavedInstanceState){
            super.onCreate(SavedInstanceState);
            setContentView(R.layout.fragment_main);

             i1= (Button) findViewById(R.id.btn1);
             i2=(Button) findViewById(R.id.btn2);



             }




             public void onClick(View view) {
             switch (view.getId()) {
             case R.id.btn1:
             i1.setVisibility(View.GONE);
             i2.setVisibility(View.VISIBLE);
             break;
             case R.id.btn2:
             i1.setVisibility(View.VISIBLE);
             i2.setVisibility(View.GONE);

             break;
              }
              }
              }
Was it helpful?

Solution

Here's the working code,

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sd"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:onClick="onClick"/>

<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sd2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="140dp"
android:visibility="gone"
android:onClick="onClick"/>    

</RelativeLayout>

MainActivity.java

package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity  {

    Button b1;
    Button b2; 

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         b1 = (Button) findViewById(R.id.btn1);
         b2 = (Button) findViewById(R.id.btn2);
    }


    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btn1:
                b2.setVisibility(View.VISIBLE);
                b1.setVisibility(View.INVISIBLE);
           break;
            case R.id.btn2:
                b2.setVisibility(View.INVISIBLE);
                b1.setVisibility(View.VISIBLE);
           break;

        }
     }
  }


Output (Compiled & ran in Emulator):

  • When the app starts Button2 will be invisible since it was set as invisible in xml layout.

    enter image description here

    • Once the user click button1 above, button2 will be visible & button1 will be invisible.



    enter image description here

OTHER TIPS

you have to register a listener to your buttons, e.g.:

i1 = (Button) findViewById(R.id.btn1);
i2 = (Button) findViewById(R.id.btn2);
//inside onCreate do this:
i1.setOnClickListener(myhandler1);
i2.setOnClickListener(myhandler2);

Then you have to create those listeners.

// somewhere outside onCreate do this:
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
  // it was the 1st button
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
  // it was the 2nd button
}
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top