Dynamically created TableRow and then RelativeLayout not showing - android

I am iterating through the results of a query creating RelativeLayouts in TableRows. If I try to set the LayoutParams for the RelativeLayout then nothing appears and get no errors. If I don't then everything appears but with the layout being set to WRAP_CONTENT. I have tried several example and can not get it to work. Below is my code:
TableLayout tblItems = (TableLayout) findViewById(R.id.tblItems);
// Fields from the database (projection)
String[] projection = new String[] { ItemsTable.ITEM_ID,
ItemsTable.ITEM_NAME,
ItemsTable.ITEM_PRICE,
ItemsTable.ITEM_PICTURE };
Cursor cursor = getContentResolver().query(ItemsTable.CONTENT_URI, projection, null, null, null);
startManagingCursor(cursor);
// Establish the item mapping
// String[] columns = new String[] {"name", "price"};
// int[] to = new int[] {R.id.tv_description, R.id.tv_price};
// adapter = new SimpleCursorAdapter(this, R.layout.item_list_select, cursor, columns, to, 0);
// lvSelectItems.setAdapter(adapter);
/*
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == cursor.getColumnIndexOrThrow(OrderDetailTable.ORDERDETAIL_PRICE)) {
// String createDate = aCursor.getString(aColumnIndex);
// TextView textView = (TextView) aView;
// textView.setText("Create date: " + MyFormatterHelper.formatDate(getApplicationContext(), createDate));
double total = cursor.getDouble(columnIndex);
TextView textView = (TextView) view;
textView.setText("$" + String.format("%.2f",dRound(total, 2)));
return true;
}
return false;
}
});
*/
// tblItems
int totalRows = 0;
int tcolumn = 1;
int numItems = cursor.getCount();
double tempRow = numItems / 2;
if (numItems > 0) {
itemsFound = true;
} else {
itemsFound = false;
}
long iPart; // Integer part
double fPart; // Fractional part
boolean ifFirst = true;
// Get user input
iPart = (long) tempRow;
fPart = tempRow - iPart;
if (fPart > 0) {
totalRows = (int) (iPart + 1);
} else {
totalRows = (int) iPart;
}
TableRow row = null;
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
// tblItems // TableLayout
// TableRow
// RelativeLayout
// ImageView
// TextView
// TextView
if (ifFirst) {
// create a new TableRow
row = new TableRow(this);
ifFirst = false;
}
// 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.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.setMargins(5, 5, 5, 5);
relativeLayout.setPadding(5, 5, 5, 5);
relativeLayout.setBackgroundResource(R.drawable.grpbx_item);
row.addView(relativeLayout, rlp);
// add text view
ImageView imgPic = new ImageView(this);
imgPic.setBackgroundResource(R.drawable.takepic);
imgPic.setPadding(0, 0, 0, 0);
relativeLayout.addView(imgPic);
// add text view
TextView tvName = new TextView(this);
String name = cursor.getString(cursor.getColumnIndexOrThrow(ItemsTable.ITEM_NAME));
tvName.setText("" + name);
// 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);
// RelativeLayout.
// Setting the parameters on the TextView
tvName.setLayoutParams(lp);
relativeLayout.addView(tvName);
// add text view
double iPrice = cursor.getDouble(cursor.getColumnIndexOrThrow(ItemsTable.ITEM_PRICE));
TextView tvPrice = new TextView(this);
tvPrice.setText("$" + String.format("%.2f",dRound(iPrice, 2)));
relativeLayout.addView(tvPrice);
tcolumn++;
numItems--;
if (tcolumn == 3) {
// add the TableRow to the TableLayout
tblItems.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// create a new TableRow
row = new TableRow(this);
tcolumn = 1;
} else {
if (numItems == 0) {
// add the TableRow to the TableLayout
tblItems.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
// Move to the next record
cursor.moveToNext();
}
}

If I try to set the layout params for the relativeLayout then nothing
appears and get no errors. If I don't then everything appears but with
the layout being set to wrapcontent.
You use the wrong LayoutParams for the RelativeLayout. You use:
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
row.addView(relativeLayout, rlp);
but as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams:
TableRow.LayoutParams rlp = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
row.addView(relativeLayout, rlp);

Related

what is wrong in this first item in gridlayout?

