Take the 2-minute tour ×

I don't like to manage XML and Java together, can I create same GUI using Java language? How can I do that, can you tell me code for simple Button? I will appreciate the proper answer.

share|improve this question

3 Answers 3

Yes, you can.

public class MyActivity extends Activity {
     protected void onCreate(Bundle icicle) {

         final Button button = new Button(this);
         button.setText("Press me!");

         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
share|improve this answer

Can I create same GUI using Java language?

Yes you can create GUI in Java code also as answered by @dtmilano but in general it's not a good practice for Android applications. Its easy in case of a small application but if you are going to develop an application for End User than your must have to create GUI using XML files. Its also useful when you want to develop application targeted for multiple devices with different-different display size and different-different languages.

The best practice is that try to avoid creating GUI using Java and instead use XML as much you can.

share|improve this answer

If you are using Eclipse, you can go to folder res/layout from your project where you will find the file main.xml Right click this file and choose Open with/Android layout editor There you will see a graphical tool that will generate all that is needed to be included in the main.xml file

share|improve this answer

Your Answer


