I need dynamically create next xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="a lot of text will be here"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
/>
</LinearLayout>
and here is my code:
LinearLayout ll = new LinearLayout(getActivity());
ll.setLayoutParams(layoutLP);
TextView title = new TextView(getActivity());
title.setText("a lot of text will be here");
title.setLayoutParams(textLP);
EditText editText = new EditText(getActivity());
editText.setLayoutParams(ediLP);
editText.setSingleLine();
ll.addView(title);
ll.addView(editText);
layoutLP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutLP.gravity = Gravity.CENTER;
textLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
textLP.weight = 2;
textLP.gravity = Gravity.CENTER;
editLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
editLP.weight = 3;
editLP.gravity = Gravity.CENTER;
However, on device I get problem: text
inside textView
is not wrapped and partly covered by editText
.
Why does this happen?
And here is how it must looks like:
Your Activity should be
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams layoutLP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutLP.gravity = Gravity.CENTER;
layoutLP.weight = 2;
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout.setLayoutParams(layoutLP);
linearLayout.setLayoutParams(layoutLP);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams textLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
textLP.weight = 2;
textLP.gravity = Gravity.CENTER;
LinearLayout.LayoutParams editLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
editLP.weight = 3;
editLP.gravity = Gravity.CENTER;
// Add textview 1
TextView textView1 = new TextView(this);
textView1.setLayoutParams(textLP);
textView1.setText("programmatically created TextView1");
// textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout1.addView(textView1);
// Add textview 1
EditText editText = new EditText(this);
editText.setLayoutParams(editLP);
editText.setHint("programmatically created TextView1");
// editText.setBackgroundColor(Color.GRAY); // hex color 0xAARRGGBB
editText.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout1.addView(editText);
linearLayout.addView(linearLayout1);
Your xml file should be:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/ll"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>