Dynamic Layout generation Android - android

I am having trouble generating below mentioned android layout with code .
<RelativeLayout
android:id="#+id/profile_info_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/ProfileTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D9D9D9"
android:baselineAligned="false"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0.5"
android:background="#F3F3F3"
android:orientation="horizontal" >
<TextView
android:id="#+id/gender_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="8dp"
android:textColor="#color/info_left"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="0.5"
android:background="#color/white"
android:orientation="horizontal" >
<TextView
android:id="#+id/profile_info_text_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="8dp"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
i tried alot of things from google and blogs but could not end up with this layout
any help will highly be appreciated and will be admired
thanks

Try this..
LinearLayout first_lay = new LinearLayout(this);
LinearLayout.LayoutParams lp_icon = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
first_lay.setOrientation(LinearLayout.HORIZONTAL);
first_lay.setLayoutParams(lp_icon);
LinearLayout left_lay = new LinearLayout(this);
LinearLayout.LayoutParams left_icon = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT,1);
left_lay.setGravity(Gravity.CENTER);
left_lay.setBackgroundColor(Color.parseColor("#696969"));
left_lay.setLayoutParams(left_icon);
LinearLayout.LayoutParams tests = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
TextView text1 = new TextView(this);
left_lay.addView(text1);
text1.setLayoutParams(tests);
text1.setText("Left Text");
text1.setTextSize(18);
first_lay.addView(left_lay);
LinearLayout right_lay = new LinearLayout(this);
LinearLayout.LayoutParams right_icon = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT,1);
right_lay.setGravity(Gravity.CENTER);
right_lay.setBackgroundColor(Color.parseColor("#D9D9D9"));
right_lay.setLayoutParams(right_icon);
TextView text2 = new TextView(this);
right_lay.addView(text2);
text2.setLayoutParams(tests);
text2.setText("Right Text");
text2.setTextSize(18);
first_lay.addView(right_lay);

Why not do this:
RelativeLayout profile_info_gender = new RelativeLayout(your activity);
LinearLayout ProfileTable = new LinearLayout(your activity);
profile_info_gender.addView(ProfileTable);
For orientation, use LayoutParams.

Related

Inflated layout's children overlapping with each other when adding them to a TableRow

I am trying to inflate a layout which contains a few textViews. When I add that to a tableRow, the textViews inside that layout are getting overlapped. In another words, the layout_width and layout_height are being ignored I guess.
But when I add rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); after inflating, nothing is appearing.
TableRow tableRow = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(lp);
View rowView = getLayoutInflater().inflate(R.layout.layout_result_row, null);
rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
loadResultRow(rowView, result); //This method sets the text of the textViews
tableRow.addView(rowView);
tableLayout.addView(tableRow);
layout_result_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/results_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp">
<TextView
android:id="#+id/results_row_match_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="#+id/results_row_turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/results_row_match_no"
android:layout_alignParentEnd="true"
android:text="TextView" />
<TextView
android:id="#+id/results_row_teams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/results_row_match_no"
android:text="TextView" />
<TextView
android:id="#+id/results_row_turn_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/results_row_turn"
android:layout_toStartOf="#+id/results_row_turn"
android:text="TextView" />
<TextView
android:id="#+id/results_row_toss_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/results_row_match_no"
android:layout_marginTop="22dp"
android:text="TextView" />
<TextView
android:id="#+id/results_row_home_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/results_row_toss_ml"
android:layout_alignBottom="#+id/results_row_toss_ml"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="#+id/results_row_rank_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/results_row_home_ml"
android:layout_toEndOf="#+id/results_row_turn_text"
android:text="TextView" />
<TextView
android:id="#+id/results_row_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/results_row_turn_text"
android:layout_alignStart="#+id/results_row_home_ml"
android:text="TextView" />
</RelativeLayout>
The inflated layout already has the Layout Params all the chid views are not redraw with the new Layout params. If you want to have the views same as before after you inflate you can use the below code.
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setBackgroundColor(Color.RED);
TableRow tableRow = new TableRow(this);
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(lp);
View rowView = getLayoutInflater().inflate(R.layout.activity_main, null);
tableLayout.setStretchAllColumns(true);
// rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// loadResultRow(rowView, result); //This method sets the text of the textViews
tableRow.addView(rowView);
tableLayout.addView(tableRow);
setContentView(tableLayout);
Instead of:
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
Try:
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);

Dynamic UI - How to add 3 linearlayouts into a parent linearlayout

