issue adding table row to table layout - java

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)

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

dynamically add rows to a table with java code

I hope someone can help me here...
I want to add rows to a table dynamically with a for Loop. This is just for example. But when I test it, the Screen where the table should be showed is empty. I don't know where the Problem is, it just Shows nothing.
Here is my Code:
private void setElementsList() {
TableLayout tableLayout = findViewById(R.id.tablelayout);
for (int i=0; i<5; i++) {
// Creation row
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 70));
tableRow.setPadding(0,0,0,8);
LinearLayout Newrowlayout =new LinearLayout(this);
Newrowlayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,5));
Newrowlayout.setOrientation(LinearLayout.HORIZONTAL);
Newrowlayout.setBackgroundColor(16746559);
// Creation textView
TextView text2 = new TextView(this);
text2.setText("Order ID: ");
text2.setTextSize(21);
text2.setTextColor(16508374);
text2.setGravity(Gravity.CENTER);
text2.setPadding(0,0,2,0);
text2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
// Creation textView
TextView text = new TextView(this);
text.setText(Integer.toString(i));
text.setTextSize(21);
text.setTextColor(16508374);
text.setGravity(Gravity.CENTER);
text.setPadding(2,0,10,0);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
// Creation textView
TextView text3 = new TextView(this);
text3.setGravity(Gravity.CENTER);
text3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT, 3));
// Creation button
Button button = new Button(this);
button.setText(">");
button.setTextSize(30);
button.setTextColor(16508374);
button.setBackgroundColor(16746559);
button.setLayoutParams(new TableRow.LayoutParams(70,70));
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
// Creation textView
TextView text4 = new TextView(this);
text4.setGravity(Gravity.CENTER);
text4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT, 3));
// Creation textView
TextView text5 = new TextView(this);
text5.setGravity(Gravity.CENTER);
text5.setBackgroundColor(16741149);
text5.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT, 1));
Newrowlayout.addView(text2);
Newrowlayout.addView(text);
tableRow.addView(Newrowlayout);
tableRow.addView(text3);
tableRow.addView(button);
tableRow.addView(text4);
tableRow.addView(text5);
tableLayout.addView(tableRow);
}
}
If you Need further Details plz ask.
Best regards
domidoof

How to assign different values to two different table without writing almost same code for other table?

I want to create two tables which have different headers in first row. both tables have 4 columns in header row. How can i create two table with different header values without writing this same code twice?
public void addHeaders(TableLayout t) {
/* Create a TableRow dynamically */
TableRow tr = new TableRow(this);
tr.setBackgroundColor(GRAY);
tr.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
/* Creating a TextView to add to the row */
TextView addressTV = new TextView(this);
addressTV.setText("Address");
addressTV.setTextColor(Color.BLACK);
addressTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
addressTV.setGravity(Gravity.CENTER_HORIZONTAL);
addressTV.setPadding(0, 7, 0, 7);
addressTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.7f));
tr.addView(addressTV); // Adding textView to tablerow.
/* Creating a TextView to add to the row */
TextView typeTV = new TextView(this);
typeTV.setText("Type");
typeTV.setTextColor(Color.BLACK);
typeTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
typeTV.setGravity(Gravity.CENTER_HORIZONTAL);
typeTV.setPadding(0, 7, 0, 7);
typeTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
tr.addView(typeTV); // Adding textView to tablerow.
/* Creating another textview */
TextView amountTV = new TextView(this);
amountTV.setText("Amount");
amountTV.setTextColor(Color.BLACK);
amountTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
amountTV.setGravity(Gravity.CENTER_HORIZONTAL);
amountTV.setPadding(0, 7, 0, 7);
amountTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.2f));
tr.addView(amountTV); // Adding textView to tablerow.
/* Creating another textview */
TextView dateTV = new TextView(this);
dateTV.setText("Date");
dateTV.setTextColor(Color.BLACK);
dateTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
dateTV.setGravity(Gravity.CENTER_HORIZONTAL);
dateTV.setPadding(0, 7, 0, 7);
dateTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.5f));
tr.addView(dateTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
Try passing the header names as an input. Then you can reuse the code to create your table objects while dynamically adjusting the header text fields.
e.g. Here is simple example, assuming always 4 columns...
public void callingFunction() {
TableLayout tl1 = // something;
TableLayout tl2 = // something else;
addHeaders(tl1, "Address", "Type", "Amount", "Date");
addHeaders(tl2, "new", "strings", "for", "table");
}
public TableRow addHeaders(TableLayout tl, String ... names) {
if (names.length != 4) throw new Exception("Unexpected number of table header names");
/* Create a TableRow dynamically */
TableRow tr = new TableRow(this);
tr.setBackgroundColor(GRAY);
tr.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
/* Creating a TextView to add to the row */
TextView addressTV = new TextView(this);
addressTV.setText(names[0]);
addressTV.setTextColor(Color.BLACK);
addressTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
addressTV.setGravity(Gravity.CENTER_HORIZONTAL);
addressTV.setPadding(0, 7, 0, 7);
addressTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.7f));
tr.addView(addressTV); // Adding textView to tablerow.
/* Creating a TextView to add to the row */
TextView typeTV = new TextView(this);
typeTV.setText(names[1]);
typeTV.setTextColor(Color.BLACK);
typeTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
typeTV.setGravity(Gravity.CENTER_HORIZONTAL);
typeTV.setPadding(0, 7, 0, 7);
typeTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
tr.addView(typeTV); // Adding textView to tablerow.
/* Creating another textview */
TextView amountTV = new TextView(this);
amountTV.setText(names[2]);
amountTV.setTextColor(Color.BLACK);
amountTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
amountTV.setGravity(Gravity.CENTER_HORIZONTAL);
amountTV.setPadding(0, 7, 0, 7);
amountTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.2f));
tr.addView(amountTV); // Adding textView to tablerow.
/* Creating another textview */
TextView dateTV = new TextView(this);
dateTV.setText(names[3]);
dateTV.setTextColor(Color.BLACK);
dateTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
dateTV.setGravity(Gravity.CENTER_HORIZONTAL);
dateTV.setPadding(0, 7, 0, 7);
dateTV.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.5f));
tr.addView(dateTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}

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.

Positioning text below image

I'm trying to put in RelativeLayout an image and text, and would like the text was down but over the image.
My code:
RelativeLayout rl;
//
rl = new RelativeLayout(mContext);
rl.setLayoutParams(new GridView.LayoutParams(85, 85));
rl.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(mContext);
imageView.setId(1);
TextView textView = new TextView(mContext);
textView.setId(2);
//
imageView.setImageResource(mThumbIds[position]);
textView.setText(mTextMenu[position]);
//
rl.addView(imageView);
//
RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
//
mParams.addRule(RelativeLayout.ALIGN_BOTTOM, imageView.getId());
rl.addView(textView, mParams);
Use linear layout. Thats more appropriate for your case.
I managed to solve by changing the layout of the TextView:
childParams = new RelativeLayout.LayoutParams(65, 25);
childParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
childParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
rl.addView(textView, childParams);

Categories

Resources