3

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.

Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
Veer
  • 1,409
  • 5
  • 17
  • 16

5 Answers5

15

Yes, you can.

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

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

         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });
     }
 }
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
8

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.

Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
  • Why is creating UI with Java a bad practice? What if I want to change the position of TextViews, images or other UI objects without using GL? – iOSAaronDavid Sep 14 '15 at 20:57
  • some developers simply prefer to write Java code than to use layout tools and XML, regardless of the advantages offered by the latter approaches – Cody Tookode Sep 28 '18 at 16:37
  • https://www.techotopia.com/index.php/Creating_an_Android_User_Interface_in_Java_Code – Cody Tookode Sep 28 '18 at 16:38
3

I found this article useful maybe It's good for you too Creating an Android User Inteface in java Code

first you need to create an object for your layout like this

RelativeLayout myLayout = new RelativeLayout(this);

then create your for example button like this

Button myButton = new Button(this);

then the Button view needs to be added as a child to the RelativeLayout view which, in turn, is displayed via a call to the setContentView() method of the activity instance

myLayout.addView(myButton);
setContentView(myLayout);

Once launched, the visible result will be a button containing no text appearing in the top left hand corner of the RelativeLayout view.

Meysam Hadigheh
  • 323
  • 3
  • 10
1

Definitely you can design your Android UI using java. Here is a little example for create a Button.

Follow these steps

  1. import a layout package(here i've import android.widget.RelativeLayout)
  2. import Button package
  3. Create a layout object
  4. Create a button object
  5. Add button to layout
  6. Set Content View

Here is the code

package com.example.vmbck.app3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //create layout
    RelativeLayout myLayout = new RelativeLayout(this);
    //set background color of the layout to Green
    myLayout.setBackgroundColor(Color.GREEN);

    //create button
    Button myButton = new Button(this);
    //set button's background color to red
    myButton.setBackgroundColor(Color.RED);
    //set button's text to Click Me
    myButton.setText("Click Me");

    //add button to layout
    myLayout.addView(myButton);
    //View the content
    setContentView(myLayout);
    }

}
Buddhika Chathuranga
  • 1,334
  • 2
  • 13
  • 22
-1

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

Antonio Leite
  • 460
  • 3
  • 7