TableLayout gets empty after phone position is changed - android

I got problem with my TableLayout using code - adding rows dynamically.
Whenever I change position of my phone from horizontal to vertical or vertical to horizontal all the data that was inputted to my table is getting removed and table is empty. The only things that are left are column headers (TextViews) with names coded in my program.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TableLayout tableOne = (TableLayout) findViewById(R.id.dataTable1);
tableOne.setId(110);
tableOne.setBackgroundColor(Color.WHITE);
TableRow namesRow = new TableRow(MainActivity.this);
namesRow.setId(111);
namesRow.setBackgroundColor(Color.GRAY);
namesRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView productName = new TextView(MainActivity.this);
productName.setId(120);
productName.setPadding(2, 0, 40, 5);
productName.setTextSize(14);
productName.setTextColor(Color.BLACK);
productName.setText("Product Name");
namesRow.addView(productName);
TextView concentration = new TextView(MainActivity.this);
concentration.setId(130);
concentration.setPadding(0, 0, 40, 5);
concentration.setTextColor(Color.BLACK);
concentration.setTextSize(14);
concentration.setText("Concentration");
namesRow.addView(concentration);
TextView packages = new TextView(MainActivity.this);
packages.setId(140);
packages.setPadding(0, 0, 40, 5);
packages.setTextSize(14);
packages.setTextColor(Color.BLACK);
packages.setText("Packages");
namesRow.addView(packages);
TextView volume = new TextView(MainActivity.this);
volume.setId(150);
volume.setPadding(0, 0, 40, 5);
volume.setTextSize(14);
volume.setTextColor(Color.BLACK);
volume.setText("Volume");
namesRow.addView(volume);
TextView totalamount = new TextView(MainActivity.this);
totalamount.setId(160);
totalamount.setPadding(0, 0, 40, 5);
totalamount.setTextSize(14);
totalamount.setTextColor(Color.BLACK);
totalamount.setText("Total Mass");
namesRow.addView(totalamount);
TextView sacksnumber = new TextView(MainActivity.this);
sacksnumber.setId(170);
sacksnumber.setPadding(0, 0, 40, 5);
sacksnumber.setText("Number of Sacks");
sacksnumber.setTextColor(Color.BLACK);
sacksnumber.setTextSize(14);
namesRow.addView(sacksnumber);
tableOne.addView(namesRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
The above code is fine and never disappears but below code when I create each row from data from EditText is disappearing once I change position of the code. Kind of like it resets...
Button sncbtn1 = (Button) findViewById(R.id.sncbtn);
sncbtn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText dataName = (EditText) findViewById(R.id.productField);
EditText dataConc = (EditText) findViewById(R.id.concField);
EditText dataPack = (EditText) findViewById(R.id.packageField);
EditText dataVol = (EditText) findViewById(R.id.volField);
String prodName = dataName.getText().toString();
String concValue = dataConc.getText().toString();
String packValue = dataPack.getText().toString();
String volValue = dataVol.getText().toString();
double concDoub = Double.parseDouble(concValue);
double packDoub = Double.parseDouble(packValue);
double volDoub = Double.parseDouble(volValue);
double massResult = volDoub * concDoub;
double sacksResult = massResult / packDoub;
System.out.println(prodName + " | " + concValue + " | " + packValue + " | " + volValue);
TableRow dataRow = new TableRow(MainActivity.this);
dataRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
if(tableOne.getChildCount() % 2 == 0) dataRow.setBackgroundColor(Color.LTGRAY);
TextView prodTable = new TextView(MainActivity.this);
prodTable.setText(prodName);
prodTable.setTextSize(14);
prodTable.setTextColor(Color.BLACK);
prodTable.setGravity(Gravity.CENTER);
dataRow.addView(prodTable);
TextView concTable = new TextView(MainActivity.this);
concTable.setText(concValue);
concTable.setTextColor(Color.BLACK);
concTable.setTextSize(14);
concTable.setGravity(Gravity.CENTER);
dataRow.addView(concTable);
TextView packTable = new TextView(MainActivity.this);
packTable.setText(packValue);
packTable.setTextSize(14);
packTable.setTextColor(Color.BLACK);
packTable.setGravity(Gravity.CENTER);
dataRow.addView(packTable);
TextView volTable = new TextView(MainActivity.this);
volTable.setText(volValue);
volTable.setTextSize(14);
volTable.setTextColor(Color.BLACK);
volTable.setGravity(Gravity.CENTER);
dataRow.addView(volTable);
TextView massTable = new TextView(MainActivity.this);
massTable.setText(new DecimalFormat("0.00").format(massResult));
massTable.setTextSize(14);
massTable.setTextColor(Color.BLACK);
massTable.setGravity(Gravity.CENTER);
dataRow.addView(massTable);
TextView sacksTable = new TextView(MainActivity.this);
sacksTable.setText(new DecimalFormat("0.00").format(sacksResult));
sacksTable.setTextColor(Color.BLACK);
sacksTable.setGravity(Gravity.CENTER);
sacksTable.setTextSize(14);
dataRow.addView(sacksTable);
tableOne.addView(dataRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
});
Also on my LogCat I'm getting error: getExtractedText on inactive InputConnection
I have tried to rewrite app with some tutorials/research etc thinking that I might find whats wrong but after 2 days I'm done and hopefully someone can help me.

