Align by Parent on programmatically generated elements - java

So I am creating headers and input fields programmatically in a loop. As it stands with the following code, all the elements appear bunched together on top of one another:
RelativeLayout mRlayout = (RelativeLayout) findViewById(R.id.checkFieldsLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
Bundle extras = getIntent().getExtras();
List<String> fields = extras.getStringArrayList("checkList");
for (int i = 0; i < fields.size(); i++) {
TextView header = new TextView(this); // Pass it an Activity or Context
header.setText(fields.get(i).toString());
header.setLayoutParams(params);
EditText field = new EditText(this);
field.setLayoutParams(params);
mRlayout.addView(header);
mRlayout.addView(field);
}
How do I programmatically lay out these elements so they match the width of the screen and dock one beneath the other? Thank you!

Try This:
RelativeLayout mRlayout = (RelativeLayout) findViewById(R.id.checkFieldsLayout);
Bundle extras = getIntent().getExtras();
List<String> fields = extras.getStringArrayList("checkList");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
TextView header1 = new TextView(this); // Pass it an Activity or Context
header.setText(fields.get(0).toString());
header.setLayoutParams(params);
header1.setId(0);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.BELOW,0);
EditText field1 = new EditText(this);
field.setLayoutParams(params2);
field1.setId(fields.size());
mRlayout.addView(header);
mRlayout.addView(field);
for (int i = 1; i < fields.size(); i++) {
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.BELOW,fields.size()+i -1);
TextView header = new TextView(this); // Pass it an Activity or Context
header.setText(fields.get(i).toString());
header.setId(i);
header.setLayoutParams(params1);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.BELOW, i);
EditText field = new EditText(this);
field.setLayoutParams(params2);
field.setId(fields.size()+i);
mRlayout.addView(header);
mRlayout.addView(field);
}

Related

How to modify the height and width of a LinearLayout programmatically for a customAlertDialog?

