Android RelativeLayout below another RelativeLayout dynamically - java

I'm trying to dynamically build relative layouts consisting of several images.
Theses relative layouts will be display below/rigth of previous relative layouts.
I'm starting with two relative layouts (rl100 and rl200). rl200 is below rl100.
But rl200 isn't below rl100, is "stacked" over rl100.
Can you help me ?
My code :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
RelativeLayout rL100 = rlAdd(rLayout,100,-1,-1);
ImageButton imgBtn101 = imgBtnAdd(rL100,101,R.drawable.apricot,-1,-1);
ImageButton imgBtn102 = imgBtnAdd(rL100,102,R.drawable.banana,-1,101);
ImageButton imgBtn103 = imgBtnAdd(rL100,103,R.drawable.cherry,101,-1);
ImageButton imgBtn104 = imgBtnAdd(rL100,104,R.drawable.strawberry,102,103);
RelativeLayout rL200 = rlAdd(rLayout,200,100,-1);
ImageButton imgBtn201 = imgBtnAdd(rL100,201,R.drawable.pineapple,-1,-1);
ImageButton imgBtn202 = imgBtnAdd(rL100,202,R.drawable.pineapple,-1,201);
ImageButton imgBtn203 = imgBtnAdd(rL100,203,R.drawable.pineapple,201,-1);
ImageButton imgBtn204 = imgBtnAdd(rL100,204,R.drawable.pineapple,202,203);
}
private RelativeLayout rlAdd(RelativeLayout parentContainer, int nId, int nIdBelow,
int nIdRightOf) {
RelativeLayout rLayout = new RelativeLayout(this);
rLayout.setId(nId);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (nIdBelow != -1) {
rlp.addRule(RelativeLayout.BELOW, nIdBelow);
}
if (nIdRightOf != -1) {
rlp.addRule(RelativeLayout.RIGHT_OF, nIdRightOf);
}
rLayout.setLayoutParams(rlp);
parentContainer.addView(rLayout);
return rLayout;
}
private ImageButton imgBtnAdd(RelativeLayout ParentContainer, int ImgId, int ResDrawable,
int imgIdBelow, int imgIdRightOf) {
ImageButton imgBtn = new ImageButton(this);
imgBtn.setId(ImgId);
imgBtn.setImageResource(ResDrawable);
ParentContainer.addView(imgBtn);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imgBtn.getLayoutParams();
if (imgIdBelow != -1) {
lp.addRule(RelativeLayout.BELOW, imgIdBelow);
}
if (imgIdRightOf != -1) {
lp.addRule(RelativeLayout.RIGHT_OF, imgIdRightOf);
}
imgBtn.setLayoutParams(lp);
return imgBtn;
}
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<RelativeLayout android:id="#+id/rlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
</RelativeLayout>
</ScrollView>

You are doing wrong over here
RelativeLayout rL200 = rlAdd(rLayout,100,-1,-1);
// From method
if (nIdBelow != -1) {
rlp.addRule(RelativeLayout.BELOW, nIdBelow);
}
As you are not passing id in nIdBelow field how it is supposed to add layout below. pass above image id or use Linear Layout with vertical orientation.

You should use this line of code to add the relative layout with ID 200 below the relative layout with id 100, am I right?
RelativeLayout rL200 = rlAdd(rLayout, 200, 100, -1);

Thanks for your help.
I make differents errors :
RelativeLayout rL200...
RelativeLayout rL200 = rlAdd(rLayout, 200, 100, -1);
Correct version :
RelativeLayout rL200 = rlAdd(rLayout,200,100,-1);
ImageButton imgBtn20x
ImageButton imgBtn201 = imgBtnAdd(rL100,201,R.drawable.pineapple,-1,-1);
Correct version :
ImageButton imgBtn201 = imgBtnAdd(rL200,201,R.drawable.pineapple,-1,-1);
Same error for imgBtn202 ... imgBtn204
activity_main.xml
Errors 1 and 2 are stunning errors.
Third error in RelativeLayout android:id="#+id/rlayout" ...
Correct version :
<RelativeLayout android:id="#+id/rlayout" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="match_parent">
</RelativeLayout>

