Creating LinearLayout Programmatically/Dynamically with Multiple Views - java

I have a hierarchy that is like this:
LinearLayout(horizontal)
ImageView
LinearLayout(vertical)
TextView
TextView
TextView
TextView
I want to be able to add the hierarchy above through iteration as long as there is data that could be obtained from the database(using Parse)
I have tried putting up the ImageView and LinearLayout under the parent LinearLayout but it doesn't seem to work. Here is my code in MainActivity.Java:
LinearLayout LL_Outer = (LinearLayout) findViewById(R.id.new_linearLayoutOuter);
LL_Outer.setOrientation(LinearLayout.VERTICAL); // set orientation
LL_Outer.setBackgroundColor(color.white); // set background
// set Layout_Width and Layout_Height
LinearLayout.LayoutParams layoutForOuter = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
LL_Outer.setLayoutParams(layoutForOuter);
LinearLayout LL_Inner = (LinearLayout) findViewById(R.id.new_linearLayoutInner);
LL_Inner.setOrientation(LinearLayout.HORIZONTAL);
LL_Inner.setBackgroundColor(color.white);
LinearLayout.LayoutParams layoutForInner = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LL_Inner.setLayoutParams(layoutForInner);
//LL_Outer.addView(LL_Inner);
ImageView IV = (ImageView) findViewById(R.id.new_imageViewPP);
//IV.getLayoutParams().height = 55;
//IV.getLayoutParams().width = 55;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(14, 12, 0, 0);
params.height = 55;
params.weight = 55;
IV.setBackgroundColor(color.black);
IV.setLayoutParams(params);
LL_Inner.addView(IV);
LL_Outer.addView(LL_Inner);
I don't know where I went wrong as my code did not prompt any error. Please help.
EDIT: I have edited the Orientations accordingly and when I run the app, it stops working. And prompts an error in LogCat saying "The specified child already has a parent. You must call removeView() on the child's parent first.
Here's my XML:
<LinearLayout
android:id="#+id/new_linearLayoutOuter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal" >
<ImageView
android:id="#+id/new_imageViewPP"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginLeft="14dp"
android:layout_marginTop="12dp"
android:background="#color/black"
android:contentDescription="#string/pp" />
<LinearLayout
android:id="#+id/new_linearLayoutInner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical" >
<TextView
android:id="#+id/new_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/movie_title"
android:paddingTop="10dp"
android:paddingLeft="7dp"
android:textSize="15sp" /> <!-- Title of the movie -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/review_by"
android:paddingTop="3dp"
android:paddingLeft="7dp"
android:textSize="12sp" /> <!-- By -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/movie_stars"
android:paddingTop="3dp"
android:paddingLeft="7dp"
android:textSize="12sp" /> <!-- Rating and date -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sample_string"
android:maxLines="5"
android:scrollbars="vertical"
android:paddingTop="10dp"
android:paddingLeft="7dp"
android:textSize="12sp"
android:layout_gravity="center_vertical|right" /> <!-- Review content -->
</LinearLayout>
</LinearLayout>

You want that hierarchy programmatically.
- LinearLayout(horizontal)
- ImageView
- LinearLayout(vertical)
- TextView
- TextView
- TextView
- TextView
Ok lets start with Parent LinearLayout
LinearLayout parent = new LinearLayout(context);
parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
parent.setOrientation(LinearLayout.HORIZONTAL);
//children of parent linearlayout
ImageView iv = new ImageView(context);
LinearLayout layout2 = new LinearLayout(context);
layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
parent.addView(iv);
parent.addView(layout2);
//children of layout2 LinearLayout
TextView tv1 = new TextView(context);
TextView tv2 = new TextView(context);
TextView tv3 = new TextView(context);
TextView tv4 = new TextView(context);
layout2.addView(tv1);
layout2.addView(tv2);
layout2.addView(tv3);
layout2.addView(tv4);
And you are done :)

The problem is because the views are already added to the layout in the XML file. Then you findViewById (find them) and try to add them to the layout again. That is why the app crashes complaining that, view already has a parent and you can't add it again.

In the XML File LinearLayout already has child view. So there is not need to add them in code.

