I add columns to an android table layout in this way:
public void addWebAction(TableLayout table){
TableRow rowWeb = new TableRow(this);
rowWeb.setWeightSum(1f);
TextView tvWeb = new TextView(this);
tvWeb.setTextSize(18);
tvWeb.setPadding(0, 10, 0, 0);
tvWeb.setText(contact.getWeb());
TableRow.LayoutParams trlp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.8f);
tvWeb.setLayoutParams(trlp);
rowWeb.addView(tvWeb);
ImageButton ibWeb = new ImageButton(this);
ibWeb.setBackgroundColor(Color.TRANSPARENT);
ibWeb.setImageResource(R.drawable.ic_action_search);
ibWeb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(contact.getWeb()));
startActivity(browserIntent);
}
});
trlp.weight = 0.1f;
ibWeb.setLayoutParams(trlp);
rowWeb.addView(ibWeb);
ImageButton abWeb = new ImageButton(this);
abWeb.setBackgroundColor(Color.TRANSPARENT);
abWeb.setImageResource(R.drawable.ic_action_chat);
abWeb.setLayoutParams(trlp);
rowWeb.addView(abWeb);
table.addView(rowWeb);
}
The problem is: If the textview contains a long string the 2nd column is moved out of the screen. In which way did I have to add TableRow Parameters to fix this?
You must give weightSum to table layout and divide edittexts weights two.
You can see the simple example in this link: https://stackoverflow.com/a/9590974/3098590
Related
I have create a lot of image button like this code:
TableRow tr = new TableRow(this);
if (db != null) {
if (db.moveToFirst()) {
do {
ImageButton button = new ImageButton(this);
int ID = Integer.parseInt(db.getString(db.getColumnIndex("ID")));
int resource = getResources().getIdentifier("unit" + ID, "drawable", getPackageName());
System.out.println("unit" + ID + ":" + resource);
button.setImageResource(resource);
button.setOnClickListener(MainActivity.this);
tr.addView(button);
button.getLayoutParams().height = 250;
button.getLayoutParams().width = 250;
button.setScaleType(ImageView.ScaleType.FIT_CENTER);
//button.setLayoutParams(tableRowParams);
rowcount++;
if (rowcount % 5 == 0) {
layout.addView(tr);
tr = new TableRow(this);
}
} while (db.moveToNext());
}
}
And I found code:
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(0, 0, 0, 0);
and apply in button at the do while.
button.setLayoutParams(tableRowParams);
It will show the error message "android.widget.TableLayout$LayoutParams cannot be cast to android.widget.TableRow$LayoutParams"
Replace TableLayout.LayoutParams with TableRow.LayoutParams:
TableRow.LayoutParams tableRowParams =
new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(0, 0, 0, 0);
The type of LayoutParams that you use is determined by the container of the view, not the view itself. So since your button is going inside a TableRow you need to use TableRow.LayoutParams rather than TableLayout.LayoutParams as you have it now.
Also, just so you're aware, if you need to style your TableRow or Button, rather than creating a new instance of the class and seeing the margins that way, you can inflate an XML layout using Button button = (Button) LayoutInflater.from(context).inflate(R.layout.button, tr, false); and that way you can set the margins in the XML file.
I am trying to create a table with 2 textviews, edittextview and a button programmatically. My button height is not equal to rest of the elements.
I have tried removing padding and margin but the issue persisted.
Can anyone point out the error or provide a workaround to this.
code is as follows
public void populateItems()
{
// TODO : create uniform table row cells
TableLayout tl = (TableLayout)findViewById(R.id.placeOrderTable);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity=Gravity.CENTER_HORIZONTAL;
lp.bottomMargin=5;
LayoutParams lpTextView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lpTextView.weight=1;
lpTextView.width=0;
lpTextView.bottomMargin=1;
LayoutParams lpEditTextView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpEditTextView.weight=1;
lpEditTextView.width=0;
// lpEditTextView.
//lpEditTextView.
lpEditTextView.gravity=Gravity.CENTER_HORIZONTAL;
for(final HashMap<String,String> hm:mTodayItemsHashMap)
{
TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(lp);
TextView tvItemName = new TextView(getApplicationContext());
tvItemName.setLayoutParams(lpTextView);
tvItemName.setBackgroundResource(R.drawable.border_edittext);
tvItemName.setText(hm.get("item_name").toUpperCase());
tvItemName.setEms(10);
tvItemName.setTextColor(Color.BLACK);
tvItemName.setGravity(Gravity.CENTER);
// evItemName.setHint(getString(R.string.item_name));
TextView tvItemPrice = new TextView(getApplicationContext());
tvItemPrice.setLayoutParams(lpTextView);
tvItemPrice.setBackgroundResource(R.drawable.border_edittext);
tvItemPrice.setTextColor(Color.BLACK);
tvItemPrice.setText(hm.get("item_price").toUpperCase());
tvItemPrice.setGravity(Gravity.CENTER);
tvItemPrice.setEms(10);
// evItemPrice.setHint(getString(R.string.item_price));
final EditText evItemQty = new EditText(getApplicationContext());
evItemQty.setLayoutParams(lpTextView);
evItemQty.setInputType(InputType.TYPE_CLASS_NUMBER);
evItemQty.setBackgroundResource(R.drawable.border_edittext);
evItemQty.setTextColor(Color.BLACK);
evItemQty.setGravity(Gravity.CENTER);
evItemQty.setEms(10);
evItemQty.setFilters(new InputFilter[]{ new InputFilterMinMax("1", hm.get("max_qty"))});
// evItemQty.setL
evItemQty.setHint(getString(R.string.max_orders));
Button btnAddToCart=new Button(getApplicationContext());
btnAddToCart.setLayoutParams(lpTextView);
btnAddToCart.setText("Add to Cart");
btnAddToCart.setPadding(0, 0, 0, 0);
// btnAddToCart.setLayoutParams(params);
// btnAddToCart.setBackgroundResource(R.drawable.border_edittext);
btnAddToCart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// mCart.
item=new Item();
item.setName(hm.get("item_name"));
item.setPrice(hm.get("item_price"));
String temp=evItemQty.getText().toString();
try{
Integer.parseInt(temp);
}
catch(NumberFormatException nfe){
Toast.makeText(getApplicationContext(), "Please specify quantity",Toast.LENGTH_LONG);
return;
}
item.setQty(evItemQty.getText().toString());
if(mCart==null)
mCart=new Cart();
mCart.add(item);
}
});
tr.addView(tvItemName);
tr.addView(tvItemPrice);
tr.addView(evItemQty);
tr.addView(btnAddToCart);
tl.addView(tr,tl.getChildCount()-1, tl.getLayoutParams());
}
}
You will probably want to override the background of the button. You'll get something like this:
This of course means you will not have the curved edges anymore. This can be fixed
as explained in this answer
I am trying to create phone fields programmatically in a form. How to identify it and subsequently get its values?.
The code that create the EditText fields and add to a form is this:
public void addTelephone(){
//form
TableLayout table = (TableLayout)findViewById(R.id.table);
//new row
TableRow tr = new TableRow(NewRequestActivity.this);
LayoutParams ltr = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ltr.setMargins(0, 0, 0, 2500);
tr.setLayoutParams(ltr);
//new field
EditText et = new EditText(NewRequestActivity.this);
et.setHint("telephone");
et.setInputType(InputType.TYPE_CLASS_NUMBER);
et.requestFocus();
Resources res = getResources();
float fontSize = res.getDimension(R.dimen.input_form) / getResources().getDisplayMetrics().density;
et.setTextSize(fontSize);
LayoutParams let = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
et.setLayoutParams(let);
ImageView img = new ImageView(NewRequestActivity.this);
img.setTag("img_"+counttelf);
img.setImageResource(R.drawable.more);
img.setBackgroundResource(R.drawable.imagefocused);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
v.setVisibility(View.GONE);
addTelephone();
}
});
LayoutParams limg = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
img.setLayoutParams(limg);
tr.addView(et, 0);
tr.addView(img, 1);
table.addView(tr, ++indextelf);
}
You can just setTag to your EditText
Eg:
You have phone field:
editText.setTag("phone");
And when you want to retrieve it.
Just do,
EditText ed = (EditText)findViewByTag("phone");
String text = ed.getText().toString();
set Id to each dynamically created edit text and get values with reference to their id.
To set Id:
edittext.setId(i);
To get from Id:
int i = edittext.getId();
First of all, all of my components have to be created at runtime. Well, I've got a TableLayout and inside this table view a bunch of TableRow. Inside every TableRow, there should be:
a Button at the right side having a given fixed height and a given fixed width
a TextView that fills the "rest" of the TableRow being allowed to wrap its text
The height of the TableRow should be the max of the Button height and the TableRow height. I either fail to set the Button height or only one of the elements (TableRow or Button) is displayed.
Maybe somebody could help me.
My current code:
// fetching the table layout
final TableLayout tl = (TableLayout) findViewById(R.id.ShoppingListTableLayout);
tl.removeAllViews();
// creating a single layout for every component
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.MATCH_PARENT);
tableRowParams.setMargins(0, 1, 0, 1);
TableRow.LayoutParams bParams = new TableRow.LayoutParams(m_Resources
.getDimensionPixelSize(R.dimen.ButtonWidth), m_DefaultButtonHeight_px);
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
Iterator<xy> itr = ...
while (itr.hasNext()) {
final xy = itr.next();
final TableRow tr = new TableRow(this);
tr.setLayoutParams(tableRowParams);
// 1. Child: TextView
final TextView CurrTxtView = new TextView(this);
CurrTxtView.setPadding(TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX);
CurrTxtView.setTextSize(TV_TEXT_SIZE);
CurrTxtView.setTypeface(m_Font);
CurrTxtView.setTextColor(TEXT_ACTIVE_COLOR);
CurrTxtView.setText(CurrItem.GetName());
CurrTxtView.setLayoutParams(tvParams);
tr.addView(CurrTxtView, CHILD_INDEX_TV);
// 2. Child Button ("Edit" für aktive, "Delete" für inaktive)
final Button MyButton = new Button(this);
MyButton.setTextColor(TEXT_ACTIVE_COLOR;
MyButton.setText(m_Resources.getString(CurrItem.GetIsActive() ? R.string.EditString
: R.string.DeleteString));
MyButton.setLayoutParams(bParams);
MyButton.setTypeface(m_Font);
MyButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{... }
});
tr.addView(MyButton, CHILD_INDEX_B);
tr.setPadding(m_TableRowPadding_px, 0, m_TableRowPadding_px, 0);
tr.setGravity(Gravity.CENTER_VERTICAL);
tl.addView(tr, 0);
}
In this case only the buttons are displayed and even worse on the left side!
Assuming that you add the TextView and Button at the right indexes(CHILD_INDEX_TV and CHILD_INDEX_B are 0 and 1, although you could just drop them and just add the views in the correct order) a bad thing in your layout is the LayoutParams that you set to the TextView, the view that doesn't show up:
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
First of all, the TextView is the child of a TableRow so you should set the TableRow.LayoutParams and not TableLayout.LayoutParams, secondly I don't understand why you suddenly decided to leave the TableLayout.LayoutParams and go for the android.view.ViewGroup.LayoutParams for the view's height. In the end it should be:
TableRow.LayoutParams tvParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
To push the Button to the right you could simply stretch the column with the TextView by using:
tl.setColumnStretchable(0, true);
I use this code to dynamically print the vaules from my database with a buttonClick-event.
The buttonClick-event to delete the database entry is present inside the a loop.
Here my code:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout3);
final DatabaseHandler dbpin = new DatabaseHandler(this);
// Log.d("Reading: ", "Reading all tasks..");
List<Detail> detail1 = dbpin.getAllDetail();
Button[] button=new Button[1000];
for (Detail cn : detail1) {
String log = cn.getTitle();
final int i = cn.getID();
button[i] = new Button(this);
button[i].setText("Delete");
button[i].setTextSize(10);
button[i].setId(2000 + i);
int width = 80;
int height = 60;
TextView textview = new TextView(this);
textview.setText(log);
textview.setWidth(200);
textview.setTextSize(20);
textview.setPadding( 0, 10, 0, 0);
textview.setId(2000 + i);
if (i == 0) {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button[i].setLayoutParams(rlp1);
rl.addView(button[i]);
} else {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp2.addRule(RelativeLayout.BELOW, button[i].getId() - 1);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp1.addRule(RelativeLayout.BELOW, button[i].getId() - 1);
button[i].setLayoutParams(rlp1);
rl.addView(button[i]);
}
button[i].setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), details.class);
Detail detail = new Detail();
detail.setID(i);
dbpin.deleteDetail(detail);
startActivityForResult(myIntent, 1);
}
});
}
Following the database handler code is, to retrieve all details from database using a loop:
// Getting All detail
public List<Detail> getAllDetail() {
List<Detail> detailList = new ArrayList<Detail>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_DETAIL;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Detail detail = new Detail();
detail.setID(Integer.parseInt(cursor.getString(0)));
detail.setTitle(cursor.getString(1));
detail.setDetail(cursor.getString(2));
// Adding contact to list
detailList.add(detail);
} while (cursor.moveToNext());
}
// return contact list
return detailList;
}
// Deleting single detail
public void deleteDetail(Detail detail) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_DETAIL, KEY_DETID + " = ?",
new String[] { String.valueOf(detail.getID()) });
db.close();
}
At first the layout is normal. Deleting first or the last data row doesn't cause any change, but if a row in the middle is deleted, then the layout overlaps each other.
Please give me suggestions to clear this logical error.
Ok I have understand your problem. Problem is that you are using relative layout as your parent layout in which you are adding all your child relative layouts. Now if you delete your first relative layout then it automatically align with its parent so there will be no problem.
If you delete last relative layout then also not problem occurs.
Now you have align all your relative layout form their above layout so if you delete above one it automatically aligns to its parent.
Solution is simple. Use your parent layout as linear layout so that you dont need to align relative layouts with their above layout. It will automatically arrange in a linear way....
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout3); convert this layout in linearlayout in your xml file.
This the code which can help you:
LinearLayout lp = (LinearLayout) findViewById(R.id.LinearLayout1);
// for loop start from here
RelativeLayout rl = new RelativeLayout(getApplicationContext());
RelativeLayout.LayoutParams lp_btn = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp_btn.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Button temp_button = new Button(getApplicationContext());
temp_button.setText("button");
rl.addView(temp_button, lp_btn);
TextView tv = new TextView(getApplicationContext());
tv.setText("bharat");
tv.setBackgroundColor(Color.GREEN);
RelativeLayout.LayoutParams lp_tv = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp_tv.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rl.addView(tv, lp_tv);
lp.addView(rl);
// for loop will end here
I think you should use listview for your purpose it will be better. Anyway this will also work you have to manage relativelayout array and button array for your purpose.