Your onCreate gets called whenever your screen orientation is changed, one way to stop that is to add this line under your relevant activity tag in your manifest file:
android:configChanges="orientation|screenSize"
This way Android OS won't recreate the UI whenever the screen is rotated and leave it up to you.

Related

Dynamic Content with wrap text not working

I want to build a dynamic table which will have TextView with some content. It could happen that the text is long, in that case, I want my text to be wrapped. But in the code given below it does not work. I tried adding layout parameters to my TextView with weight=1 does not work.
Here is my code:
table = FindViewById<TableLayout>(Resource.Id.main_table);
TableRow tr_head = new TableRow(this);
tr_head.Id = 10;
tr_head.SetBackgroundColor(Color.Gray);
TableRow.LayoutParams paramTableRow = new TableRow.LayoutParams(LayoutParams.MatchParent,
LayoutParams.WrapContent);
tr_head.LayoutParameters = paramTableRow;
LinearLayout.LayoutParams paramcell = new LinearLayout.LayoutParams(LayoutParams.WrapContent,LayoutParams.WrapContent,1.0f);
TextView label_Buyway = new TextView(this);
label_Buyway.Id=20;
label_Buyway.Text ="Buyway";
label_Buyway.SetTextColor(Color.Black);
label_Buyway.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_Buyway);
TextView label_qty = new TextView(this);
label_qty.Id=21;
label_qty.Text="Qty";
label_qty.SetTextColor(Color.Black);
label_qty.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_qty);
TextView label_price = new TextView(this);
label_price.Id = 22;
label_price.Text = "Price";
label_price.SetTextColor(Color.Black);
label_price.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_price);
TextView label_total = new TextView(this);
label_total.Id = 23;
label_total.Text = "Total";
label_total.SetTextColor(Color.Black);
label_total.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_total);
table.AddView(tr_head, new TableLayout.LayoutParams(
LayoutParams.MatchParent,
LayoutParams.WrapContent));
for (int i = 0; i < 10; i++)
{
string date = DateTime.Now.ToString();
Double weight_kg = 1.1;
TableRow tr = new TableRow(this);
tr.Id=(100 + i);
tr.LayoutParameters = paramTableRow;
TextView labelBuyway = new TextView(this);
labelBuyway.Id=(200 + i);
labelBuyway.Text =date;
labelBuyway.SetTextColor(Color.Black);
labelBuyway.SetBackgroundResource(Resource.Drawable.table_row);
labelBuyway.LayoutParameters = paramcell;
tr.AddView(labelBuyway);
TextView labelqty = new TextView(this);
labelqty.Id=(200 + i);
labelqty.Text=(weight_kg.ToString());
labelqty.SetTextColor(Color.Black);
labelqty.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelqty);
TextView labelPrice = new TextView(this);
labelPrice.Id = (200 + i);
labelPrice.Text = (weight_kg.ToString());
labelPrice.SetTextColor(Color.Black);
labelPrice.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelPrice);
TextView labelTotal = new TextView(this);
labelTotal.Id = (200 + i);
labelTotal.Text = (weight_kg.ToString());
labelTotal.SetTextColor(Color.Black);
labelTotal.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelTotal);
table.AddView(tr, new TableLayout.LayoutParams(
LayoutParams.MatchParent,
LayoutParams.WrapContent));
i++;
}
Right now the text gets cut and does not wrap. Any help would be appreciated.
What I guess is these three lines would do the trick :
textViewName.HorizontalScrollBarEnabled = false;
textViewName.SetMinLines(2);
textViewName.InputType = Android.Text.InputTypes.TextFlagMultiLine;
with the weight property set to 1 and these three properties, it should work.
Please revert whether it works.
give your textviews with weight=1 and Wrap content. SO it will expand according to the text size.

