I am trying to achieve a layout dynamically which is given below:
I can manage to done all things dynamically. But I can't align item name (Chicken Masala) to right of the ImageView . I am reached at this position as following.
RelativeLayout primary_layout = new RelativeLayout(this);
LayoutParams layoutParam = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
primary_layout.setLayoutParams(layoutParam);
// primary_layout.setOrientation(LinearLayout.HORIZONTAL);
// primary_layout.setBackgroundColor(0xff99ccff);
//String cross = " � ";
String makeString = aOrder.getQuantity() + " "
+ aOrder.getFoodName();
ImageView imageView_remove = createAImageview(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.CENTER_VERTICAL,
10, 20);
TextView item_name = createATextViewWithParam(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_TOP, imageView_remove.getId(),
makeString, 20, 10, 20);
TextView txt_item_price = createATextView(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_PARENT_RIGHT,
"" + item_price, 20, 10, 20);
primary_layout.addView(imageView_remove);
primary_layout.addView(item_name);
primary_layout.addView(txt_item_price);
I am share Two methods createAImageview() & createATextViewWithParam() which is necessary for this Layout.
public ImageView createAImageview(int layout_width, int layout_height, int align,
int margin, int padding) {
ImageView imageView = new ImageView(this);
RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams(
layout_width, layout_height);
_params.setMargins(margin, margin, margin, margin);
_params.addRule(align);
imageView.setLayoutParams(_params);
imageView.setPadding(padding, padding, padding, padding);
imageView.setImageResource(R.mipmap.remove);
return imageView;
}
public TextView createATextViewWithParam(int layout_widh, int layout_height, int align, int align_id,
String text, int fontSize, int margin, int padding) {
TextView textView_item_name = new TextView(this);
// LayoutParams layoutParams = new LayoutParams(
// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// layoutParams.gravity = Gravity.LEFT;
RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams(
layout_widh, layout_height);
_params.setMargins(margin, margin, margin, margin);
_params.addRule(align, align_id);
textView_item_name.setLayoutParams(_params);
textView_item_name.setText(text);
textView_item_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
textView_item_name.setTextColor(Color.parseColor("#000000"));
// textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView_item_name.setPadding(padding, padding, padding, padding);
return textView_item_name;
}
You need to add rule for LEFT_OF/RIGHT_OF :
_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
_params.addRule(RelativeLayout.LEFT_OF, R.id.id_of_textview);
imageView.setLayoutParams(_params);
Try this,
activity_main.xml
<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="com.example.dynamicviewdemo.MainActivity" >
<RelativeLayout
android:id="#+id/llAddMember"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" />
</RelativeLayout>
MainActivity.java
package com.example.dynamicviewdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
private RelativeLayout llAddMember;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llAddMember = (RelativeLayout) findViewById(R.id.llAddMember);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
final RelativeLayout linearLayout = new RelativeLayout(getApplicationContext());
linearLayout.setLayoutParams(layoutParams);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView imgView = new ImageView(MainActivity.this);
imgView.setImageResource(R.drawable.ic_launcher);
imgView.setLayoutParams(lparams);
linearLayout.addView(imgView);
lparams.setMargins(0, 15, 0, 0);
LayoutParams lparams1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView txtResGender = new TextView(MainActivity.this);
txtResGender.setLayoutParams(lparams1);
txtResGender.setText("Hello World");
txtResGender.setTextSize(14);
txtResGender.setTextColor(Color.parseColor("#9C9C9C"));
linearLayout.addView(txtResGender);
lparams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lparams1.setMargins(0, 30, 15, 0);
llAddMember.addView(linearLayout);
}
}
Related
I'm creating a ScrollView, nesting in a RelativeLayout (which contains tables). Everything is properly added to the layout. The problem is: vertically scrolling works fine, but horizontal scrolling does NOT work at all. I already tested various combinations of MATCH_PARENT, WRAP_CONTENT and FILL_PARENT. No try even worked close. So my question is: What am I missing here?
Here is the code:
public class PlayerView extends View {
private RelativeLayout relativeLayout;
private TableLayout tableLayout;
private int player_loop;
private int player_count;
public PlayerView(Context context, int player_count){
super(context);
setPlayer_count(player_count);
}
public ScrollView create_scrollView(){
ScrollView scrollView = new ScrollView(getContext());
ScrollView.LayoutParams scroll_params = new ScrollView.LayoutParams(
ScrollView.LayoutParams.MATCH_PARENT,
ScrollView.LayoutParams.MATCH_PARENT
);
scrollView.setLayoutParams(scroll_params);
scrollView.addView(create_relativeLayout());
return scrollView;
}
public RelativeLayout create_relativeLayout(){
relativeLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(relativeParams);
create_Player_Table_Layout();
return relativeLayout;
}
public void create_Player_Table_Layout(){
for(player_loop = 1; player_loop <= player_count; player_loop++){
relativeLayout.addView(player_table(getResources().getString(R.string.dummy_player_name) + player_loop, player_loop));
}
}
public TableLayout player_table(String playername, int playernumber){
tableLayout = new TableLayout(getContext());
tableLayout.setId(playernumber * 1000);
if (playernumber > 1) {
//TABLE PLACEMENT
RelativeLayout.LayoutParams tbl_params_New = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tbl_params_New.addRule(RelativeLayout.RIGHT_OF, (playernumber - 1) * 1000);
tableLayout.setLayoutParams(tbl_params_New);
}
//Add Playername
TableRow row_playername = new TableRow(getContext());
TextView view_name = new TextView(getContext());
TableRow.LayoutParams view_name_params = new TableRow.LayoutParams();
view_name_params.setMargins(20,20,20,20);
view_name.setLayoutParams(view_name_params);
view_name.setGravity(Gravity.CENTER);
view_name.setText(playername);
view_name.setTextSize(20);
view_name.setId(playernumber * 100);
row_playername.addView(view_name);
tableLayout.addView(row_playername);
//Add Lifepoints
TableRow row_lifepoints = new TableRow(getContext());
TextView view_lifepoints = new TextView(getContext());
TableRow.LayoutParams view_lifepoints_params = new TableRow.LayoutParams();
view_lifepoints_params.setMargins(20, 0, 20, 20);
view_lifepoints.setText("40");
view_lifepoints.setTextSize(40);
view_lifepoints.setGravity(Gravity.CENTER);
view_lifepoints.setId(playernumber * 100 + 10);
view_lifepoints.setLayoutParams(view_lifepoints_params);
row_lifepoints.addView(view_lifepoints);
tableLayout.addView(row_lifepoints);
Log.d("Test", "Player count:" + player_count);
for(int opponent_loop = 1; opponent_loop <= player_count; opponent_loop++){
tableLayout.addView(commander_damage_from_player(player_loop, opponent_loop));
}
return tableLayout;
}
private TableRow commander_damage_from_player(int player_loop, int opponent_loop){
TableRow row = new TableRow(getContext());
TextView textView = new TextView(getContext());
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams();
layoutParams.setMargins(20, 0, 40, 20);
textView.setLayoutParams(layoutParams);
textView.setText("0 | " + getResources().getString(R.string.dummy_player_name)+ " " + opponent_loop);
textView.setTextSize(20);
textView.setId(player_loop + 100 + opponent_loop);
row.addView(textView);
return row;
}
private void setPlayer_count(int player_count){
this.player_count = player_count;
}
}
And for documentation the calling class:
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PlayerView playerView = new PlayerView(this, 8);
setContentView(playerView.create_scrollView());
}
}
The only way you can do this is by creating a horizontalscrollview to be the child of the scrollview. If you make the minor modification to your create_relativeLayout() to create a horizontalscroll view then it will work correctly. I have seen this happen in other examples.
public HorizontalScrollView create_relativeLayout(){
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());
relativeLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(relativeParams);
create_Player_Table_Layout();
horizontalScrollView.addView(relativeLayout);
return horizontalScrollView;
}
I'm adding 5 elements in my CardView (ImageView, TextView and 3 buttons). I'm trying to set the RelativeLayout.LayoutParams and also adding the alignments in addRule() but somehow not getting the expected output.
This is my code:
package bhadra.com.mca;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Dashboard extends AppCompatActivity {
Button button_add, button_reports, button_appointment, button_message;
Context context;
CardView cardview;
ImageView imageview;
RelativeLayout.LayoutParams layoutparams, textparams, imageparams,
buttonparams, buttonparams2, buttonparams3;
TextView textview;
LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
button_add = (Button)findViewById(R.id.btn_add);
context = getApplicationContext();
linearLayout = (LinearLayout)findViewById(R.id.layout_linear);
//cardview = (CardView)findViewById(R.id.card_patientdetails);
button_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
createCardView();
}
});
}
public void createCardView() {
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,160 , getResources().getDisplayMetrics());
int margintop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,30 , getResources().getDisplayMetrics());
int imglayout = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,60 , getResources().getDisplayMetrics());
int txtmargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,70 , getResources().getDisplayMetrics());
int drawpad = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,5 , getResources().getDisplayMetrics());
int cardpad = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,5 , getResources().getDisplayMetrics());
int cardpad2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10 , getResources().getDisplayMetrics());
int radius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,4 , getResources().getDisplayMetrics());
int elevation = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10 , getResources().getDisplayMetrics());
cardview = new CardView(context);
layoutparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, height);
layoutparams.setMargins(0,margintop,0,0);
imageparams = new RelativeLayout.LayoutParams(
imglayout, imglayout);
imageparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
textparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textparams.setMargins(0,txtmargin,0,0);
textparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
buttonparams2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonparams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
buttonparams2.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonparams3 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonparams3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
buttonparams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageview = new ImageView(context);
imageview.setLayoutParams(imageparams);
imageview.setImageResource(R.drawable.ic_account_circle_black_24px);
textview = new TextView(context);
textview.setLayoutParams(textparams);
textview.setText(R.string.patient_name);
textview.setTextSize(20);
textview.setTextColor(Color.BLACK);
button_reports = new Button(new ContextThemeWrapper(this, R.style.Widget_AppCompat_Button_Borderless), null, 0);
button_reports.setLayoutParams(buttonparams);
button_reports.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_assessment_black_24px, 0, 0, 0);
button_reports.setCompoundDrawablePadding(drawpad);
button_reports.setText(R.string.upload_reports);
button_reports.setTextSize(12);
button_appointment = new Button(new ContextThemeWrapper(this, R.style.Widget_AppCompat_Button_Borderless), null, 0);
button_appointment.setLayoutParams(buttonparams2);
button_appointment.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_today_black_24px, 0, 0, 0);
button_appointment.setCompoundDrawablePadding(drawpad);
button_appointment.setText(R.string.book_appointment);
button_appointment.setTextSize(12);
button_message = new Button(new ContextThemeWrapper(this, R.style.Widget_AppCompat_Button_Borderless), null, 0);
button_message.setLayoutParams(buttonparams3);
button_message.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_message_black_24px, 0, 0, 0);
button_message.setCompoundDrawablePadding(drawpad);
button_message.setText(R.string.message);
button_message.setTextSize(12);
cardview.setLayoutParams(layoutparams);
cardview.setRadius(radius);
cardview.setPadding(cardpad,cardpad2,cardpad, 0);
cardview.setMaxCardElevation(elevation);
cardview.addView(imageview);
cardview.addView(textview);
cardview.addView(button_reports);
cardview.addView(button_appointment);
cardview.addView(button_message);
linearLayout.addView(cardview);
}
}
This code is giving me the following output:
Output
Can someone please help me?
I am trying to create a custom view without xml
public class MyView extends ViewGroup {
public MyView (Context context) {
super(context);
setBackgroundColor(0xffff0000);
RelativeLayout base = new RelativeLayout(context);
RelativeLayout.LayoutParams rP = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
base.setBackgroundColor(0xff00ff00);
base.setLayoutParams(rP);
RelativeLayout.LayoutParams iP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView icon = new ImageView(context);
icon.setBackgroundResource(android.R.drawable.ic_menu_add);
icon.setLayoutParams(iP);
addView(icon);
addView(base);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
And use it in actitvy
MyView myview = new MyView(this);
WindowManager.LayoutParams baseParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
addContentView(myview, baseParams);
But the LayoutParams does not work, it display nothing
I must use this onLayout to make it display, and the base layout is not MATCH_PARENT and icon is not WRAP_CONTENT
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final View child = getChildAt(0);
final View child2 = getChildAt(1);
child.layout(0, 0, 300, 300);
child2.layout(0, 0, 300, 300);
}
I also tried to add icon to layout, but the app will crash
base.addView(icon);
Is it possible to create this layout without hardcode the size and position?
I checked RelativeLayout.LayoutParams, but I cannot find any method to set it centerInParent to true.
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_add"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Please give this code a try.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create parent RelativeLayout
RelativeLayout relParent = new RelativeLayout(this);
RelativeLayout.LayoutParams relParentParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
relParent.setLayoutParams(relParentParam);
// Create child ImageView
ImageView imgView = new ImageView(this);
RelativeLayout.LayoutParams imgViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
imgView.setLayoutParams(imgViewParams);
imgView.setImageResource(android.R.drawable.ic_menu_add);
imgViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
// Add child ImageView to parent RelativeLayout
relParent.addView(imgView);
// Set parent RelativeLayout to your screen
setContentView(relParent, relParentParam);
}
}
I have a Class that used in it below code
this.guageBack= (AbsoluteLayout) findViewById(R.id.gaugeFrame);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY);
gaugeNeedle.setLayoutParams(params);
AbsoluteLayout deprecated so When I want to use RelativeLayout I have not replcement for
AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY);
Because it accept two args like below
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight);
What should I do?
Just add this code instead of yours
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight);
params.topMargin = needleY;
params.leftMargin = needleX;
Do it like this
public class CodeLayout extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText("Test");
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// Setting the parameters on the TextView
tv.setLayoutParams(lp);
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(tv);
// Setting the RelativeLayout as our content view
setContentView(relativeLayout, rlp);
}
}
try this code,
AbsoluteLayout absoluteLayout = //get absolute layout
Button button = new Button();
AbsoluteLayout.LayoutParms params = absoluteLayout.generateDefaultLayoutParams();
params.x = 100;
params.y = 100;
absoluteLayout.addView(button, params);
I am trying to create a method where each time I click a button, I want the screen to display a Relative Layout box below one that is currently there or create one at the top if there isn't one there and I want it to keep creating them below from each button click. Currently it just displays the Relative Layout box at the top of the screen as it would expect to do.
Here is the code that I have so far and can someone please help me see what I am doing wrong and what I can do to fix this issue:
onCreate code:
Resources r = this.getResources();
dpMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, r.getDisplayMetrics()); //Changes pixels to dp of margins
dpContainerHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, r.getDisplayMetrics()); //Changes pixels to dp of container height
dpContainerPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics()); //Changes pixels to dp of container padding
layout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams textParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams progressParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams imageButtonParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams newLayoutButtonParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams containerParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
RelativeLayout.LayoutParams containerParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
layout.setPadding(dpMargin, dpMargin, dpMargin, dpMargin);
//Declaring new views
tv1 = new TextView(this);
tv2 = new TextView(this);
containerLayout1 = new RelativeLayout(this);
containerLayout2 = new RelativeLayout(this);
tv3 = new TextView(this);
tv4 = new TextView(this);
pb1 = new ProgressBar(this);
ib1 = new ImageButton(this);
newLayoutButton = new Button(this);
//Declaring views ID's
tv1.setId(1);
tv2.setId(2);
containerLayout1.setId(3);
containerLayout2.setId(4);
tv3.setId(5);
tv4.setId(6);
pb1.setId(7);
ib1.setId(8);
newLayoutButton.setId(9);
//Text view 1
textParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
textParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tv1.setText("TextView1");
//Text view 2
textParams2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textParams2.addRule(RelativeLayout.BELOW, tv1.getId());
textParams2.setMargins(0, 0, 0, dpMargin);
tv2.setText("TextView2");
//Container 1
containerParams1.addRule(RelativeLayout.ALIGN_LEFT, tv2.getId());
containerParams1.addRule(RelativeLayout.ALIGN_RIGHT, tv1.getId());
containerParams1.addRule(RelativeLayout.BELOW, tv2.getId());
containerParams1.setMargins(0, 0, 0, dpMargin);
containerLayout1.setPadding(dpContainerPadding, 0, dpContainerPadding, dpContainerPadding);
containerLayout1.setBackgroundResource(R.color.display_panels);
//Container 2
containerParams2.addRule(RelativeLayout.ALIGN_LEFT, containerLayout1.getId());
containerParams2.addRule(RelativeLayout.ALIGN_RIGHT, containerLayout1.getId());
containerParams2.addRule(RelativeLayout.BELOW, containerLayout1.getId());
containerParams2.setMargins(0, 0, 0, dpMargin);
containerLayout2.setBackgroundResource(R.color.display_panels);
//Text view 3
textParams3.addRule(RelativeLayout.ABOVE, tv4.getId());
textParams3.addRule(RelativeLayout.ALIGN_LEFT, pb1.getId());
tv3.setText("TextView3");
tv3.setTextAppearance(this, android.R.style.TextAppearance_Small); //Need to change text colour to white
tv3.setTextColor(getResources().getColor(R.color.text_colour));
//Text view 4
textParams4.addRule(RelativeLayout.ABOVE, pb1.getId());
textParams4.addRule(RelativeLayout.CENTER_HORIZONTAL);
tv4.setText("TextView4");
tv4.setTextAppearance(this, android.R.style.TextAppearance_Small); //Need to change text colour to white
tv4.setTextColor(getResources().getColor(R.color.text_colour));
//Progress bar 1
progressParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
progressParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
progressParams1.addRule(RelativeLayout.LEFT_OF, ib1.getId());
pb1.setProgress(40);
pb1.setMax(100);
//Image button 1
imageButtonParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageButtonParams1.addRule(RelativeLayout.BELOW, tv4.getId());
ib1.setBackgroundResource(R.color.display_panels);
ib1.setImageResource(R.drawable.ic_green_ok);
newLayoutButtonParams.addRule(RelativeLayout.ABOVE, tv2.getId());
newLayoutButton.setText("New Layout");
newLayoutButton.setOnClickListener(this);
layout.addView(tv1, textParams1);
layout.addView(tv2, textParams2);
layout.addView(newLayoutButton, newLayoutButtonParams);
containerLayout1.addView(tv3, textParams3);
containerLayout1.addView(tv4, textParams4);
containerLayout1.addView(pb1, progressParams1);
containerLayout1.addView(ib1, imageButtonParams1);
layout.addView(containerLayout1, containerParams1);
layout.addView(containerLayout2, containerParams2);
setContentView(layout, layoutParams);
createNewLayout method code:
public RelativeLayout createNewLayout(RelativeLayout newlayout){
newLayoutContainer = new RelativeLayout(this);
final RelativeLayout.LayoutParams newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutContainer.setLayoutParams(newLayoutParams);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
newLayoutParams.addRule(RelativeLayout.BELOW);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
return newLayoutContainer;
}
onClick code:
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==newLayoutButton){
layout.addView(createNewLayout(newLayoutContainer), 0);
}
}
EDIT
new method code:
public void createNewLayout(){
int currentId = 1;
for(int i = 1; i <= numberOfLayouts; i++){
newLayoutContainer = new RelativeLayout(this);
newLayoutContainer.setId(currentId);
if(currentId == 1){
newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, containerLayout2.getId());
newLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, containerLayout2.getId());
newLayoutParams.addRule(RelativeLayout.BELOW, containerLayout2.getId());
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
}
else{
newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, newLayoutContainer.getId());
newLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, newLayoutContainer.getId());
newLayoutParams.addRule(RelativeLayout.BELOW, currentId);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
}
newLayoutContainer.setLayoutParams(newLayoutParams);
layout.addView(newLayoutContainer);
currentId++;
}
}
You could use a global variable to store currentId assigned to layouts, and update it every time u add a layout.
int currentId = 10;
button click function
public void onClick(View v) {
if(v==newLayoutButton){
layout.addView(createNewLayout(newLayoutContainer,currentId), 0);
currentId++;
}
}
createNewLayout function will have one more attribute as currentId.
public RelativeLayout createNewLayout(RelativeLayout newlayout, int currentId){
newLayoutContainer = new RelativeLayout(this);
final RelativeLayout.LayoutParams newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
newLayoutParams.addRule(RelativeLayout.BELOW, currentId);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setLayoutParams(newLayoutParams);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
return newLayoutContainer;
}
Try this working code:
int currentId = 5;
#Override
public void onClick(View v) {
if(v.getId()==9){
containerLayout2.addView(createNewLayout(currentId), 0);
currentId +=10;
}
}
createNewLayoutFunction
public RelativeLayout createNewLayout(int currentId){
RelativeLayout newLayoutContainer = new RelativeLayout(this);
final RelativeLayout.LayoutParams newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
newLayoutParams.addRule(RelativeLayout.BELOW, currentId);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setLayoutParams(newLayoutParams);
RelativeLayout.LayoutParams image = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView x = new ImageView(this);
x.setImageResource(android.R.drawable.ic_media_play);
newLayoutContainer.addView(x, image);
newLayoutContainer.setId(currentId+10);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
return newLayoutContainer;
}
also change this line in containerParams2
RelativeLayout.LayoutParams containerParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight+330);
this should work fine. working in my code.