Related

Error "The specified child already has a parent" after LinearLayout.addView() and then Linearlayout.removeView()

I'm working on a project where I have a parent LinearLayout with id=linear_parent created on activity_main.xml. Inside this parent layout, n numbers of LinearLayouts are created programmatically base on user input at EditText (id=no_of_views_input).
And inside the each of these Linearlayouts, there are more child views (imageView, edit, button) created programmatically.
Suppose a user enters 4 no_of_views_input, the views heirarchy would be something like,
============
linear_parent
linearLayout //position 1
imageView
edit
button
linearLayout //position 2
imageView
edit
button
linearLayout //position 3
imageView
edit
button
linearLayout //position 4
imageView
edit
button
===========
After these layouts and views are created, I want to rearrange the LinearLayouts, base on user input from child id=edit.
Suppose the user enters 1 on edit at LinearLayout position 3 , what I want is, the current LinearLayout at position 3 must be added to position 1 along with its children, and finally remove the old Linearlayout which was at position 3. (would be currently at position 4 if indexes are shifted down).
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linear_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="vertical">
<!-- Views are added here programmatically -->
</LinearLayout>
<EditText
android:id="#+id/no_of_views_input"
android:layout_width="100dp"
android:layout_height="40dp"
android:hint="Number of view to add"/>
<Button
android:id="#+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout linear_parent;
Button add_button;
EditText no_of_views_input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linear_parent = findViewById(R.id.linear_parent);
add_button= findViewById(R.id.add_button);
no_of_views_input= findViewById(R.id.no_of_views_input);
no_of_views_input.setInputType(InputType.TYPE_CLASS_NUMBER);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!no_of_views_input.getText().toString().isEmpty()) {
String no_of_views_temp = no_of_views_input.getText().toString();
int no_of_views = Integer.parseInt(no_of_views_temp);
for (int i = 0; i < no_of_views; i++) {
LinearLayout linearLayout = new LinearLayout(MainActivity.this);
LinearLayout.LayoutParams lp_ll = new LinearLayout.LayoutParams(600, 1500);
lp_ll.setMargins(0, 50, 0, 0);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setId(View.generateViewId()); // id = 1, 2, 3, 4, ...
int linearLayoutID = linearLayout.getId();
linearLayout.setLayoutParams(lp_ll);
ImageView imageView = new ImageView(MainActivity.this);
LinearLayout.LayoutParams lp_image = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 800);
imageView.setBackgroundColor(Color.Yellow);
imageView.setLayoutParams(lp_image);
EditText edit = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp_edit = new LinearLayout.LayoutParams(300, ViewGroup.LayoutParams.WRAP_CONTENT);
edit.setBackgroundColor(Color.WHITE);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setHint("move to index");
edit.setLayoutParams(lp_edit);
Button button = new Button(MainActivity.this);
LinearLayout.LayoutParams lp_button= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button.setBackgroundColor(Color.BLUE);
button.setText("Move");
button.setLayoutParams(lp_button);
linear_parent.addView(linearLayout);
linearLayout.addView(imageView);
linearLayout.addView(edit);
linearLayout.addView(button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String indexTarget_temp = edit.getText().toString();
int indexTarget = Integer.parseInt(indexTarget_temp );
int childCount = linear_parent.getChildCount();
int currentLinearLayoutPos = linear_parent.indexOfChild(linearLayout);
try {
linear_parent.addView(linearLayout, indexTarget - 1); //adding current linearLayout to indexTarget
//error occurs this line
linear_parent.removeView(linear_parent.getChildAt(currentLinearLayoutPos + 1)); //removing linearLayout at old index
}
catch(IllegalStateException e) {
e.printStackTrace();
}
}
});
}
}else {
Toast.makeText(MainActivity.this, "Enter index no", Toast.LENGTH_SHORT).show();
}
}
});
}
}
The error I'm getting is
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Any help would be highly appreciated.
Try to remove the child from its parent before adding it as a subview:
try {
if (linearLayout.getParent() != null) {
// Remove child from parent
((ViewGroup) linearLayout.getParent()).removeView(linearLayout)
}
linear_parent.addView(linearLayout, indexTarget - 1);
}
catch(IllegalStateException e) {
e.printStackTrace();
}
However, consider switching to a RecyclerView.
It would simplify your code structure.

