How to add a view programmatically to RelativeLayout? - android

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);

Related

How I can use java code for creating design on android

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))

Programmatic RelativeLayout overlapping

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.

Android Relativelayout Programmatically

Basically I'm attempting to add rows to a table, I need to do this programmatically.
I can make it add the content I want, but not exactly how I want it.
For example I want a textview on the left with a button edittext button on the right.
Basically I want it like the image below:
.
Also I don't want to just and width to the textview.
Anyway here's what I've made so far:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="5dp" xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/btnOutstandingJob"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Outstanding Job" />
<Button
android:id="#+id/btnVanStock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Van Stock" />
<Button
android:id="#+id/btnRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Register User" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btnLogout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
android:text="Log Out" />
</RelativeLayout>
</LinearLayout>
Java:
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
TableRow row = new TableRow(vanstock.this);
RelativeLayout RowLayout = new RelativeLayout(vanstock.this);
RowLayout.setId(99);
TextView lblItem = new TextView(vanstock.this);
lblItem.setId(1);
lblItem.setText("ddfsgsdfgs");
RelativeLayout.LayoutParams lblitemParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lblitemParam.addRule(RelativeLayout.CENTER_VERTICAL,
RelativeLayout.TRUE);
lblitemParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
RelativeLayout.TRUE);
lblItem.setLayoutParams(lblitemParam);
RowLayout.addView(lblItem);
Button btnPlus = new Button(vanstock.this);
btnPlus.setId(4);
btnPlus.setText("+");
btnPlus.setWidth(40);
RelativeLayout.LayoutParams btnPlusParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
RelativeLayout.TRUE);
btnPlus.setLayoutParams(btnPlusParams);
RowLayout.addView(btnPlus);
EditText txtItem = new EditText(vanstock.this);
txtItem.setId(3);
txtItem.setWidth(40);
RelativeLayout.LayoutParams txtItemParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
txtItemParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
txtItemParams.addRule(RelativeLayout.LEFT_OF, btnPlus.getId());
txtItem.setLayoutParams(txtItemParams);
RowLayout.addView(txtItem);
Button btnMinus = new Button(vanstock.this);
btnMinus.setId(2);
btnMinus.setText("-");
btnMinus.setWidth(40);
RelativeLayout.LayoutParams btnMinusParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
btnMinusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
btnMinusParams.addRule(RelativeLayout.LEFT_OF, txtItem.getId());
btnPlus.setLayoutParams(btnMinusParams);
RowLayout.addView(btnPlus);
row.addView(RowLayout);
table.addView(row, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
I've tried searching etc., but I still can't get it to work correctly.. Any help would be appreciated!
LayoutInflater inflater = getLayoutInflater();
TableRow row = (TableRow) inflater.inflate(R.layout.table,
_tablelayout, false);
TextView textview = (TextView) row.findViewById(R.id.rowdata);
Like this way you can fullfill your requirement.Is this sufficient??
Best
V.k

How to align ImageView to the right side of the LinearLayout Programmatically android

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.

Relate a programmatically RelativeLayout's position with other already defined in the xml

In my xml I have:
<RelativeLayout
android:id="#+id/mainWeek"
android:layout_height="387dip"
android:layout_width="match_parent"
android:layout_marginTop="5dip">
<RelativeLayout
android:id="#+id/day1"
android:layout_marginTop="5dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_height="50dip"
android:layout_width="match_parent">
...
</RelativeLayout>
</RelativeLayout>
in code I want something like:
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, R.id.day1);
rlp.addRule(RelativeLayout.ALIGN_LEFT, R.id.day1);
TextView tv1 = new TextView(this);
tv1.setText("A");
layout.addView(tv1);
this.addContentView(layout, rlp);
but the "layout" and its "tv1" appears on the top of the screen and not in below the "day1".
What am I doing wrong?
Thanks
Maybe you actually want the add the layout view to the RelativeLayout identified by the id mainWeek:
RelativeLayout mainWeek = (RelativeLayout) findViewById(R.id.mainWeek);
//Build the layout view object
mainWeek.addView(layout, rlp);
With the method addContentView()(i think) you append the layout view directly to the main window.

Categories

Resources