How to set Id's for list of EditText within the dynamically created TableLayout in android

I am programmatically creating a dynamic TableLayout with a mixture of TextView and EditText. This TextViews are fetched from the sqlite database of application. The last column of the TableLayout consists of the EditText whose state is set to Invisible. I want to set the state of the EditText of the particular row of TableLayout to Visible based on the onClick over a row of TableLayout. After onClick, EditText should be visible and I want to store the entry made in that editText within the database.
My code of attempt made for this is below:
List<Details> customerDetailList = db.getAllCustomers(routeId);
Details customerDetails;
TableLayout stk = (TableLayout) findViewById(R.id.tableLayout1);
stk.removeAllViews();
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText("Sl.No.");
tv0.setTextColor(Color.BLACK);
tv0.setTypeface(null, Typeface.BOLD);
//tv0.setTextSize(20);
tv0.setPadding(5, 5, 5, 5);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Patron ID ");
tv1.setTextColor(Color.BLACK);
tv1.setTypeface(null, Typeface.BOLD);
//tv1.setTextSize(20);
tv1.setPadding(5, 5, 5, 5);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Name ");
tv2.setTypeface(null, Typeface.BOLD);
//tv2.setTextSize(20);
tv2.setTextColor(Color.BLACK);
tv2.setPadding(5, 5, 5, 5);
tv2.setGravity(Gravity.CENTER);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" Requested ");
tv3.setTextColor(Color.BLACK);
tv3.setTypeface(null, Typeface.BOLD);
//tv3.setTextSize(20);
tv3.setPadding(5, 5, 5, 5);
tv3.setGravity(Gravity.CENTER);
tbrow0.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setText(" Supplied ");
tv4.setTypeface(null, Typeface.BOLD);
//tv4.setTextSize(20);
tv4.setTextColor(Color.BLACK);
tv4.setPadding(5, 5, 5, 5);
tbrow0.addView(tv4);
tbrow0.setBackgroundResource(R.color.colorLightGray);
stk.addView(tbrow0);
for (int row = 0, j = 1; row < customerDetailList.size(); row++, j++) {
customerDetails = customerDetailList.get(row);
TableRow tbrow = new TableRow(this);
tbrow.setClickable(true);
final TextView t1v = new TextView(this);
t1v.setText("" + j);
t1v.setTextColor(Color.BLACK);
t1v.setGravity(Gravity.CENTER);
t1v.setPadding(5, 5, 5, 5);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText("" + customerDetails.getcId());
t2v.setTextColor(Color.BLACK);
t2v.setGravity(Gravity.CENTER);
t2v.setPadding(5, 5, 5, 5);
tbrow.addView(t2v);
TextView t3v = new TextView(this);
t3v.setText("" + customerDetails.getName());
t3v.setTextColor(Color.BLACK);
t3v.setGravity(Gravity.CENTER);
t3v.setPadding(5, 5, 5, 5);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setText("" + customerDetails.getRequested());
t4v.setTextColor(Color.BLACK);
t4v.setGravity(Gravity.CENTER);
t4v.setPadding(5, 5, 5, 5);
tbrow.addView(t4v);
final EditText t5v = new EditText(this);
// t5v.setHint(0);
t5v.setId(j);
t5v.setVisibility(View.INVISIBLE);
t5v.setTextColor(Color.BLACK);
t5v.setGravity(Gravity.CENTER);
t5v.setPadding(5, 5, 5, 5);
tbrow.addView(t5v);
tbrow.setBackgroundResource(R.color.colorWhite);
tbrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TableRow tr1=(TableRow)v;
TextView tv1= (TextView)tr1.getChildAt(0);
//Get the integer value of tv1 to make the corresponding edittext to be visible
t5v.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(),tv1.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
stk.addView(tbrow);
}
I found similar kind of questions being asked in How to Set Id in EditText programmatically in Android
and Android: Set Edit text or text view id Programmatically wherein, solution provided to make use of editText.setId(i) where i is any integer value. but it did not help me with my situation.
You can create a custom id in an xml file and set that id using setId() method. Here are the steps to do that:
1) Create the file ids.xml under res/values directory.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="my_custom_id" type="id" />
</resources>
2) Then create your view programmatically and set its id:
EditText et = new EditText(getContext());
et.setId(R.id.my_custom_id);
That should do the trick.
EDIT: You can also do the following if you do not know the number of views to be created and cannot use id's from xml file:
et.setId(View.generateViewId());
The generateViewId() method creates a random unique id for you.
I followed the below steps to set the Id's for the programmatically created EditText and to retrieve data entered in the edittext.
Step 1: Make global declaration for edittext and int variable as
int idOfEditText;
EditText t5v;
Step 2: Setup the Id for editText as
t5v = new EditText(this);
t5v.setId(Integer.valueOf(j)); //I tried with just j being declared as t5v.setId(j); but it was showing up error with the suggestion as Expected resource of type id in android studio. I have made int variable **j** to loop till list.size()
Step 3: Get the id of the row onclick over the table using
tbrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TableRow tr1=(TableRow)v;
TextView tv1= (TextView)tr1.getChildAt(0);
//Get the integer value of tv1 to make the corresponding edittext to be visible
idOfEditText = Integer.valueOf(tv1.getText().toString());
t5v = (EditText) findViewById(idOfEditText);
}
});
Step 4: The data entered in the EditText can be retrieved by
t5v = (EditText) findViewById(idOfEditText);
String value = t5v.getText().toString();
Toast.makeText(this, "You entered: "+value, Toast.LENGTH_SHORT).show();

