0

For testing purpose:

Here, for example:

<TextView    
     android:id="@id/expander"
     android:text="Fewer Details"
     android:layout_gravity="center_vertical|start"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

gravity int equivalent is:8388627.

How reparse 8388627 for obtain string "center_vertical|start" ?

ilw
  • 2,499
  • 5
  • 30
  • 54

1 Answers1

1

Looking at the Gravity object, you need to translate the XML descriptors into the object's constants:

  • center_vertical -> Gravity.CENTER_VERTICAL Constant Value: 16 (0x00000010)
  • start -> Gravity.START Constant Value: 8388611 (0x00800003)

You add them and you get your value (8388627).

The reverse can be done with logical operations in 3 steps:

  • eliminate the generic flags (like Gravity.RELATIVE_LAYOUT_DIRECTION)
  • progressively eliminate the other flags
  • recombine the generic and normal flags back (like LEFT + RELATIVE_LAYOUT_DIRECTION = START) - this is optional

My approach would be:

Create a Map<int, String> modifiers
Create a Map<int, String> flags
Create a List<int> components

Populate modifiers with all generic flags and their corresponding strings
  Use something like (flag > 0x0010000 && (flag & 0x0000FFFF == 0))
Populate flags with all other elements ( < 0x0010000)

// Note: Don't populate modifiers with elements like Gravity.START

int value = my_value_to_parse

for (int i in modifiers.keys()) {
    if (value & i > 0) {
        components.add(i)
        value = value & !i
    }
}
// Same code for the flags
for (int i in flags.keys())
    ...

// One can add logic for merging composite flags here

// And add the components to a string
String result = ""
for (int component: components) {
   if (modifiers.containsKey(component))
       result += modifiers.get(component)
   else if (flags.containsKey(component))
       result += flags.get(component)

   result += "|"

// Return everything except last | character
return result.substring(0, result.length() - 2)

You can use this question to generate a Map of strings and codes.

To convert the list to a string you can also create a List and use a Join (guava) or Apache Commons.

Community
  • 1
  • 1
Laur Ivan
  • 4,117
  • 3
  • 38
  • 62
  • I need back translate for : `String str = "center_vertical|start"`; – ilw Aug 11 '16 at 15:11
  • NO SIMILAR question. Yes ! programmatically – ilw Aug 11 '16 at 15:12
  • I'm afraid you'll have to go through the Gravity object and implement the per-item translation yourself. You'd have to do the splitting into values ("|") and push the list of strings through your mapper. – Laur Ivan Aug 11 '16 at 15:14
  • Example added. Normally, you should show what you've tried :) – Laur Ivan Aug 11 '16 at 18:29
  • OP wants to parse the string and convert it to Gravity (int) flags. - NO. i want: `int gravity = 8388627; /* some code, and then...*/ ; String str = "center_vertical|start"; Ok? I upload my class to github with public access. Please edit the method https://gist.github.com/FreakMurderer/410bea2401dcf53c10cd856e38b5d515#file-xmldeparser-java-L183 based on the discussion. Yes , i know - my english is terrible :) – ilw Aug 11 '16 at 19:36
  • Thank you. But i wish PRACTIC example . Please , edit the my gist , if you want... – ilw Aug 12 '16 at 09:26
  • Ok ok... but gist - is code writing service. With stackoverflow we done, welcome to the gist. :) – ilw Aug 12 '16 at 09:36
  • [These](http://programmers.stackexchange.com/q/28129) are places for this kind of requests. – Laur Ivan Aug 12 '16 at 09:44