Modify weight in the code doesn't display anything - java

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.

Related

Add click effect Ripple effect on Card View in Android Studio

I have programmatically added Card View. I just want to make it clickable and show animation while it is clicked. Here is my Code
CardView cardView = new CardView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cardView.setLayoutParams(layoutParams);
cardView.setRadius(15);
cardView.setPadding(25, 25, 25, 25);
cardView.setCardBackgroundColor(Color.MAGENTA);
TextView textView = new TextView(this);
textView.setLayoutParams(layoutParams);
textView.setText("Programmatically set");
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.WHITE);
cardView.addView(textView);
LinearLayout linearLayout = findViewById(R.id.linearLayout1);
linearLayout.addView(cardView);
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = this.obtainStyledAttributes(attrs);
int selectableItemBackground = typedArray.getResourceId(0, 0);
typedArray.recycle();
cardView.setForeground(this.getDrawable(selectableItemBackground));
cardView.setClickable(true);
With the Material Components Library just use:
cardView.setRippleColor(ContextCompat.getColorStateList(this,R.color.selector_card));
You can use this:
cardView.setClickable(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
cardView.setBackgroundResource(typedValue.resourceId);
}

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);

Align by Parent on programmatically generated elements

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);
}

issue adding table row to table layout

I have the following code:
HashMap<String, String> tip = venue.tips.get(j);
TableRow tr = new TableRow(this);
TableRow.LayoutParams tableRowParams =
new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(tableRowParams);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextView tips = new TextView(this);
tips.setText(tip.get("text"));
Log.v("TIPS TEXT", tip.get("text"));
tips.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
ImageView avatar = new ImageView(this);
avatar.setImageBitmap(getBitmapFromURL(tip.get("photo")));
Log.v("PHOTO URL", tip.get("photo"));
avatar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
ll.addView(tips);
ll.addView(avatar);
tr.addView(ll);
View v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
tipsTable.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tipsTable.addView(v);
I think I did something wrong with setting up the layout, but the issue now is that I can't see the rows added to the tipsTable. Why is this?
You're not calling setContentView() anywhere. You create this layout but never assign it to the activity. I think what you want to do is after you've set up all your layout stuff:
setContentView(ll);
along the same line: tipsTable seems to be your top view so try setContentView(tipsTable)

Categories

Resources