i suggest you to remove the xml file and just use full code on the java side. you can add the views programatically from the java side. this one from xtreemdeveloper but i change few line for the parent layout.
// remove this params set it up below
parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// change the code above into
.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
addContentView(parent,layoutParams);
// end of my change
it will look like this in full code =
LinearLayout parent = new LinearLayout(context);
// remove this params set it up below
parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// change the code above into
.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
addContentView(parent,layoutParams);
// end of my change
parent.setOrientation(LinearLayout.HORIZONTAL);
//children of parent linearlayout
ImageView iv = new ImageView(context);
LinearLayout layout2 = new LinearLayout(context);
layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
parent.addView(iv);
parent.addView(layout2);
//children of layout2 LinearLayout
TextView tv1 = new TextView(context);
TextView tv2 = new TextView(context);
TextView tv3 = new TextView(context);
TextView tv4 = new TextView(context);
layout2.addView(tv1);
layout2.addView(tv2);
layout2.addView(tv3);
layout2.addView(tv4);

Related

How to add relative Layout below some element dynamically

Please help me to add a RelativeLayout below a RadioButton dynamically (I have to create RelativeLayout and RadioButton in class).
Something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioButton3"
android:text="RadioButton" />
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_below="#+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
To View I can write:
myLayout.addView(myView, layoutParams);
But how about ViewGroup?
I am not sure whether I understand u. Please check it and give me feedback. Thank you!
LinearLayout mLayout = (LinearLayout) findViewById(R.id.myLayout);
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
RadioButton mRadioButton = new RadioButton(this);
mRadioButton.setLayoutParams(p);
mLayout.addView(mRadioButton);
p = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
RelativeLayout mRelativeLayout = new RelativeLayout(this);
mRelativeLayout.setLayoutParams(p);
mLayout.addView(mRelativeLayout);

Align TextView above EditText

I want to align a TextView above an EditText. The following code is working well when the TextView is not higher than the space between the Top Margin of the window (red line) and the EditText (green line), like in the screenshot.
The problem occurs when the TextView has more lines than in the screenshot: it just "overruns" the EditText, but I want to keep the EditText in foreground.
In other words: I would like to place the TextView's bottom margin onto the green line and let it grow towards the red line, in order to maintain the visibility of the EditText.
// LINEAR LAYOUT
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
// TEXTVIEW
// how to set bottom margins programmatically?
layout.addView(tv);
// EDITTEXT
// place the EditText to the bottom of the layout, working well
et.setGravity(Gravity.BOTTOM);
et.setInputType(InputType.TYPE_CLASS_TEXT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.BOTTOM;
et.setLayoutParams(params);
layout.addView(et);
This should do it for you:
// LINEAR LAYOUT
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
// TEXTVIEW
LinearLayout.LayoutParams paramstv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1f);
tv.setLayoutParams(paramstv);
layout.addView(tv);
// EDITTEXT
et.setGravity(Gravity.BOTTOM);
et.setInputType(InputType.TYPE_CLASS_TEXT);
LinearLayout.LayoutParams etparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,8f);
etparams.gravity = Gravity.BOTTOM;
et.setLayoutParams(etparams);
layout.addView(et);
I guess the key is really to wrap both the EditText and TextView into LayoutParams. Probably you have to adjust the weights.
If this one doesn't work, try to create your layout with an XML file (the handling there is easier).
Try using the relative layout instead and set the edittext at the parent's bottom..
In case you don't want to switch to relative layout then try declaring edit text before the text view..
You can set the property of EditText type to textMultiline.
As you're using LinearLayout , set weight for your views
LinearLayout.LayoutParams paramTextView = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
tv.setlayoutParams(paramTextView);
LinearLayout.LayoutParams paramEditText = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 3.0f);
et.setlayoutParams(paramEditText);
This shall give you the result you expect
This below xml code can solve your problem...
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<SrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_above="#+id/edittext_id"
android:layout_margin="7dp">
<TextView
android:id="#+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</SrollView>
<EditText
android:id="#+id/edittext_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>

How to place a TextView next to a button

