How to write XML equivalent code in Java in android?



I am trying to reduce all my XML files. For that I am trying to convert all XML code into java code. I've done all other very properly but unable to do this.


**Requirement:**Functionality should not change even after changing the XML to java


My xml code is:



<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_margin="1dp"
android:background="@android:color/darker_gray">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_margin="1dp"
>

<WebView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" />
<Button
android:id="@+id/dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
</LinearLayout>




What I tried in java code is:



WebView Text = new WebView(this);
String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";
String summary = "Hello</body></html>";
Text.loadData(String.format(text, summary), "text/html", "utf-8");
Button1 = new Button(this);
LinearLayout layout = new LinearLayout(this);
LinearLayout inside = new LinearLayout(this);
LayoutParams lpView = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
inside.addView(Text);
inside.addView(Button1, lpView);
ScrollView sc = new ScrollView(this);
layout.addView(sc);
layout.addView(inside);


Please correct me. I didn't get same behavior in both ways.


No comments:

Post a Comment