In this code i am able to create gridlayout. but the first item is in default view and the rest is like what i want. I try debug and I couldn't see what is wrong. Would you help me to solve this.
question="this is a sentence";
String sLetter;
String question = question.replace(" ", "");
//char[] charAr = question.toCharArray();
final ArrayList<String> letters = new ArrayList<>();
for (int k = 0; k < question.length(); k++) {
sLetter = Character.toString(question.charAt(k));
letters.add(sLetter);
}
Collections.sort(letters);
int i;
for (i = 0; i < letters.size();) {
int count = recursiveMethod(letters,i,1);
if (count>0) {
//region text add
View main_view = new View(context);
main_view.setLayoutParams(new LinearLayout.LayoutParams(100, ViewGroup.LayoutParams.WRAP_CONTENT));
gridLayout.setColumnCount(7);
if (question.length()<=7){
gridLayout.setRowCount(1);
}else if (question.length()<12){
gridLayout.setRowCount(2);
}else {
gridLayout.setRowCount(3);
}
GridLayout.Spec colSpan = GridLayout.spec(GridLayout.UNDEFINED,1);
GridLayout.Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
rowSpan, colSpan);
gridParam.setGravity(Gravity.FILL_HORIZONTAL);
gridParam.setMargins(5, 10, 5, 10);
final TextView tv = new TextView(context);
tv.setText(letters.get(i).toUpperCase());
tv.setId(i);
tv.setPadding(15, 15, 15, 15);
tv.setTextColor(getResources().getColor(R.color.colorPrimaryLight));
RelativeLayout.LayoutParams relTv = new RelativeLayout.LayoutParams(80, ViewGroup.LayoutParams.WRAP_CONTENT);
relTv.addRule( RelativeLayout.CENTER_HORIZONTAL);
relTv.addRule( RelativeLayout.CENTER_VERTICAL);
tv.setLinksClickable(true);
tv.setTextSize(0, 60);
//tv.setLayoutParams(relTv);
CardView cardView = new CardView(context);
cardView.setCardBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
cardView.setRadius(10);
cardView.setCardElevation(5);
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
final TextView textCount = new TextView(context);
textCount.setText(Integer.toString(count));
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relParam.addRule(RelativeLayout.ALIGN_BOTTOM, i);
relParam.addRule(RelativeLayout.RIGHT_OF, i);
textCount.setTypeface(Typeface.DEFAULT_BOLD);
textCount.setTextColor(getResources().getColor(R.color.colorPrimaryLight));
textCount.setTextSize(0, 30);
textCount.setPadding(0,5,5,0);
//textCount.setLayoutParams(relParam);
relativeLayout.addView(tv,relTv);
relativeLayout.addView(textCount,relParam);
cardView.addView(relativeLayout);
//llLetters.addView(tv, lp);
gridLayout.addView(cardView,gridParam);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, tv.getText()+ "" + textCount.getText(), Toast.LENGTH_SHORT).show();
}
});
//llLetters.addView(gridLayout);
//endregion
i = i + count;
}
else {
i++;
}
}
The first letter of image is at the top left. I want to show that at the bottom right side of the letter. Would you help me to solve problem? Thanks =)
The problem lies here:
relParam.addRule(RelativeLayout.ALIGN_BOTTOM, i);
relParam.addRule(RelativeLayout.RIGHT_OF, i);
If you pass a zero to addRule, it is interpreted as false. Therefore you say, that ALIGN_BOTTOM should be false in your first letter, when i is zero.
See the documentation:
parameter: subject, int: the ID of another view to use as an anchor, or a boolean value (represented as RelativeLayout.TRUE for true or 0 for false).

Change gravity header in table layout Android

I have this
public void header(int recursocabecera)
{
TableRow.LayoutParams layoutCelda;
TableRow fila = new TableRow(actividad);
TableRow.LayoutParams layoutFila = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
fila.setLayoutParams(layoutFila);
String[] arraycabecera = rs.getStringArray(recursocabecera);
COLUMNAS = arraycabecera.length;
for(int i = 0; i < arraycabecera.length; i++)
{
TextView texto = new TextView(actividad);
// layoutCelda = new TableRow.LayoutParams(obtenerAncho(arraycabecera[i]), LayoutParams.FILL_PARENT);
layoutCelda = new TableRow.LayoutParams(obtenerAnchoPixelesTexto(arraycabecera[i]), LayoutParams.FILL_PARENT);
// layoutCelda= new LayoutParams(100, ViewGroup.LayoutParams.FILL_PARENT);
texto.setSingleLine(true);
texto.setText(arraycabecera[i]);
texto.setGravity(Gravity.CENTER_HORIZONTAL);
texto.setRotation(-90);
texto.setTextAppearance(actividad, R.style.estilo_celda2);
texto.setBackgroundResource(R.drawable.tabla_celda_cabecera2);
texto.setLayoutParams(layoutCelda);
texto.setPadding(20,0,0,100);
layoutCelda.setMargins(0,0,60,0);
layoutCelda.setMarginEnd(90);
fila.addView(texto);
}
tabla.addView(fila);
filas.add(fila);
FILAS++;
}
the result is somenthing like that
but I need it to be something like this
setGravity it works half, but the titles are shown cropped
My result:

