0

I am trying to add a button to my app in android studio, i followed a youtube video, but everytime I run it, it shuts down. I dont know why this happens, and have no idea how to get a button working. if anyone could help that would be great

public class MainActivity extends Activity {
    Draw draw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout l = new LinearLayout(this);
        l.setOrientation(LinearLayout.VERTICAL);
        setContentView(l);
        l.addView(new Draw(this));
        setContentView(R.layout.activity_main);

        draw = new Draw(this);
        draw.setBackgroundColor(Color.BLUE);
        setContentView(draw);

        setUpBlockBtn();
    }

    private void setUpBlockBtn(){
        Button addBlockButton = (Button)findViewById(R.id.btnBlock);

        addBlockButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("DemoButtonApp", "you clicked the button");
                //finish();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}
Ciaran
  • 697
  • 1
  • 12
  • 35
  • put your logcat output here to know about error?. But may be it happen because you use setContentView(l); two times. – Md Abdul Gafur Jun 21 '15 at 07:48
  • possible duplicate of [how to add button dynamically in android?](http://stackoverflow.com/questions/1851633/how-to-add-button-dynamically-in-android) – Ahmad Sanie Jun 21 '15 at 08:27

3 Answers3

0

you are using setContentView 3 times, why?? if you have defined button in activity_main.xml then use it as contentview. in your programm when you are calling setUpBlockBtn() to access Button object you have to set the correct contentview, in this case it should be activity_main.xml. again at line setContentView(draw); you are changing it that can not instantiate the button findViewById(R.id.btnBlock); so you app is crashing. in short set correct contentView

ThatsME
  • 147
  • 7
  • Ok thanks, how do i do this though, i dont really understand what it does and how it works? – Ciaran Jun 21 '15 at 08:04
  • define button in activity_main.xml with id btnBlock. and `@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button addBlockButton = (Button)findViewById(R.id.btnBlock); addBlockButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "Button Clicked", Toast.LENGTH_SHORT).show(); } }); }` google it for basic tutorials :) – ThatsME Jun 21 '15 at 08:17
0

You have two options:

1- Creating a xml layout file with button in it:

create a new layout file as activity_main in res/layout directory

<?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/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Action!"
  />

</LinearLayout> 

and use this layout in your Activity:

public class MainActivity extends Activity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        setUpButton();
    }

    private void setUpButton() {
        // Do something with your button
    }
}

2- Adding button dynamically to your Activity:

public class MainActivity extends Activity {

    Button button;

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

        button = new Button(this);
        // set button's properties

        setContentView(button);
        setUpButton();
    }

    private void setUpButton() {
        // Do something with your button
    }
}
Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
0

There is a simpler way to add a Button Create a blank xml file In the text add

<Button 
     android:id="@+id/button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
/>

This should create a button