Change gravity header in table layout Android - 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:

Related

TextView FILL_PARENT doesn't work

I want to make that TextView fill parent but my code doesn't work.
Here is my code:
TableLayout tableLayout = new TableLayout(this);
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
TableRow.LayoutParams param = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
param.span = 2;
param.setMargins(1, 1, 1, 1);
int trainIndex = 0;
int stationIndex = 1;
for(int i = 0; i < 6; i++){
TableRow row = new TableRow(this);
if(i == 0){
TextView text = new TextView(this);
text.setText("Way");
text.setBackgroundColor(Color.GRAY);
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
row.addView(text, tableRowParams);
for(int j = 0; j < 4; j++) {
TextView text1 = new TextView(this);
text1.setText(getInfo(0, stationIndex));
text1.setBackgroundColor(Color.GRAY);
row.addView(text1, param);
stationIndex++;
}
}else {
for (int j = 0; j <= 8; j++) {
TextView text = new TextView(this);
if(j == 0){
text.setText(getInfo(1,trainIndex));
text.setBackgroundColor(Color.GRAY);
row.addView(text, tableRowParams);
trainIndex++;
}else {
text.setText(" train " + j);
text.setBackgroundColor(Color.GRAY);
row.addView(text, tableRowParams);
}
}
}
tableLayout.addView(row);
}
setContentView(tableLayout);
When I apply layout params to textview it doesn't work and when text isn't very long it causes white space in cells, I want that all cells have grey background.
Here is the sample how it looks:
What am I doing wrong ?
Be sure that your container has the FILL_PARENT property
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
You forgot to set LayoutParams for the TextView. By default is uses WRAP_CONTENT for both width and height.
Also, use MATCH_PARENT instead of FILL_PARENT
EDIT
Try this:
TableRow row = new TableRow(this);
TextView text = new TextView(this);
text.setText("Way");
text.setBackgroundColor(Color.GRAY);
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
row.addView(text);

How to center a TableRow programmatically

I am creating a number of buttons in a table row programmatically, but they always end up left-aligned. How can I center the entire row? I am trying this to create each row:
TableRow row = new TableRow(getActivity());
TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(params);
for (int j = 0; j != numButtonCols; j++) {
Button b = new Button(getActivity());
b.setOnClickListener(actionListener);
row.addView(b);
actionButtons[j][i] = b;
}
layout.addView(row);
You can simply center the row using a Gravity. Just set it to your row with
row.setGravity(Gravity.CENTER_HORIZONTAL);
Try this:
TableRow row = new TableRow(getActivity());
TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(params);
TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
for (int j = 0; j != numButtonCols; j++) {
Button b = new Button(getActivity());
b.setLayoutParams(buttonParams);
b.setOnClickListener(actionListener);
row.addView(b);
actionButtons[j][i] = b;
}
layout.addView(row);

final LayoutParams size=new LayoutParams(200, 25) is not working when i apply this on my EditTexts

when i pass "size" as argument in code line it show no work......
Here is my code
final LayoutParams size=new LayoutParams(200, 25);//globally declared
private void Add() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
TableLayout myTable = new TableLayout(AddParing.this);
EditText[][] myEditTexts = new EditText[2][2];
myTable.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TableRow[] myRow = new TableRow[2];
for (int a = 0; a < 1; a++) {
myRow[a] = new TableRow(AddParing.this);
myRow[a].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int b = 0; b < 2; b++) {
myEditTexts[a][b] = new EditText(AddParing.this);
myEditTexts[a][b].setText("");
myEditTexts[a[b].setBackgroundResource(R.drawable.input_bar);
myEditTexts[a][b].setLayoutParams(size);
//myEditTexts[a][b].setHeight(15);
myRow[a].setGravity(Gravity.CENTER);
myRow[a].addView(myEditTexts[a][b]);
}
myTable.addView(myRow[a]);
}
TableLayout lin = (TableLayout) findViewById(R.id.tableLayout1);
lin.addView(myTable);
}
});
i want to apply the setlayoutparams(). so how can i apply so it works correctly?
final TableRow.LayoutParams size=new TableRow.LayoutParams(200, 25);
use this code it works.

Dynamically created TableRow and then RelativeLayout not showing

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

dynamic radio button layout not work

I'm trying to make a dynamic radio button to linear layout. I have two radio buttons which are called from List and added to Linear Layout. Everything works fine until I get bad looks at my radio button and get stuck with this layout. My question is why does my radio button layout not work at all?
Here is my code :
TextView label;
RadioButton[] rb;
RadioGroup radiogroup;
LinearLayout ll;
String title;
public MyBtn(Context context,String labelText,String options) {
super(context);
ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(params);
label = new TextView(context);
label.setText(labelText);
label.setTextColor(getResources().getColor(R.color.label_color));
radiogroup = new RadioGroup(context);
radiogroup.setOrientation(RadioGroup.HORIZONTAL);
radiogroup.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams [] rbLayout = new RadioGroup.LayoutParams[2];
LinearLayout.LayoutParams rbParam = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rbParam.weight = 1f;
rbParam.gravity = Gravity.CENTER | Gravity.LEFT;
LinearLayout.LayoutParams rbParam2 = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rbParam2.gravity = Gravity.CENTER | Gravity.RIGHT;
rbParam2.weight = 1f;
rbLayout[0] = new RadioGroup.LayoutParams(rbParam);
rbLayout[1] = new RadioGroup.LayoutParams(rbParam2);
List<String> list = Arrays.asList(options.split("\\|"));
String[] opts = new String[list.size()];
list.toArray(opts);
rb = new RadioButton[list.size()];
for (int i = 0; i < list.size(); i++) {
rb[i] = new RadioButton(context);
rb[i].setText(opts[i]);
rb[i].setId(i);
radiogroup.addView(rb[i],rbLayout[i]);
}
title = label.getText().toString();
radiogroup.check(0);
ll.addView(label);
ll.addView(radiogroup);
//int resID = getResources().getIdentifier(buttonID, "id", context.getPackageName());
this.addView(ll);
}
Thank you.
My radio button :
image link
It should :
image link
change this:
LinearLayout.LayoutParams [] rbLayout = new RadioGroup.LayoutParams[2];
LinearLayout.LayoutParams rbParam2 = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
to this:
RadioGroup.LayoutParams [] rbLayout = new RadioGroup.LayoutParams[2];
RadioGroup.LayoutParams rbParam2 = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
there are many same mistake in your code. find out them and correct
let me know about result
LinearLayout.LayoutParams rbParam = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.FILL_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT,1f);
that must work.
let me know about it

Categories

Resources