27

I'm wondering if it is possible to create an Android app with only Java. No XML, no other things.

In Eclipse, when I create a new Android Project, the Manifest xml-file and the main layout xml-file is automatically generated. Can I delete those files and replace them by a Java-file?

10
  • 4
    I'm pretty sure that you cannot remove the manifest file, beause it's an essential part of the application. You can, however, do all layouting with Java instead of XML, thus restricting the usage of XML to a single file. Is there a specific reason why you want to avoid XML? Especially considering the definition of interationalized string constants etc. it's very useful. Commented Nov 7, 2011 at 13:41
  • Yes, I really don´t like XML :D It was a step forward to me, when we were able to create the DOM without HTML, and it will of course be a step forward when I am able to create layouts directly from Java. I think I can solve this language thing much better in Java rather than XML. Commented Nov 7, 2011 at 13:45
  • 2
    Well...layouting only with Java is possible, albeit not particularly pretty. XML is much better suited for structural representation than a programming language. There are some drawbacks to using XML for layouting though, especially concerning the lack of checks for correctness (wrongly typed functions in "click" attributes...eew). And I myself hate to mix XML and Java, which is why in general I prefer Java layouting for Android. However, I'm a big fan of the XML manifest. Easy to read, well structured and has a strict definition. Commented Nov 7, 2011 at 13:49
  • 1
    @VanCoding Yeah you can solve this.. you just need some data structure on file where you store your translated strings and some easy way to access them from your application. Now where have I heard some technology that does exactly that? Commented Nov 7, 2011 at 13:51
  • 2
    You´re right. XML is genious for visual structures and layouts. It´s bad for defining settings and other data. JSON would be much better for such things, since its structure is closer to the structure of a program. BUT, as genious XML for structures may be, I hate mixing things... Commented Nov 7, 2011 at 13:54

6 Answers 6

16

For the layouts you have two options

  1. Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.

  2. Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.

So for the first question - yes - you can delete xml layout files (if you must).

I think you cannot get rid of the manifest.xml..Quoting:

Every application must have an AndroidManifest.xml file (with precisely that name) in its root directory.

Sign up to request clarification or add additional context in comments.

2 Comments

Does anyone have a simple example of how to accomplish this? I see lots of people saying "yes, it's possible to avoid using XML layouts", but no one providing an example of doing so.
People prefer not to create the View objects by hand - that's why there are no/few examples. It's easier to describe what you want in XML and leave the generation of those classes for the Android platform - not to mention, that if they change those View classes in the SDK and keep the XML vocabulary the same - you will automatically use the new code. I would recommend to go with the flow on this one. Disclaimer: I have not seen Android SDK In 5 years.
5

Is it possible to avoid XML? Yes, with the exception of the manifest and perhaps some theme declarations (I'm not sure if there are public Java equivalents for everything we can set up via themes).

Is it a good idea? Heavens, no.

The point behind the resource system is to allow Android to transparently hand you the proper resources needed by the device at the present moment, based on both permanent device characteristics (e.g., screen density) and transient device characteristics (e.g., portrait vs. landscape orientation).

To avoid the resources, you will have to go through a bunch of if statements to determine which hunk of Java code to run, detecting all these things by hand. This gets significantly more complicated once you take into account changes in Android itself, as new configuration changes and values get added, making it difficult for you to support everything you need to in a backwards-compatible way.

Along the way, you will lose all tool support (drag-and-drop GUI building, MOTODEV Studio's string resource assistants, etc.), outside of plain Java editing and debugging.

You seem to be placing your own personal technical inclinations ahead of all other considerations. If this is some small personal project, that may be a fine attitude. If you are creating code to be developed and/or maintained by others over time, though, you need to factor in the needs of those other developers, and they may be much more open to XML than are you.

Comments

2

Take a look at this video, just posted by the android team: http://www.parleys.com/#st=5&id=2191&sl=8

It's all about layouts and includes how to layout apps using Java, not XML. However, you are warned that the android team wants you to use XML...

Comments

2

yes it's possible to make an app with java and no xml.
here's a simple example.
i use eclipse.
first i made my own my class called MyView, which extends View.

public class MyView extends View {

    Paint paint;

    public MyView(Context context) {
        super(context);
        setBackgroundColor(Color.BLACK);
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setTextSize(50);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(50, 50, 50, paint);
        canvas.drawText("hello", 0, 150, paint);
        canvas.drawLine(0, 150, 100, 170, paint);
    }

}

in the activity, i create a MyView object called myView.
i get rid of setContentView(R.layout.activity_main);
then i type setContentView(myView);

public class MainActivity extends Activity {

    MyView myView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      setContentView(R.layout.activity_main); // i delete this
        myView = new MyView(this);
        setContentView(myView);
    }
}

res\layout\activity_main.xml file can now be deleted.
res\layout folder can now be deleted.

Comments

1

It is possible to create all layout files from code. But it's reccomended to use the layout XML files.

The AndroidManifest.xml can not be replaced by code, since the system relies on the data included in this file.

Comments

1

I fully agree Van Coding. If you have the proper programming language, you can write much more concise layout definitions than with AXML. A C# example how to replace AXML layouts

ViewGroup Layout(ViewGroup layout, IList<View> contents) {
    if (contents != null)
        foreach (View v in contents)
            layout.AddView(v);

    return layout;
}

1 Comment

`ViewGroup Layout(ViewGroup layout, IList<View> contents) { if (contents != null) foreach (View v in contents) layout.AddView(v); return layout; }'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.