How to show textview left alignment in android

In my application i want to show VIEW ALL to left align like below image:-
But I found this type of layout and more thing i also do all these layout programmatically.
My SourceCode:-
private void printFrontCategory(){
for(int i=0;i<main.size();i++) {
View v = new View(MainActivity.this);
v.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT, 5));
v.setBackgroundColor(Color.rgb(51, 51, 51));
/* View v1 = new View(MainActivity.this);
v1.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT,5));
v1.setBackgroundColor(Color.rgb(255, 255, 255));*/
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(MainActivity.this);
horizontalScrollView.setHorizontalScrollBarEnabled(false);
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
textView.setGravity(Gravity.RIGHT);
// textView.setGravity(Gravity.CENTER);
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
textView2.setGravity(Gravity.LEFT);
LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
linearLayout2.addView(textView,0);
linearLayout2.addView(textView2,1);
LinearLayout linearLayout = new LinearLayout(MainActivity.this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int j = 0; j < main.get(i).size(); j++) {
LinearLayout linearLayout1 = new LinearLayout(MainActivity.this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
ImageView image = new ImageView(MainActivity.this);
TextView nameProduct = new TextView(MainActivity.this);
TextView priceProduct = new TextView(MainActivity.this);
TextView special_discount = new TextView(MainActivity.this);
/* Log.d("counter val",cnt+"");
Log.d("thumb ",front_category_thumb.size()+"");
Log.d("image", front_category_thumb.get(52));*/
new SetImageView(image).execute(main.get(i).get(j).get(1));
nameProduct.setText(main.get(i).get(j).get(2));
if (!String.valueOf(main.get(i).get(j).get(5)).equals(null)) {
priceProduct.setText(main.get(i).get(j).get(3));
special_discount.setText(main.get(i).get(j).get(5));
priceProduct.setPaintFlags(nameProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
priceProduct.setGravity(Gravity.CENTER);
special_discount.setGravity(Gravity.CENTER);
nameProduct.setGravity(Gravity.CENTER);
linearLayout1.addView(image);
linearLayout1.addView(nameProduct);
linearLayout1.addView(priceProduct);
linearLayout1.addView(special_discount);
} else if (!String.valueOf(main.get(i).get(j).get(4)).equals(null)) {
priceProduct.setText(main.get(i).get(j).get(3));
special_discount.setText(main.get(i).get(j).get(4));
priceProduct.setPaintFlags(nameProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
priceProduct.setGravity(Gravity.CENTER);
special_discount.setGravity(Gravity.CENTER);
nameProduct.setGravity(Gravity.CENTER);
linearLayout1.addView(image);
linearLayout1.addView(nameProduct);
linearLayout1.addView(priceProduct);
linearLayout1.addView(special_discount);
} else {
priceProduct.setText(main.get(i).get(j).get(3));
priceProduct.setGravity(Gravity.CENTER);
nameProduct.setGravity(Gravity.CENTER);
linearLayout1.addView(image);
linearLayout1.addView(nameProduct);
linearLayout1.addView(priceProduct);
}
linearLayout.addView(linearLayout1, j);
}
horizontalScrollView.addView(linearLayout);
// linearLayoutmens.addView(textView);
// linearLayoutmens.addView(v1);
linearLayoutmens.addView(linearLayout2);
linearLayoutmens.addView(horizontalScrollView);
linearLayoutmens.addView(v);
}
}
I am new in android programming. Please help me!
If you haven't found solution yet, then you can try the following
textView.setGravity(Gravity.RIGHT); This basically act as Gravity for text within TextView for more info refer So basically it is counter part of android:Gravity. Hence we need LayoutParam
Your problem it can be solved in two ways using LinearLayout and RelativeLayout change the below code from
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
textView.setGravity(Gravity.RIGHT);
// textView.setGravity(Gravity.CENTER);
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
textView2.setGravity(Gravity.LEFT);
LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
linearLayout2.addView(textView,0);
linearLayout2.addView(textView2,1);
Solution1 Using LinearLayout
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
textView.setGravity(Gravity.START); //Gravity.Left also works
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
textView2.setGravity(Gravity.END); //Gravity.Right also works
LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams linearLayoutParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,2);
linearLayout2.setLayoutParams(linearLayoutParam);
LinearLayout.LayoutParams viewParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,1);
linearLayout2.addView(textView, 0, viewParam);
linearLayout2.addView(textView2, 1, viewParam);
In above solution I have assigned weightSum = 2 to LinearLayout and weight=1 to each TextView.
Solution2 Using RelativeLayout
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
//textView.setGravity(Gravity.START); //Gravity.Left also works
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
//textView2.setGravity(Gravity.END); //Gravity.Right also works
RelativeLayout relativeLayout2 = new RelativeLayout(MainActivity.this);
RelativeLayout.LayoutParams relativeLayoutParam1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParam1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
relativeLayout2.addView(textView, 0, relativeLayoutParam1);
RelativeLayout.LayoutParams relativeLayoutParam2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParam2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relativeLayout2.addView(textView2, 1, relativeLayoutParam2);
In this case textView.setGravity(Gravity.RIGHT); is optional.
Update Based on comment
If you want underlined text as in SAMPLE below then check one of the answer here
<u>Sample</u>
But if you want a black line then it is view and you need to add it after adding linearlayout2 or relativelayout2 depending upon the solution in the main view.

