4

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?

raybaybay
  • 647
  • 6
  • 16

2 Answers2

6

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    To add to CommonsWare's point, every View can be controlled at runtime through Java code. You are not forced to use XML layout although it is much easier to do it using XML layouts. In fact, you can define the controls in XML layouts and still change them at runtime if you need to (for e.g. changing the visibility, colors etc...) – Raghu May 13 '14 at 19:39
  • You have replied to one of my questions: [link] (http://stackoverflow.com/questions/23637615/android-application-that-receives-xml-files-and-renders-them) If I were to convert every XML file I receive into Java code, then it will take a lot of time and all the logic of mapping XML functions to Java functions will be integrated in that application. However, I could transfer Java classes instead using Java reflection that will have the already-mapped layout and it can be directly rendered in my application. All the mapping from XML to Java could be done on the other end instead. – raybaybay May 13 '14 at 19:39
  • 1
    @user3158979: "However, I could transfer Java classes instead using Java reflection that will have the already-mapped layout and it can be directly rendered in my application" -- even if that were somehow simpler than simply processing the XML in-app, what you are proposing is a security disaster. – CommonsWare May 13 '14 at 19:43
  • I located a project that fully renders dynamically loaded XML into an inflated layout. The open source ItsNat project https://github.com/jmarranz/itsnat - has a sample App that demos it. Build the sample Android App - It first does a compiled binary (native layout), the you then press the reload - and it switched to the dynamic one (loaded as a plain text file). It seems to support most of the common view types. – RoundSparrow hilltx Apr 03 '15 at 12:12
5

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.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187