Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML layout to Java code converter

Tags:

java

android

xml

I know that in Android all XML layout files are converted to Java code on compilation. Is there any way to have access to those java codes (layout in java)?

Or if there isn't a way, is there any converters I can find?

--

For example:

XML layout:

<TextView
    android:layout_width="50px">
</TextView>

Java code:

TextView textview = new TextView();
textview.setWidth(50);

--

As you can see, this is just a simple example, however, layouts are usually much longer. It would take a very long time to convert it line by line.

Any suggestions?

like image 588
raybaybay Avatar asked Nov 03 '25 17:11

raybaybay


2 Answers

Is there any way to have access to those java codes (layout in java)?

There are no such "codes". You are welcome to examine the source to LayoutInflater, which, as you will see, looks substantially different than what you envision.

is there any converters I can find?

Asking for off-site resources is considered off-topic for Stack Overflow. For the record, I am not aware of any, and I'd be stunned if somebody spent their time on it.

It would take a very long time to convert it line by line.

Then write it in Java in the first place. Or, use the XML layout resources directly.

I'm working on a project where it will be much easier to work with Java code rather than XML

While I cannot rule out this possibility, it would be rather surprising.

like image 126
CommonsWare Avatar answered Nov 05 '25 09:11

CommonsWare


You can try this solution:

http://www.xmltojava.com/

For your example:

<TextView
   android:layout_width="50px">
</TextView>

Will result in the following Java code:

TextView textView_706 = new TextView(this);
LayoutParams layout_420 = new LayoutParams();
layout_420.width = 50px;
textView_706.setLayoutParams(layout_420);

Obviously, the naming is a little problematic but it's a small refractor.

like image 32
Emil Adz Avatar answered Nov 05 '25 10:11

Emil Adz