Why getView() of BaseAdapter showing image multiple times?

I'm using the following code to make a custom listview. I have to show image dynamically, so all the images are added to this in Linearlayout. The problem is that these dynamic images add multiple times. Below is the code of my getView().
LinearLayout fbpiclayout = null;
if (convertView == null)
vi = inflater.inflate(R.layout.popular_event_list, null);
fbpiclayout = (LinearLayout) vi
.findViewById(R.id.fbpic_layout);
ArrayList<String> list= mDbManager
.getAllValuesComingSoonFriendList(facebookEventList
.get(position).getEventId());
int height = 0;
for(int i=0;i<list.size();i++)
{
if(i<3)
{
String friendPics = "http://graph.facebook.com/"
+ list.get(i)
+ "/picture?type=large";
Log.d("Friends","list no "+i+" "+mDbManager
.getFacebookFriendName(list.get(i)));
ImageView fbFriendimage = new ImageView(getActivity());
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
vp.setMargins(3, 0, 3, 3);
fbFriendimage.setLayoutParams(vp);
fbFriendimage.setAdjustViewBounds(true);
fbFriendimage.getLayoutParams().height = height;
fbFriendimage.getLayoutParams().width = width_iv;
fbFriendimage.setScaleType(ImageView.ScaleType.CENTER_CROP);
// image.setImageBitmap(bm);
imageLoader.DisplayImage(friendPics, fbFriendimage);
fbpiclayout.addView(fbFriendimage, vp);
}
}
Kindly suggest me on that issue.
Thanks in Advance.
Try the following code:
LinearLayout fbpiclayout = null;
if (convertView == null)
vi = inflater.inflate(R.layout.popular_event_list, null);
fbpiclayout = (LinearLayout) vi
.findViewById(R.id.fbpic_layout);
//if the convertView was not null it would have the child view you added the
// last time liner layout was used. So remove the added child view.
fbpiclayout.removeAllViews();
ArrayList<String> list= mDbManager
.getAllValuesComingSoonFriendList(facebookEventList
.get(position).getEventId());
int height = 0;
for(int i=0;i<list.size();i++)
{
if(i<3)
{
String friendPics = "http://graph.facebook.com/"
+ list.get(i)
+ "/picture?type=large";
Log.d("Friends","list no "+i+" "+mDbManager
.getFacebookFriendName(list.get(i)));
ImageView fbFriendimage = new ImageView(getActivity());
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
vp.setMargins(3, 0, 3, 3);
fbFriendimage.setLayoutParams(vp);
fbFriendimage.setAdjustViewBounds(true);
fbFriendimage.getLayoutParams().height = height;
fbFriendimage.getLayoutParams().width = width_iv;
fbFriendimage.setScaleType(ImageView.ScaleType.CENTER_CROP);
// image.setImageBitmap(bm);
imageLoader.DisplayImage(friendPics, fbFriendimage);
fbpiclayout.addView(fbFriendimage, vp);
}
}
EDIT1: Try to use smart image view http://loopj.com/android-smart-image-view/ to improve performance....
don't use that loop. Put that list's size in getCount(). It will work properly

Table doesn't fill the containing layout's width