I'm having a little bit of trouble with creating a dynamic UI.
I can get it to work with just one LinearLayout but I have no idea how to add more.
This is what I have now, it's one LinearLayout with 3 buttons.
Here is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
maakLayout();
}
private void maakLayout() {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams layoutParams = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(layoutParams);
setContentView(linearLayout);
for (int i = 0; i < 3; i++) {
Button button = new Button(this);
button.setText("Test");
button.setTag(i);
button.setBackgroundResource(R.drawable.border_red);
LayoutParams layoutTextParams = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// LayoutParams layoutImageParams =
// new LayoutParams(100,100);
layoutTextParams.topMargin = 5;
layoutTextParams.leftMargin = 5;
layoutTextParams.rightMargin = 5;
button.setLayoutParams(layoutTextParams);
linearLayout.addView(button);
}
}
What I need is the following:
And the static XML code for this is the following:
<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="horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="144dp"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/border_blue"
android:text="Test" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
</LinearLayout>
</LinearLayout>
Once I know how to add other linearlayouts I'm good I think, thank you for your help.
LinearLayout is itself a subclass of View, so you can add it as a child to other LinearLayouts.
Like so:
LinearLayout horizontalLayout = new LinearLayout(this);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout verticalLayout1 = new LinearLayout(this);
verticalLayout1.setOrientation(LinearLayout.VERTICAL);
LinearLayout verticalLayout2 = new LinearLayout(this);
verticalLayout2.setOrientation(LinearLayout.VERTICAL);
horizontalLayout.addView(verticalLayout1);
horizontalLayout.addView(verticalLayout2);
Just populate your first and third vertical layout with buttons as you do in your loop.
Hope this helps.

How add LinearLayout and RelativeLayout programmatically in a ScrollView?

How I can add LinearLayout and RelativeLayout programmatically in a ScrollView :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollID"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="1.5"
android:background="#drawable/bottom_conversation">
<TextView
android:id="#+id/isTyping"
android:layout_width="0dip"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:hint="Chat" />
<EditText
android:id="#+id/txtInpuConversation"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="#string/edt_Conversation" >
<requestFocus />
</EditText>
<ImageButton
android:id="#+id/someID"
android:layout_width="0dip"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:background="#drawable/background_selector"
android:contentDescription="#null"
android:layout_marginTop="5dp"
android:src="#drawable/social_send_now" />
</LinearLayout>
</LinearLayout>
Notice : I need that RelativeLayout insert in LinearLayout
TextView tv = new TextView(this);
tv.setText("Testsssssssssssssss");
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setId(1);
RelativeLayout relativeLayout = new RelativeLayout(this);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(tv,
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
LinearLayout firstLinearLayout= new LinearLayout(this);
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
firstLinearLayout.addView(relativeLayout);
ScrollView sclView = (ScrollView) findViewById(R.id.scrollID);
sclView.addView(firstLinearLayout, new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Good Luck...
You can add Views ( LinearLayout, RelativeLayout... ) with
scrollView.addView(newLinearLayoutView);

How to Set Margin Programatically to Layout Which is exist in another layout

i have following layout,i want to set margin programmaticaly . This layout i am accessing this layout in baseadpter. How can i access it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<CheckBox
android:layout_width="20dp"
android:layout_height="17dp"
android:button="#drawable/my_custom_checkbox"
android:id="#+id/check"
android:layout_marginTop="5dip"
android:checked="false"
>
</CheckBox>
<RelativeLayout
android:id="#+id/Relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="6dip"
android:layout_weight="0.5">
<TextView android:id="#+id/list_item_entry_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_marginTop="5dip"
android:layout_marginBottom="15dip"
android:ellipsize="marquee"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceSmall"
android:fadingEdge="horizontal" />
//Want to access this layout
<LinearLayout
android:id="#+id/LinearCal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="50dip" to 100dip
here is your code..
LinearLayout ll = (LinearLayout) findViewbyid(R.id.LinearCal);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 100, 0, 0);//set margin left, top, right, bottom
ll.setLayoutParams(layoutParams);
Use the findViewByID method.
http://developer.android.com/reference/android/view/View.html#findViewById(int)
then access its layoutparams
Try this way.
//LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(50, 10, 0, 0);
LinearLayout layout = (LinearLayout)findViewById(R.id.LinearCal);
layout.setLayoutParams(layoutParams);

Dynamical creation of UI

I want to implement the following layout in code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget92"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/widget164"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignTop="#+id/widget163"
android:layout_alignLeft="#+id/widget161" />
<Button
android:id="#+id/widget163"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignTop="#+id/widget162"
android:layout_toLeftOf="#+id/widget161" />
<Button
android:id="#+id/widget162"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_below="#+id/widget161"
android:layout_toLeftOf="#+id/widget160" />
<Button
android:id="#+id/widget161"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/widget160" />
<Button
android:id="#+id/widget160"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/widget159" />
<Button
android:id="#+id/widget159"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
Can anyone tell me how to implement this in code.
Thanx in advance.
You can create all UI elements manually with the same hierarchy as in layout xml file.
Say in Activity.onCreate()
onCreate(...) {
super.onCreate(...);
RelativeLayout layout = new RelativeLayout(...);
Button button = new Button(...);
layout.addView(button);
setContentView(layout);
}
RelativeLayout rl = new RelativeLayout(this);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
rl.setId(R.id.widget92);
Button btn = new Button(this);
btn.setId(R.id.widget164);
btn.setText("Text");
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_TOP, R.id.widget163);
lp.addRule(RelativeLayout.ALIGN_LEFT, R.id.widget161);
rl.addView(btn, lp);
btn = new Button(this);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
...
setContentView(rl);

Categories

Resources