Generate UI from a string [duplicate] - android

I had lots of research about this issue but I didn't get the answer that I want.
So I do have an application getting string from the server. The series of string is in XML Format.
Here is an example of what I shall get from the server (as you can see it's a layout):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/number_ref" />
<EditText
android:id="#+id/etTxtNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/subject_ref" />
<EditText
android:id="#+id/etTxtSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/body_ref" />
<EditText
android:id="#+id/etTxtBody"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btnTxtSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/save_ref"/>
<Button
android:id="#+id/btnTxtSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/send_ref"/>
<Button
android:id="#+id/btnTxtClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/close_ref"/>
</LinearLayout>
</LinearLayout>
This will be changed depending on what xml file will be uploaded from the server. My problem is how can I implement these series of string into layout and to be loaded as layout of an activity. I was thinking of saving it in an xml file to sdcard of the device and load it as layout but I guess it's not possible to recompile the code after running it. Any suggestions? Thank you.

As explained in the Android documentation, the layout xml files are not used at runtime - they are compiled to binary code at compile time. So, you can't directly load the xml as a Layout.
You'd have to write code that reads the xml, interprets it, and uses Android api methods to create a corresponding View.

Common use cases
If you need to A/B test different UI designs without the need to re-upload your APK.
If your app's UI changes dynamically based on different users or scenarios.
If you need to deploy UI fixes quickly and in real-time.
json2view can help you.

I think now it can be done using the dynamic layout's in android.
https://www.javacodegeeks.com/2012/09/android-dynamic-and-xml-layout.html#:~:text=Android%20activity%20contains%20various%20user,activity%20in%20two%20different%20ways.&text=Basically%2C%20Layout%20file%20is%20xml,file%20present%20in%20res%2Flayout.
https://medium.com/mindorks/creating-dynamic-layouts-in-android-d4008b72f2d

Related

#id is not working inside Relative Layout

As far as I know, the difference between #+id and #id is to create a resource id first time and reuse that already existed resource id in different places. For instance, If we have a Relative layout having two textViews one below another, we shall use #resourceId for the second textView which refers to the first TextView.
The problem is, after updating the android studio to 3.0, #resourceId is not working anymore.To place second textView below the first one, I need to use #+firstTextViewId instead of #firstTextViewId. More specifically I need to use,
android:layout_below="#+id/totalQty"
instead of
android:layout_below="#id/totalQty"
Here is the code
<RelativeLayout
android:id="#+id/relBottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/totalQty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abcdef"/>
<TextView
android:id="#+id/totalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/totalQty"
android:text="saasdfdsdfsdf"/>
<TextView
android:id="#+id/totalNetPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/totalPrice"
android:text="abcdsadfsafddgfdgfgdef"
/>
</RelativeLayout>
Is it an understanding issue? or a problem from any end? Can anyone please explain?
I just remove + sign at #+id from your code. Here's the updated code
<RelativeLayout android:id="#+id/relBottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/totalQty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abcdef"/>
<TextView
android:id="#+id/totalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/totalQty"
android:text="saasdfdsdfsdf"/>
<TextView
android:id="#+id/totalNetPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/totalPrice"
android:text="abcdsadfsafddgfdgfgdef"
/>
</RelativeLayout>

How to port RelativeLayout from Android source code?

Background
I have a card-like UI on my app, which uses a RelativeLayout.
The problem
Recently I've discovered that on at least one device ( or rom, or andorid version), when I switch to an RTL language (such as Hebrew), everything got messed up.
This has happened only on LG G2 with Android V4.2.2 .
Here's what I see:
If you compare it to the screenshots of the app, you can see this is not how it should look like.
That's even though on all Android devices that I've checked, and on all emulators, I've never seen it (even when switching to Hebrew).
What I've tried
I've tried to fix it by using a GridLayout instead, but that wasn't working well (link here).
I've also tried to use a LinearLayout, but considering there might be issues with it, I've used LinearLayoutCompat instead. It works, but it's not recommended to use so many.
Another thing I've tried is to port the RelativeLayout of Android source code, but this gives me a lot of warnings and errors that are quite hard to handle.
Here's the original XML of the layout, BTW (I've removed the irrelevant stuff, to make it shorter) :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="afterDescendants" >
<ImageView
android:id="#+id/appIconImageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:src="#android:drawable/sym_def_app_icon" />
<LinearLayout
android:id="#+id/appDetailsContainer"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/appIconImageView"
android:layout_toLeftOf="#+id/overflowView"
android:layout_toRightOf="#+id/appIconImageView"
android:layout_toStartOf="#+id/overflowView"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:text="label" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="description" />
</LinearLayout>
<ImageView
android:id="#+id/overflowView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#android:drawable/sym_def_app_icon" />
<ImageView
android:id="#+id/isSystemAppImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="#android:drawable/sym_def_app_icon" />
</RelativeLayout>
The question
As I use RelativeLayout, how can I port the code from the official source code of Android?
Is there maybe a library that does it?