How to create programmatically EditText and then get its value

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

Adding View to TableLayout Takes Up Whole Screen

I am simply trying to add a separator line under my header row in a table layout, but no matter what i set the width/height to in layoutparams, the view takes up the whole page.
TableLayout tl = (TableLayout)FindViewById(Resource.Id.counted);
TableRow row = new TableRow(this);
TextView itemNumber = new TextView(this);
TextView itemDesc = new TextView(this);
TextView quantity = new TextView(this);
itemNumber.Text = "Item Number";
var inlo = new TableRow.LayoutParams(1);
inlo.SetMargins(10, 10, 30, 10);
itemNumber.LayoutParameters = inlo;
itemDesc.Text = "Item Description";
var idlo = new TableRow.LayoutParams(2);
idlo.SetMargins(10, 10, 30, 10);
itemDesc.LayoutParameters = inlo;
quantity.Text = "Quantity";
var qlo = new TableRow.LayoutParams(3);
qlo.SetMargins(10, 10, 30, 10);
quantity.LayoutParameters = qlo;
View v = new View(this);
v.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.FillParent | 0);
v.SetBackgroundColor(Android.Graphics.Color.White);
row.AddView(itemNumber);
row.AddView(itemDesc);
row.AddView(quantity);
tl.AddView(row);
tl.AddView(v);
v and its white background takes up the whole screen below row. ????
Just decided to use Strings to underline my column headers rather than trying to put a line under the first row.

Categories

Resources