I want to make a customAlertDialog that shows the title, message and button centered, but I cannot make the size of the title and message of my customAlertDialog take the size of the parent
This is what my customAlertDialog currently looks like1
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.RED);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
final TextView header = new TextView(context);
final TextView body = new TextView(context);
final SpannableString formatHeader = new SpannableString(title);
final StyleSpan negrita = new StyleSpan(android.graphics.Typeface.BOLD);
formatHeader.setSpan(negrita,0, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
header.setText(formatHeader);
header.setGravity(Gravity.CENTER);
header.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
header.setTextColor(Color.BLACK);
body.setPadding(8, 0, 8, 10);
body.setText(message);
body.setGravity(Gravity.CENTER);
body.setPadding(8, 0, 8, 0);
body.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
body.setTextColor(Color.BLACK);
layout.addView(header);
layout.addView(body);
view = layout;
break;
You can change the default LayoutParams of Dialog to make your dialog full screen like this:
Window window = yourDialog.getWindow();
if (window != null) {
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setAttributes(lp);
}

Android: LinearLayout.addView not rendering added views

I'm trying to add two text views to a LinearLayout programmatically (in a loop), and then in turn adding that to a LinearLayout that's defined in the layout file. The code runs with no errors, and when evaluating getChildCount I get the expected value, but absolutely nothing is rendering on the device.
XML:
<LinearLayout
android:id="#+id/llNotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="#dimen/activity_vertical_margin_quarter"
android:background="#drawable/rounded_bottom_corners"
android:orientation="vertical"
android:paddingTop="#dimen/activity_vertical_margin_half"
android:paddingBottom="#dimen/activity_vertical_margin" />
Java:
for (Note MyNote : foo.GetNotes()) {
LinearLayout llNoteParent = new LinearLayout(this);
TextView tvNoteHeader = new TextView(this);
TextView tvNoteValue = new TextView(this);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llParams.gravity = Gravity.CENTER_HORIZONTAL;
llNoteParent.setLayoutParams(llParams);
llNoteParent.setOrientation(LinearLayout.HORIZONTAL);
llNoteParent.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
llNoteParent.setPadding(R.dimen.activity_horizontal_margin, R.dimen.activity_vertical_margin_half, R.dimen.activity_horizontal_margin, R.dimen.activity_horizontal_margin);
LinearLayout.LayoutParams tvNoteHeaderParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
tvNoteHeaderParams.weight = 1;
tvNoteHeader.setLayoutParams(tvNoteHeaderParams);
tvNoteHeader.setPadding(R.dimen.activity_vertical_margin_half, R.dimen.activity_vertical_margin_half, R.dimen.activity_vertical_margin_half, R.dimen.activity_vertical_margin_half);
tvNoteHeader.setBackground(getDrawable(R.drawable.left_text_field));
tvNoteHeader.setGravity(Gravity.CENTER);
tvNoteHeader.setText(MyNote.GetAbbreviatedText());
LinearLayout.LayoutParams tvNoteValueParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);
tvNoteValueParams.weight = 1;
tvNoteValue.setLayoutParams(tvNoteValueParams);
tvNoteValue.setPadding(R.dimen.activity_vertical_margin_half, R.dimen.activity_vertical_margin_half, R.dimen.activity_vertical_margin_half, R.dimen.activity_vertical_margin_half);
tvNoteValue.setBackground(getDrawable(R.drawable.right_text_field));
tvNoteValue.setText(MyNote.GetText());
llNoteParent.addView(tvNoteHeader);
llNoteParent.addView(tvNoteValue);
llNotes.addView(llNoteParent);
}
you cannot pass number with dp into setPadding method
setPadding(R.dimen.activity_horizontal_margin, R.dimen.activity_vertical_margin_half, R.dimen.activity_horizontal_margin, R.dimen.activity_horizontal_margin);
you need to pass integers into setPadding method.
1- try something like this
llNoteParent.setPadding(20,20,20,20);
tvNoteHeader.setPadding(20,20,20,20);
2- OR user integer.xml
for (Note MyNote : foo.GetNotes()) {
LinearLayout llNoteParent = new LinearLayout(this);
TextView tvNoteHeader = new TextView(this);
TextView tvNoteValue = new TextView(this);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llParams.gravity = Gravity.CENTER_HORIZONTAL;
llNoteParent.setLayoutParams(llParams);
llNoteParent.setOrientation(LinearLayout.HORIZONTAL);
llNoteParent.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
llNoteParent.setPadding(R.integer.activity_horizontal_margin, R.integer.activity_vertical_margin_half, R.integer.activity_horizontal_margin, R.integer.activity_horizontal_margin);
LinearLayout.LayoutParams tvNoteHeaderParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
tvNoteHeaderParams.weight = 1;
tvNoteHeader.setLayoutParams(tvNoteHeaderParams);
tvNoteHeader.setPadding(R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half);
tvNoteHeader.setBackground(getDrawable(R.drawable.left_text_field));
tvNoteHeader.setGravity(Gravity.CENTER);
tvNoteHeader.setText(MyNote.GetAbbreviatedText());
LinearLayout.LayoutParams tvNoteValueParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);
tvNoteValueParams.weight = 1;
tvNoteValue.setLayoutParams(tvNoteValueParams);
tvNoteValue.setPadding(R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half, R.integer.activity_vertical_margin_half);
tvNoteValue.setBackground(getDrawable(R.drawable.right_text_field));
tvNoteValue.setText(MyNote.GetText());
llNoteParent.addView(tvNoteHeader);
llNoteParent.addView(tvNoteValue);
llNotes.addView(llNoteParent);
}
NOTE: Add the values to integer.xml file in values.
You have to use,
float horizontalMargin = getResources()
.getDimension(R.dimen.R.dimen.activity_horizontal_margin);
float verticleMargin = getResources()
.getDimension(R.dimen.R.dimen.activity_vertical_margin_half);
After that set padding,
llNoteParent.setPadding(horizontalMargin,verticleMargin, horizontalMargin, horizontalMargin);
tvNoteHeader.setPadding(verticleMargin, verticleMargin, verticleMargin, verticleMargin);
tvNoteValue.setPadding(verticleMargin, verticleMargin, verticleMargin, verticleMargin);

Android RelativeLayout not moving items