I want this table to extend as possible to fill the remaining (empty) area of the linear layout that contains it
I generate the table using the following method
public void drawTable(LinearLayout tableLayout,String tableTitle, String[][] data,String[] headerTitles, int type){
//clearing previous views
tableLayout.setVisibility(View.VISIBLE);
tableLayout.setGravity(Gravity.CENTER);
int chids = tableLayout.getChildCount();
if (chids > 0){
tableLayout.removeAllViews();
}
if((headerTitles!= null) && (headerTitles.length != data[0].length) ){
return;
}
TableLayout table = new TableLayout(context);
// table.setStretchAllColumns(true);
// table.setShrinkAllColumns(true);
//Rendering the table title
TextView tableTitleView = new TextView(context);
tableTitleView.setText(tableTitle);
tableTitleView.setTextColor(Color.WHITE);
tableTitleView.setGravity(Gravity.CENTER);
tableTitleView.setTextSize(18);
tableTitleView.setTextAppearance(context, R.style.boldText);
tableTitleView.setBackgroundColor(Color.parseColor("#008888"));
tableTitleView.setPadding(2, 2, 2, 10);
tableLayout.addView(tableTitleView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
ScrollView scroll1 = new ScrollView(context);
HorizontalScrollView scroll2 = new HorizontalScrollView(context);
int rows = data.length;
int columns = data[0].length;
if(headerTitles!=null){
//Rendering the header
TableRow headerRow = new TableRow(context);
for (int i = 0; i < columns; i++) {
TextView t = new TextView(context);
t.setTextSize(17);
t.setTextAppearance(context, R.style.boldText);
t.setPadding(5, 5, 5, 5);
t.setText(headerTitles[i]);
t.setGravity(Gravity.CENTER);
t.setTextColor(Color.WHITE);
t.setBackgroundResource(R.drawable.text_border_header_1);
headerRow.addView(t, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
}
table.addView(headerRow, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
//Rendering the Table Data
TableRow row;
for (int current = 0; current < rows ; current++) {
row = new TableRow(context);
TextView t;
for(int i =0 ; i < columns ; i++){
t = new TextView(context);
t.setTextSize(15);
t.setPadding(5,5,5,5);
t.setText(data[current][i]);
t.setGravity(Gravity.CENTER);
if (type == 1) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd);
} else {
t.setBackgroundResource(R.drawable.text_border_even);
}
}else if (type == 2) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd_2);
} else {
t.setBackgroundResource(R.drawable.text_border_even_2);
}
}else if (type == 3) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd_3);
} else {
t.setBackgroundResource(R.drawable.text_border_even_3);
}
}
t.setTextColor(Color.BLACK);
row.addView(t,TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
}
table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
scroll2.addView(table,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
scroll1.addView(scroll2,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
scroll1.setPadding(2, 20, 2, 20);
tableLayout.addView(scroll1,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
}
I couldn't figure out exactly the problem with my code.
EDIT :
when I added the table directly to the linear layout instead of the scroll views the table fits with the linear layout width. It seems that my problem is with the scroll view
I found the answer Here (thanks to Felix)
Just a single line of code to be added to the scroll views
scrollView.setFillViewPort(true);
try this on your TableLayout object.
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.width = LayoutParams.FILL_PARENT;
table.setLAyoutParams(params);
Edit:
TextView tView = new TextView(this);
LinearLayout.LayoutParams params = tView.getLayoutParams();
params.width = LayoutParams.FILL_PARENT;
params.weight = 1;
tView.setLayoutParams(params);
regards,
Aqif

Add TextViews programmatically

I have an empty LinearLayout, and I need to add a dynamic number of TextViews to it. However when I use the code below, only the first TextView is shown:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] listofnumbers = new String[1000];
for ( int i = 0 ; i < 1000 ; ++i ) {
listofnumbers[i] = "null";
}
Context context = getBaseContext();
String text = null;
Uri uri = Uri.parse("content://sms");
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
String[] columnNames = cursor.getColumnNames();
LinearLayout lv = new LinearLayout(context);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, dip(48));
boolean v = true;
while (cursor.moveToNext())
{
String numberString = (cursor.getString( cursor.getColumnIndexOrThrow("address") ) ).replace(" ", "");
int i = 0;
boolean numberNotPresent = true;
for ( ; listofnumbers[i] != "null" ; ++i ) {
if ( numberString.equals(listofnumbers[i]) ) {
numberNotPresent = false;
}
}
if ( numberNotPresent == true ) {
text = (CharSequence) "From: " + cursor.getString(cursor.getColumnIndexOrThrow("address")) + ": " + cursor.getString(cursor.getColumnIndexOrThrow("body"));
listofnumbers[i] = numberString;
TextView tv = new TextView(this);
tv.setText(text);
tv.setLayoutParams(textViewParams);
lv.addView( tv );
}
}
setContentView(lv);
}
Where have I gone wrong?
try to change these two lines:
LinearLayout lv = new LinearLayout(context);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, dip(48));
with these:
LinearLayout lv = new LinearLayout(context);
lv.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
basically you need to set the orientation for the lv LinearLayout to vertical as the default one is horizontal.
I am not sure, but may be for each textView you set fill_parent param? The 1st textView shows on all your layout.

Categories

Resources