I have a MainActivity initially empty. When I click on the "plus" button, you open a DialogFragment where there are two buttons("cancel" and "continue") and a EditText. In EditText you can write the name of the project (project 1 in this case) and by clicking "continue" you can create at run time the "Project 1", a "play" button and a TextView "hello" placed as shown in the image below.
I would like the TextView "hello" was positioned next to the "play" button on the same line.
Here is my code:
Method of the button "continue":
#Override
public void onDialogPositiveClick(DialogFragment dialog) {
EditText editText = (EditText) dialog.getDialog().findViewById(R.id.project_name);
String projectName = editText.getText().toString();
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp1.gravity = Gravity.END;
Button projectButton = new Button(this);
projectButton.setText(projectName);
projectButton.setLayoutParams(lp1);
projectButton.setOnClickListener(projectListener);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp2.gravity = Gravity.START;
lp2.setMarginStart(10);
Button playButton = new Button(this);
playButton.setBackgroundResource(R.drawable.play_button_green);
playButton.setLayoutParams(lp2);
TextView buttonView = new TextView(this);
buttonView.setText("Hello");
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row);
linearLayout.addView(projectButton);
linearLayout.addView(playButton);
linearLayout.addView(buttonView);
}
MainActivity XML layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/button_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" >
</LinearLayout>
</RelativeLayout>
The whole layout must be created dynamically and then using java code not XML.
Anyone have any ideas?
Put Button and TextView inside LinearLayout with orientation="horizontal"
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(playButton);
ll.addView(buttonView);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row);
linearLayout.addView(projectButton);
linearLayout.addView(ll);
by using horizontal android:orientation
<?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="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textView"
android:layout_weight="1"/>
</LinearLayout>
Rather than using Button and Text View you can use a single button also.
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
Button playButton = new Button(this);
playButton.setText("Hello");
playButton.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
playButton.setLayoutParams(lp2);
now you will get a button with text and icon on its left.

Android Table Cells Border

This xml code below gives me a proper border around my table cells
<TableRow android:background="#cdcdcd" >
<TextView android:text="1st Column" android:background="#ffffff" android:layout_margin="2dip"/>
<TextView android:text="2nd Column" android:background="#ffffff" android:layout_margin="2dip"/>
<TextView android:id="#+id/amount" android:background="#ffffff" android:layout_margin="2dip" android:text="3rd col"/>
</TableRow>
But when i try to do it programmatically it is doesn't work here is my code:
dataTable = (TableLayout)findViewById(R.id.data_table);
TableRow tr=new TableRow(this);
counter++;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.setMargins(2,2, 2, 2);
TextView tv= new TextView(this);
tv.setLayoutParams(lp);
tv.setText("text"+counter);
tv.setBackgroundColor(Color.parseColor("#ffffff"));
counter++;
TextView cb= new TextView(this);
cb.setLayoutParams(lp);
cb.setText("text"+counter);
counter++;
TextView ib= new TextView(this);
ib.setText("text"+counter);
ib.setLayoutParams(lp);
tr.setBackgroundColor(Color.parseColor("#cdcdcd"));
tr.setPadding(2, 2, 2, 2);
tr.addView(tv);
tr.addView(cb);
tr.addView(ib);
dataTable.addView(tr,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Every thing works except for the borders and when i set "tv.setLayoutParams(lp);" the parameters the column disappears.
Try to add the view by calling
addView(View child, int index, ViewGroup.LayoutParams params)
Also, try to create a new LayoutParams instance for each view

Change ListView position dynamically

I'm doing this Application where I dynamically change a ListView Size like in the following code:
RelativeLayout.LayoutParams mParam = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
sucs.setLayoutParams(myListView);
But when I do this, the ListView appears on top of the view. Is there any way where I can place it below the textview where it is supposed to be?
Thanks in advance!
EDITED:
HereĀ“s my xml:
<TextView
android:id="#+id/sucursales_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/direccion_empresa"
android:background="#drawable/barra"
android:text="#string/sucursales"
android:visibility="gone" />
<ListView
android:id="#+id/sucursalesEmpresaList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/sucursales_empresa"
android:cacheColorHint="#00000000"
android:visibility="gone" />
You have to add rule for listview LayoutParams like this..
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.BELOW, R.id.textview);
listview.setLayoutParams(params1);
Try this
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, view.getId());
view.setLayoutParams(params);
ViewGroup parentView = (ViewGroup) findViewById(R.id.viewgroup);
parentView.addView(mylsitview);

Categories

Resources