I am trying to create a viewGroup that takes the form of a 'post' and I need this view to have an image in the upper left and then a username and content to the right of that post, aligned with eachother however I can not seem to get the layouts to move. They just stay overlapping.
public ViewGroup generateViewGroup(Context context){
RelativeLayout rl = new RelativeLayout(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point p = new Point();
wm.getDefaultDisplay().getSize(p);
TextView tvUsername = new TextView(context);
tvUsername.setText(userName);
TextView tvPostContent = new TextView(context);
tvPostContent.setText(postContent);
ImageView iv = new ImageView(context);
RelativeLayout.LayoutParams lpTvUsername = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lpTvUsername.addRule(RelativeLayout.RIGHT_OF,0);
lpTvUsername.addRule(RelativeLayout.ALIGN_TOP,0);
tvUsername.setLayoutParams(lpTvUsername);
RelativeLayout.LayoutParams lpTvPostContent = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lpTvPostContent.addRule(RelativeLayout.RIGHT_OF,0);
lpTvPostContent.addRule(RelativeLayout.BELOW,1);
tvPostContent.setLayoutParams(lpTvPostContent);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
if (userPic==null) Log.e("generateViewGroup","userPic is null");
iv.setImageDrawable(userPic);
rl.addView(iv, 0);
rl.addView(tvUsername,1);
rl.addView(tvPostContent);
rl.setLayoutParams(lp);
}
I have modified the code in the following way and set it as the content view of a blank Activity:
RelativeLayout rl = new RelativeLayout(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point p = new Point();
wm.getDefaultDisplay().getSize(p);
//
// integer ID's for all the Views;
int ivID = 0x0FD;
int tvUserNameID = 0x0FE;
int tvPostContentID = 0x0FF;
//
TextView tvUsername = new TextView(context);
//
tvUsername.setId(tvUserNameID); // Set the ID
//
tvUsername.setText(userName);
TextView tvPostContent = new TextView(context);
//
tvPostContent.setId(tvPostContentID); // Set the ID
//
tvPostContent.setText(postContent);
ImageView iv = new ImageView(context);
//
iv.setId(ivID); // Set the ID
//
RelativeLayout.LayoutParams lpTvUsername = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//
//lpTvUsername.addRule(RelativeLayout.RIGHT_OF,0);
//lpTvUsername.addRule(RelativeLayout.ALIGN_TOP,0);
//
// Reference the ID not the Index;
lpTvUsername.addRule(RelativeLayout.RIGHT_OF,ivID);
lpTvUsername.addRule(RelativeLayout.ALIGN_TOP,ivID);
//
tvUsername.setLayoutParams(lpTvUsername);
RelativeLayout.LayoutParams lpTvPostContent = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//
// lpTvPostContent.addRule(RelativeLayout.RIGHT_OF,0);
// lpTvPostContent.addRule(RelativeLayout.BELOW,1);
//
// Reference the ID not the Index;
lpTvPostContent.addRule(RelativeLayout.RIGHT_OF,ivID);
lpTvPostContent.addRule(RelativeLayout.BELOW,tvUserNameID);
//
tvPostContent.setLayoutParams(lpTvPostContent);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
if (userPic==null) Log.e("generateViewGroup","userPic is null");
iv.setImageDrawable(userPic);
rl.addView(iv, 0);
rl.addView(tvUsername,1);
rl.addView(tvPostContent);
rl.setLayoutParams(lp);
setContentView(rl);
Mainly the problem was taking place because while setting the LayoutParams you were referencing the index not the Views ID.

Modify weight in the code doesn't display anything

I'm trying to create with code, 3 linearlayout with weight. Put them in a linearlayout, and put this last in a RelativeLayout. At the end I use this RelativeLayout in a Spinner (I say this in case is relevant).
The problem is when I try to put the weights, the RelativeLayout I obtain don't display anything (When I create the parameters, if I put WRAP_CONTENT instead 0, then is displayed, but of course ignores the weight).
public RelativeLayout createRL (final object objectSel, boolean opcion)
{
if(opcion)
{
objectSel.setTimes(1);
}
RelativeLayout rl = new RelativeLayout(this);
LinearLayout llayout = new LinearLayout(this);
llayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout f0 = new LinearLayout(this);
f0.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout f1 = new LinearLayout(this);
f1.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout f2 = new LinearLayout(this);
f2.setOrientation(LinearLayout.HORIZONTAL);
final TextView t0 = new TextView(this);
final TextView t1 = new TextView(this);
ImageButton ib1 = new ImageButton(this);
ImageButton ib2 = new ImageButton(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
param.setMargins(0, 10, 0, 10);
llayout.setLayoutParams(new ViewGroup.LayoutParams(param));
LinearLayout.LayoutParams param0 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT);
param0.weight=0.1f;
f0.setLayoutParams(new ViewGroup.LayoutParams(param0));
LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT);
param1.weight=0.7f;
f1.setLayoutParams(new ViewGroup.LayoutParams(param1));
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT);
param2.weight=0.2f;
f2.setLayoutParams(new ViewGroup.LayoutParams(param2));
t0.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
t0.setTypeface(null,Typeface.BOLD);
LinearLayout.LayoutParams param3 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
param2.weight=1.0f;
t1.setLayoutParams(new ViewGroup.LayoutParams(param3));
t1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
LinearLayout.LayoutParams param4 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
ib1.setLayoutParams(new ViewGroup.LayoutParams(param4));
ib1.setBackgroundResource(R.drawable.boton_mas_xml);
LinearLayout.LayoutParams param5 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
ib2.setLayoutParams(new ViewGroup.LayoutParams(param5));
ib2.setBackgroundResource(R.drawable.boton_menos_xml);
f0.setGravity(Gravity.CENTER_VERTICAL);
f1.setGravity(Gravity.CENTER_VERTICAL);
f2.setGravity(Gravity.RIGHT);
t0.setText(Integer.toString(objectSel.getTimes())+" ");
t1.setText(objectSel.getNombre());
f0.addView(t0);
f1.addView(t1);
f2.addView(ib1);
f2.addView(ib2);
llayout.addView(f0);
llayout.addView(f1);
llayout.addView(f2);
rl.setPadding(0, 0, 1, 3);
rl.addView(llayout);
return rl;
}
For linear layous you need to use LinearLayout.LayoutParams object.
MATCH_PARENT property breaks weight. Basicly use 0px fixed size to avoid calculations.