How add array of TextViews into Relative layout without overlapping programmatically in android?

I'm looking for a solution to solve my problem which all my TextViews overlaps on themselves, when are added to Relative Layout. In fact, I need to do put them after each. other I've read existed answers, I followed them but nothing could solve it yet. can someone tell me where I did wrongly?
here is my code:
for (int i=0;i<parts.length;i++)
{
valueTV[i] = new TextView(this);
valueTV[i].setText(parts[i]);
valueTV[i].setId(i);
valueTV[i].setWidth(300);
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
linearLayout_Skills.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
linearLayout_Skills.setBackgroundColor(getResources().getColor(R.color.blue));
if(i>=1)
{
lparams.addRule(RelativeLayout.END_OF, valueTV[i-1].getId());
valueTV[i].setLayoutParams(lparams);
}else {
lparams.addRule(RelativeLayout.ALIGN_PARENT_START);
valueTV[i].setLayoutParams(lparams);
}
linearLayout_Skills.addView(valueTV[i]);
}
XML code:
<RelativeLayout
android:id="#+id/linearSkills"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
android:paddingTop="5dp"
</RelativeLayout>
You can achieve this using Relative layout
So this is the main idea , first you define your relation layout
//layout variable is your relative layout
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
Then you define a param variable like this
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
After that you define your textView with an id (in your case this id can be its position in the array)
TextView tv1 = new TextView(this);
tv1.setId(1);
tv1.setText("textView1");
The next textView will be declared like this
TextView tv2 = new TextView(this);
params1.addRule(RelativeLayout.BELOW, tv1.getId());
tv2.setId(2);
tv2.setText("textView2");
Finally you set your view using the params you defined
layout.addView(tv2, params1);
Here is a complete example you can check answer by #AndiM

How to set one view in vertical linearlayout in top and another in bottom programmatically?

I'm trying to create programmatically parent LinearLayout
and 3 TextViews.
One TextView must be aligned in top left, the second one in the center of parent
and the third one to the right and bottom of my screen. Everything must be done in code.
I'm almost done, but there is still some problem.
All my 3 views are on the top
My code:
public class ActivityFour extends AppCompatActivity {
private LinearLayout mLinearLayout;
private TextView tv1;
private TextView tv2;
private TextView tv3;
private static final int TV_ID1 = 101;
private static final int TV_ID2 = 102;
private static final int TV_ID3 = 103;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
ViewGroup.LayoutParams llParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
setContentView(mLinearLayout, llParams);
LinearLayout.LayoutParams linlayout_params1 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linlayout_params2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linlayout_params3 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linlayout_params1.setMargins(16,16,16,16);
tv1 = new TextView(this);
tv1.setId(TV_ID1);
linlayout_params1.gravity = Gravity.START;
linlayout_params1.gravity = Gravity.TOP;
mLinearLayout.addView(tv1, linlayout_params1);
tv1.setText("TextView number ONE");
linlayout_params2.setMargins(16,16,16,16);
tv2 = new TextView(this);
tv2.setId(TV_ID2);
linlayout_params2.gravity = Gravity.CENTER;
mLinearLayout.addView(tv2, linlayout_params2);
tv2.setText("TextView number TWO");
linlayout_params3.setMargins(16,16,16,16);
tv3 = new TextView(this);
tv3.setId(TV_ID3);
linlayout_params3.gravity = Gravity.END;
mLinearLayout.addView(tv3, linlayout_params3);
tv3.setText("TextView number THREE");
}
}
After adding the weight property to all view.params it looks like this
In order to make the LinearLayout fill all the available space, you need to set the android:layout_weight attribute on the middle child View. You can do it programmatically by using a Constructor which takes the weight as third parameter:
LinearLayout.LayoutParams linlayout_params2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
First of all no need to create liner layout for all textview you can do like this:
in xml file :
<?xml version="1.0" encoding="utf-8"?>
<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="vertical"
android:id="#+id/test"
tools:context=".activity.TestActivity"/>
and in activity file :
LinearLayout layout = findViewById(R.id.test);
TextView product_1 = new TextView(this);
product_1.setText("Product 1");
product_1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
layout.addView(product_1);
TextView product_2 = new TextView(this);
product_2.setText("Product 2");
product_2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
product_2.setGravity(Gravity.CENTER);
layout.addView(product_2);
TextView product_3 = new TextView(this);
product_3.setText("Product 3");
product_3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
product_3.setGravity(Gravity.END|Gravity.BOTTOM);
layout.addView(product_3);

