I have got a problem by implementing relative layout from code.
What I want, that TextView1 is located to the left, and TextView2 to the right of the screen at one line.
It works fine from XML, but doing this from code do not... what could be the reason?
This work just fine:
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/hello_world" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="TextView" />
</RelativeLayout>
This doesn't work(giving an overlapping of elements):
RelativeLayout rel = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
rel.setLayoutParams(params);
TextView t1 = new TextView(this);
t1.setText("balbla_1");
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
t1.setLayoutParams(lp1);
TextView t2 = new TextView(this);
t2.setText("balbla_2");
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
t2.setLayoutParams(lp2);
rel.addView(t1);
rel.addView(t2);
setContentView(rel);
but this would work fine: (replace MATCH_PARENT with WRAP_CONTENT):
RelativeLayout rel = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
rel.setLayoutParams(params);
TextView t1 = new TextView(this);
t1.setText("balbla_1");
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
t1.setLayoutParams(lp1);
TextView t2 = new TextView(this);
t2.setText("balbla_2");
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
t2.setLayoutParams(lp2);
rel.addView(t1);
rel.addView(t2);
setContentView(rel);
The Java code is not an exact match to the XML, the overlapping is because you have forgotten:
lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
You have also neglected:
lp1.addRule(RelativeLayout.CENTER_VERTICAL);
And you are using MATCH_PARENT for the widths instead of WRAP_CONTENT.
Related
I am trying to put TextView into FrameLayout into Linear layout.
When I do it using XML it's OK, but using Java code there are problems.
So I can't move text view using layout_gravity in Java code. Also, I don't understand why text matching parent when I use a parameter wrap content. must be
XML code
<LinearLayout
android:id="#+id/chatBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_gravity="right"/>
</FrameLayout>
Java code
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
TextView tv = new TextView(this);
tv.setText(message);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
p.gravity = Gravity.LEFT; tv.setLayoutParams(p);
fl.addView(tv);
LinearLayout chatbox = (LinearLayout) findViewById(R.id.chatBox);
chatbox.addView(fl, 0);
you are adding TextView tv to FrameLayout, so you should use FrameLayout.LayoutParams for this TextView
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
p.gravity = Gravity.LEFT;
tv.setLayoutParams(p);
fl.addView(tv);
You need to LayoutParams for your Textview not Linearlayout Params. That is causing the problem. You can create it like;
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
How do I write the above XML in code?
android.widget.LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
Is that right?
//Create the layout params
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//Create the LinearLayout
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL); //set orientation
ll.setLayoutParams(params); //asign your layout params
I'm adding TextViews and EditTexts to the the UI, but they are overlapping each other. I want them to appear next to each other. What I am missing in this code?
ScrollView sv = new ScrollView(this);
RelativeLayout ll = new RelativeLayout(this);
ll.setId(99);
sv.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setBackgroundResource(R.drawable.background);
for (int i = 0; i < 10 /*Changed the actual value for better Understanding*/; i++) {
tv = new TextView(this);
tv.setText("" + (productStr[i]));
tv.setId(i);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 18);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_RIGHT, RelativeLayout.TRUE);
ll.addView(tv, lay);
et = new EditText(this);
et.setInputType(InputType.TYPE_CLASS_NUMBER);
et.setEms(2);
allEds.add(et);
et.setId(i);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
ll.addView(et, p);
}
this.setContentView(sv);
Use LinearLayout instead of RelativeLayout, and leave positioning to the layouter.
PS: it's spelled "them" not "dem".
There is no need to add extra layouts, you could do it with the RelativeLayout. The code below should layout the TextView and EditText like you want:
ScrollView sv = new ScrollView(this);
RelativeLayout ll = new RelativeLayout(this);
ll.setId(99);
ll.setBackgroundResource(R.drawable.background);
sv.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
for (int i = 0; i < 10; i++) {
tv = new TextView(this);
tv.setText("" + (productStr[i]));
tv.setId(1000 + i);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 18);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
if (i != 0) {
lay.addRule(RelativeLayout.BELOW, 2000 + i - 1);
}
ll.addView(tv, lay);
et = new EditText(this);
et.setInputType(InputType.TYPE_CLASS_NUMBER);
et.setEms(2);
et.setId(2000 + i);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.RIGHT_OF, tv.getId());
if (i != 0) {
p.addRule(RelativeLayout.BELOW, 2000 + i - 1);
}
ll.addView(et, p);
}
this.setContentView(sv);
Just use LinearLayout in your xml. Something like this will generate a TextView next to the EditText
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
</EditText>
</LinearLayout>
</LinearLayout>
P.S: There are few norms which everyone follows in asking questions on this forums, like not using chat language in your question. Make short and sweet title and you can write any amount of explanation in your post.
Can you give me a very simple example of adding child view programmatically to RelativeLayout at a given position?
For example, to reflect the following XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="107dp"
android:layout_marginTop="103dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
I don't understand how to create an appropriate RelativeLayout.LayoutParams instance.
Heres an example to get you started, fill in the rest as applicable:
TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);
The docs for RelativeLayout.LayoutParams and the constructors are here
Firstly, you should give an id to your RelativeLayout let say relativeLayout1.
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
TextView mTextView = new TextView(context);
mTextView.setText("Dynamic TextView");
mTextView.setId(111);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
mainLayout.addView(mTextView, params);
i want to align an ImageView to the right side of the LinearLayout Programmatically.How can i do this.
You don't. That is not what a linear layout does. Use a RelativeLayout instead.
XML
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Programmatically
public void makeView(Context context) {
RelativeLayout rl = new RelativeLayout(context);
TextView tv = new TextView(context);
tv.setText("Hello, World");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rl.addView(tv, lp);
}
You can try this:
ImageView view = (ImageView)findViewById(R.id.imageView);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.LEFT;
But you should use RelativeLayout instead of LinearLayout.