Create a new TextView programmatically then display it below another TextView

String[] textArray={"one","two","asdasasdf asdf dsdaa"};
int length=textArray.length;
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<length;i++){
TextView tv=new TextView(getApplicationContext());
tv.setText(textArray[i]);
relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
layout.addView(tv, relativeParams);
}
I need to do something like that.. so it would display as
one
two
asdfasdfsomething
on the screen..
If it's not important to use a RelativeLayout, you could use a LinearLayout, and do this:
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Doing this allows you to avoid the addRule method you've tried. You can simply use addView() to add new TextViews.
Complete code:
String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for( int i = 0; i < textArray.length; i++ )
{
TextView textView = new TextView(this);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}
Try this code:
final String[] str = {"one","two","three","asdfgf"};
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
final TextView[] tv = new TextView[10];
for (int i=0; i<str.length; i++)
{
tv[i] = new TextView(this);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params.leftMargin = 50;
params.topMargin = i*50;
tv[i].setText(str[i]);
tv[i].setTextSize((float) 20);
tv[i].setPadding(20, 50, 20, 50);
tv[i].setLayoutParams(params);
rl.addView(tv[i]);
}
public View recentView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create a relative layout and add a button
relativeLayout = new RelativeLayout(this);
btn = new Button(this);
btn.setId((int)System.currentTimeMillis());
recentView = btn;
btn.setText("Click me");
relativeLayout.addView(btn);
setContentView(relativeLayout);
btn.setOnClickListener(new View.OnClickListener() {
#Overr ide
public void onClick(View view) {
//Create a textView, set a random ID and position it below the most recently added view
textView = new TextView(ActivityName.this);
textView.setId((int)System.currentTimeMillis());
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, recentView.getId());
textView.setText("Time: "+System.currentTimeMillis());
relativeLayout.addView(textView, layoutParams);
recentView = textView;
}
});
}
This can be modified to display each element of a String array in different TextViews.
You're not assigning any id to the text view, but you're using tv.getId() to pass it to the addRule method as a parameter. Try to set a unique id via tv.setId(int).
You could also use the LinearLayout with vertical orientation, that might be easier actually. I prefer LinearLayout over RelativeLayouts if not necessary otherwise.

Categories

Resources