Incremental Opacity, wanting constant opacity Image View with Drawable

I am programmatically creating textviews with horizontal lines between each view. Using a drawable created programmatically.
The problem is, the opacity starts off light and gradually increases for each line.
I've logged the opacity (getAlpha()) of the drawable, paint, image view and linear layout at all the points in the two methods provided and from the drawables it's always 255 and the views 1.0. I don't understand why it's not behaving as though this is true. I've also tried setting Alpha, it makes no difference.
Why is it doing this and how do I fix it?
xml:
<LinearLayout android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" .../>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="PaintDashedLines"
android:text="Press Me"/>
</LinearLayout>
java:
static int tvCount = 0;
public void PaintDashedLines(View v) {
LinearLayout ll = (LinearLayout) findViewById(R.id.main);
TextView tv = new TextView(MainActivity.this);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(25);
tv.setPadding(0, 5, 0, 5);
ll.addView(tv);
tv.setText("TextView " + tvCount);
ImageView divider = new ImageView(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ll.getWidth(), 2);
lp.setMargins(0, 5, 0, 5);
divider.setLayoutParams(lp);
divider.setBackground(CreateDashedLined());
ll.addView(divider);
tvCount++;
}
public static Drawable CreateDashedLined() {
ShapeDrawable sd = new ShapeDrawable(new RectShape());
Paint fgPaintSel = sd.getPaint();
fgPaintSel.setColor(Color.BLACK);
fgPaintSel.setStyle(Paint.Style.STROKE);
fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));
return sd;
}
To me looks like more some issue with the emulator. You could also try disable the hardware acceleration with
ll.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Please, keep also in mind that you don't need to instantiate a new ImageView every time, just to draw a divider between the TextViews. You could use setDivider. E.g.
In your onCreate
ShapeDrawable sd = new ShapeDrawable(new RectShape());
sd.setIntrinsicHeight(1);
Paint fgPaintSel = sd.getPaint();
fgPaintSel.setARGB(255, 0, 0, 0);
fgPaintSel.setStyle(Paint.Style.STROKE);
fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main);
linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE | LinearLayout.SHOW_DIVIDER_END);
linearLayout.setDividerDrawable(sd);
and when you press the button
public void pressMe(View view) {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main);
TextView tv = new TextView(MainActivity.this);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(25);
tv.setPadding(0, 5, 0, 5);
tv.setText("TextView " + linearLayout.getChildCount());
linearLayout.addView(tv);
}
the result is

How to set left margin for RelativeLayout Programmatically Android

