-5

My app runs well and shows no errors, but my button is not working ( is unClickable in device)

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button11=(Button)findViewById(R.id.button11);
        button11.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                new Intent(MainActivity.this, Main2Activity.class);

            }
        });
    }
}

and XML file :

  <Button
            android:id="@+id/button11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50px"
            android:layout_marginTop="200dp"
            android:background="@color/colorPrimary"
            android:text="صفحه اصلی"
            android:onClick="onClick"/>/>

On the real device when I click on this button nothing happens !

t.m.adam
  • 15,106
  • 3
  • 32
  • 52
MRAK
  • 25
  • 1
  • 6

1 Answers1

0

try this way

 Intent i = new Intent(MainActivity.this, Main2Activity.class);
 startActivity(i);

EDIT: I wanted to add a bit of an explanation to help

Hi MRAK,

First things first, you do NOT need to create an onClick listener. Remove all of that completely. Android studio is a beast and automatically does that for you using the XML file. When you set "onClick" in the XML file, it automatically calls the name of whatever method you put in there. You should change it so it is not also called "onClick." I would prefer to call it "startAcitivty2" or so on so you are not confused later. I stuck with your method name for now.

See below for corrected code:

public void onClick(View v){
    // note in the below line i'm just using "this"
    Intent myIntent = new Intent(this, Main2Activity.class)
    // Secondly, you need to end the current activity
    finish();
    // Third, you need to start your new activity... 
    //    Creating an Intent does not the activity alone
    startActivity(myIntent);
}

Also, this has so many downvotes because this has been asked 1000+ times. Please use google or the search bar above before asking. Google will reroute you to stackoverflow anyway :)

  • You button click should be like this button.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // Start NewActivity.class Intent myIntent = new Intent(MainActivity.this, NewActivity.class); startActivity(myIntent); } }); – coder_baba Jul 21 '17 at 05:54
  • I don't think this question is a real duplicate of the question it is marked as... But it isn't quite clear what MRAK is asking. The question is why the button is not clickable, there is no talk about activities not starting... The reason is setting the onClick method twice, once in Java via listener and then in XML, while it is unlikely for another method onClick to be existing. – deHaar Jul 21 '17 at 05:56
  • thanks for answers. I use eclipse before . so, I confused a litte in android studio. **I just shoud use"startactivity" instead of "new intent".** Now I get -5 votes for this !! – MRAK Jul 21 '17 at 06:25
  • ya its because of this question has already multiple answer in SO .so,community go down your question – Hasmukh Kachhatiya Jul 21 '17 at 06:27