Code okay and yet I get error - XML document structures must start and end within the same entity

I am taking the first step into android programming and XML. I am using the Big Nerd Ranch Guide book. In Eclipse, I used the XML given in the book and I got the error XML document structures must start and end within the same entity.
How do I fix this?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="#string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button" />
</LinearLayout>
</LinearLayout>
Problem is solved. I removed the code and saved the xml file. After that, I replaced it with the above code and saved. The error is gone. It was probably a remanant from the time when "automatically build project" was enabled.
Strange.

Internal link to TextView in Android?

I find myself stuck in something I think would be really easy to solve. My app contains a lot of TextViews and ImageViews, and one TextView contains the content listed. I want each element of the displayed content internally linked to the respective TextView. Probably the TextView isn't the right element to use, but I have trouble finding the right element.
The internal link should work exactly like internal links in html-documents... is this possible to achieve in Android?
strings.xml:
<string name="c3">3. Contents</string>
<string name="c4">1. Abstract
\n3. Contents
\n4. List of Abbreviations
\n5. Introduction
\n6. Materials & Methods
\n6.1 Literature Selection
\n6.2 Method
\n6.3 What is Android?
\n6.3.1 Dalvik Virtual Machine and Android Applications
\n6.3.2 Android Structure, Java and XML
\n6.3.3 Android Versions
\n6.4 The Development Environment
\n6.4.1 Android SDK
\n6.4.2 Eclipse IDE
\n6.4.3 Android Virtual Device
\n6.4.4 Secure Digital Card
\n6.5 “Hello World!” as Test of IDE
\n6.6 Test Application
\n6.7 Developing Strategy
\n7. Result
\n7.1 Facebook Connect
\n7.1.1 Facebook Application
\n7.1.2 Facebook Connect for Android
\n7.2 Networking
\n7.3 Data Synchronization
\n8. Discussion
\n9. Conclusion
\n10. References
\n10.1 Internet References
\n10.2 Lecture References\n\n
</string>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/smoke"
android:scrollbars="vertical"
>
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scrollbars="none"
>
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="4px"
android:paddingRight="4px"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/a"
android:textColor="#color/black"
android:gravity="center_horizontal"
android:textSize="16dp"
android:paddingBottom="2dp"
android:paddingTop="20dp"
android:scrollbars="vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/b"
android:gravity="center_horizontal"
android:textColor="#color/black"
android:scrollbars="vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/c1"
android:textColor="#color/black"
android:textSize="20dp"
android:paddingBottom="2dp"
android:scrollbars="vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/c2"
android:textColor="#color/black"
android:scrollbars="vertical"
/>
If I were you, I'd convert your content into HTML with internal links and use a WebView to display it. I have no idea how you would pull off what you are trying to do any other way. Moreover, that's the typical approach used for ebooks, which appears to be what you are trying to create.

Help Arranging TextView objects on screen in Android

I'm using textview objects to hold labels such as Score, Level etc on my game screen but they don't seem to be displayed where I want them to be. I understand about view hierarchies (parents, children) and am using the gravity tags in the XML layout file but it doesnt seem to have any effect.
Could someone just quickly provide a guide to positioning a textview object on the screen, and also linking it in the code so that its contents can be programmatically controlled (I believe this would by done via =(TextView) findViewById(r.id.resourcename))?
Many thanks
XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gestures"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gestureStrokeWidth="2.0"
android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true">
<com.darius.android.distractions.DistractionsView
android:id="#+id/distractions_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/text"
android:text="Hello"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#88ffffff"
android:textSize="24sp"/>
<TextView
android:id="#+id/textLevel"
android:text="#string/level_count"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="bottom"
android:textColor="#EB0000"
android:textSize="10sp"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/concentration_bar"
android:textColor = "#EB0000"
android:textSize="10sp"
android:id="#+id/conbartext"
android:visibility="visible"
></TextView>
</RelativeLayout>
</android.gesture.GestureOverlayView>
</FrameLayout>
These are some really helpful tutorials on getting started with android layout and widgets: http://developer.android.com/resources/tutorials/views/index.html
Documentation and a guide on the layout itself is here:
http://developer.android.com/guide/topics/ui/declaring-layout.html
Common Layout Objects is a good guide to the basics of layouts. The gravity tag only has meaning in certain layout types.
Another useful tool is the Heirarchy Viewer, found in the tools folder of your Android installation. This allows you to visualize the View heirarchy of your running activity.
If you post your layout XML, and a mockup of what you are trying to accomplish, we might be able to help you accomplish it.

Categories

Resources