I am trying to set the left margin for first RelativeLayout, and
I have this xml code:
<?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:id="#+id/market_layout"
android:orientation="horizontal">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/horizontalScrollView"
android:layout_gravity="center_horizontal"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/market_linear_layout_in_hsv">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
In this java code insert in LinearLayout with id = market_linear_layout_in_hsv inserting RelativeLayout including some view (Button, TextView, ImageView):
market_ll = (LinearLayout)findViewById(R.id.market_linear_layout_in_hsv);
RelativeLayout.LayoutParams TVParams = new RelativeLayout.LayoutParams(RLwrapContent, RLwrapContent );
RelativeLayout.LayoutParams IVParams = new RelativeLayout.LayoutParams(250, 400);
RelativeLayout.LayoutParams BParams = new RelativeLayout.LayoutParams(RLwrapContent, RLwrapContent);
RelativeLayout.LayoutParams RLParams = new RelativeLayout.LayoutParams(RLMatchParent, RLMatchParent);
TVParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
TVParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
IVParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
IVParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
BParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
BParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
if (folder.exists()) {
String[] files = folder.list();
relationsIdMarket = new ArrayList<RelationsIdMarket>(files.length);
for (int i = 0; i < files.length; i++){
RLParams.setMargins(i == 0?150:0, 0, 0, 0);//here i set on first iteration left margin 150
RelationsIdMarket rim = new RelationsIdMarket();
rim.idProduct = i;
rim.idTextView = ViewIdGenerator.generateViewId();
rim.idImageView = ViewIdGenerator.generateViewId();
rim.idButton = ViewIdGenerator.generateViewId();
TextView tv = new TextView(this);
tv.setText("name");
tv.setId(rim.idTextView);
ImageView iv = new ImageView(this);
if(GeneralData.images.size() == i)
GeneralData.images.add(BitmapFactory.decodeFile(pathToImg+"/"+files[i]));
iv.setImageBitmap(GeneralData.images.get(i));
iv.setId(rim.idImageView);
iv.setImageBitmap(rp.bmp);
Button btn = new Button(this);
btn.setText("open");
btn.setOnClickListener(this);
btn.setId(rim.idButton);
RelativeLayout rl = new RelativeLayout(this);
rl.addView(tv, TVParams);
rl.addView(iv, IVParams);
rl.addView(btn, BParams);
rl.setPadding(50, 0, 50, 0);
market_ll.addView(rl, RLParams);
relationsIdMarket.add(rim);
}
}
But I haven't been able to get this working. Any help is appreciated.
I am corrected my code as follows:
ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(RLMatchParent, RLMatchParent);
if (folder.exists()) {
String[] files = folder.list();
relationsIdMarket = new ArrayList<RelationsIdMarket>(files.length);
for (int i = 0; i < files.length; i++){
mlp.leftMargin = i==0?100:0;
RelationsIdMarket rim = new RelationsIdMarket();
rim.idProduct = i;
rim.idTextView = ViewIdGenerator.generateViewId();
rim.idImageView = ViewIdGenerator.generateViewId();
rim.idButton = ViewIdGenerator.generateViewId();
TextView tv = new TextView(this);
tv.setText("Название");
tv.setId(rim.idTextView);
ImageView iv = new ImageView(this);
if(GeneralData.images.size() == i)
GeneralData.images.add(BitmapFactory.decodeFile(pathToImg+"/"+files[i]));
iv.setImageBitmap(GeneralData.images.get(i));
iv.setId(rim.idImageView);
iv.setImageBitmap(rp.bmp);
Button btn = new Button(this);
btn.setText("Открыть");
btn.setOnClickListener(this);
btn.setId(rim.idButton);
RelativeLayout rl = new RelativeLayout(this);
rl.addView(tv, TVParams);
rl.addView(iv, IVParams);
rl.addView(btn, BParams);
rl.setPadding(50, 0, 50, 0);
market_ll.addView(rl, mlp);
relationsIdMarket.add(rim);
}
}
But The problem is not solved.
Use android:layout_marginLeft within your first linear layout.
Android RelativeLayout inherits from android.view.ViewGroup.MarginLayoutParams which provides methods to set margins for layouts.
Ref Source

Categories