ANDROID JAVA - Define all id's at once. (+create easy buttons)



I am new to programming in Java, i've managed to create a little calculator as a little test app. But i think i am using way to much code for my needs. So i've given a Button a name: buttonname Now to change it's text when clicked i need to:



public class MyActivity extends Activity {
Button buttonname;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);

buttomname = (Buttom) findViewById(R.id.buttomname);

}

public void buttonnameOnClick(View v) {
button1.setText ("NewText")
}

}


(i've bolted everything i had to add) So i had to do everything above + connect the buttonClick through the xml file.


So i was wondering if there is a easier way to define all objects so i dont have to do: Button buttonname; and buttomname = (Buttom) findViewById(R.id.buttomname); all the time.


And i was wondering if there is a easier way to auto create button events.


(I am used to Visual Studio, but now i am kinda lost in Android Studio. So on Visual Studio i just had to double click the button and type: buttonname.Text = "NewText";)


No comments:

Post a Comment