Android: LinearLayout.addView not rendering added views - java

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

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 wrap dynamically created textviews in android?

I am creating various textviews inside a linear layout (lay). I want that when the textview reach out of the screen then that textview automatically jump on the next line instead of out of the screen. I am a beginner in android and not getting how to achieve it.
Here is my code:
for (int i = 0; i < string.size(); i++) {
final TextView txt = new TextView(con);
txt.setText(string.get(i));
lay.addView(txt);
}
Try This
TextView txt = new TextView(context);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
private void populateViews(LinearLayout linearLayout, ArrayList<TextView> views, View extraView)
{
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
float mWidth = displaymetrics.widthPixels;
extraView.measure(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.removeAllViews();
int maxWidth = (int) mWidth - extraView.getMeasuredWidth() - 10;
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params;
LinearLayout newLL = new LinearLayout(mContext);
newLL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
newLL.setGravity(Gravity.LEFT);
newLL.setOrientation(LinearLayout.HORIZONTAL);
int widthSoFar = 0;
for (int i = 0; i < views.size(); i++)
{
LinearLayout LL = new LinearLayout(mContext);
LL.setOrientation(LinearLayout.HORIZONTAL);
LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
LL.setLayoutParams(new ListView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
views.get(i).measure(0, 0);
params = new LinearLayout.LayoutParams(views.get(i).getMeasuredWidth(), LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 2, 10, 2);
LL.addView(views.get(i), params);
LL.measure(0, 0);
widthSoFar += views.get(i).getMeasuredWidth();
if (widthSoFar >= maxWidth)
{
linearLayout.addView(newLL);
newLL = new LinearLayout(mContext);
newLL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
newLL.setOrientation(LinearLayout.HORIZONTAL);
newLL.setGravity(Gravity.LEFT);
params = new LinearLayout.LayoutParams(LL.getMeasuredWidth(), LL.getMeasuredHeight());
newLL.addView(LL, params);
widthSoFar = LL.getMeasuredWidth();
}
else
{
newLL.addView(LL);
}
}
linearLayout.addView(newLL);
}
This method should be able to help you. I had made this method long back, so with a few modifications you should be able to use it. The first parameter is a LinearLayout which will be the base vertical linear layout which will act as the primary container for the views. The second parameter are the actual views you have to add. The third parameter in my case was an Image which I was using like a placeholder.
You can add like this also.
for (int i = 0; i < string.size(); i++) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(
R.layout.Layout_Design, null);
TextView txt = (TextView) view.findViewById(R.id.some_text);
txt.setText("for runtime change");
layout.addView(view);
// layout is linear layout in my case.
}
Layout_Design:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp" >
<RelativeLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/some_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SOme Text" />
</RelativeLayout>
</FrameLayout>

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

use java code instead of XML?

in my app i cant use XML layout for some reason now i need to create a layout which its XML code is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/adlayout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="vertical" >
<ir.adad.AdView
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
token="fhsfxfhdghghrgfggh" />
</LinearLayout>
now i need to create a java code which its the same as this XML code! is there any way?how i can do this?
i:ir.adad.adview is a ad services and i put its liberally in my app.
This might help you a bit.
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
int adlayout = 12345; //if you need id of layout somewhere else
//View.generateViewId();Can be used if minSDK= 17
layout.setId(adlayout);
android.view.ViewGroup.LayoutParams params = layout.getLayoutParams();
params.height = 48;
params.width = LayoutParams.MATCH_PARENT;
//Below code is used if you need height in dp.
//int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <HEIGHT>, getResources().getDisplayMetrics());
ir.adad.AdView adView = new ir.adad.AdView(this);
int adViewId = 123456;//Should not be dublicate.
adView.setId(adViewId);
android.view.ViewGroup.LayoutParams paramView = adView.getLayoutParams();
paramView.height = LayoutParams.WRAP_CONTENT;
paramView.width = LayoutParams.WRAP_CONTENT;
layout.addView(adView)
Since I don't know that ad service library I can't help you with its constructor and creation but this should give you something to start working on.
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,48);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
ir.adad.AdView adview = new ir.adad.AdView();
LinearLayout.LayoutParams adviewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
adview.setLayoutParams(params);
adview.setToken("asdasdas");
layout.addView(adview);

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.

Categories

Resources