I have code in eclipse for create multiple TextView in multiple rows but when run the application items don't show, please help me how to fix it.
This is my code:
public void createInputBoxes(Activity gameplay, int colnums, int rownums, TableLayout.LayoutParams lparams, TableLayout puzzlelayout) {
TextView[][] puzcels = new TextView[colnums][];
puzzlelayout.removeAllViews();
for(int c=0; c<colnums; c++)
{
puzcels[c]=new TextView[rownums];
TableRow tr = new TableRow(gameplay);
tr.setLayoutParams(lparams);
for(int r=0; r<rownums; r++)
{
puzcels[c][r] = new TextView(gameplay);
puzcels[c][r].setLayoutParams( lparams);
puzcels[c][r].setId(tvid);
puzcels[c][r].setText("?");
tvid++;
puzcels[c][r].setBackgroundColor(0xffffb90f);
tr.addView(puzcels[c][r]);
}
puzzlelayout.addView(tr,lparams);
}
}
You are using TableLayout.LayoutParams params for all of your views - TableLayout, TableRow and TextView. That won't work for those last two. You need to give them params that work for them:
Dynamically filling a Table Layout with Table Rows
Related
Edit: as Blumer pointed out, I was not adding the items to the table, so that this question appeared just because I was careless and I didn't see my mistake.
I am trying to create a dynamic TableLayout, as I have to receive results from the server and add rows based on the results, but the table is not updating. (Also, the TableLayout already has 1 initial row, the header row).
This is my code:
Room[] rooms = State.rooms;
TableLayout tblBookDetails = (TableLayout) findViewById(R.id.tblbookdetails);
for(int i = 0; i < rooms.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LayoutParams layout_wrapwrap = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout_wrapwrap.rightMargin = 10; //TODO: Convert into DP
Resources res = getResources();
TextView txt1 = new TextView(this);
txt1.setLayoutParams(layout_wrapwrap);
txt1.setTextColor(res.getColor(android.R.color.black));
txt1.setText(rooms[i].name);
TextView txt2 = new TextView(this);
txt2.setLayoutParams(layout_wrapwrap);
txt2.setTextColor(getResources().getColor(android.R.color.black));
txt2.setText(rooms[i].price + " " + rooms[i].currency);
EditText edit1 = new EditText(this);
edit1.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit1.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit1.setEms(3);
edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
EditText edit2 = new EditText(this);
edit2.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit2.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit2.setEms(3);
edit2.setInputType(InputType.TYPE_CLASS_NUMBER);
Spinner spinner = new Spinner(this);
layout_wrapwrap.rightMargin = 0;
spinner.setLayoutParams(layout_wrapwrap);
Integer[] numbers = new Integer[rooms[i].count];
for(int j = 0; j < numbers.length; j++) {
numbers[j] = i + 1;
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(
BookActivity.this, R.layout.spinner_textview, numbers);
spinner.setAdapter(adapter);
tblBookDetails.addView(tr);
}
//Another exquisite beauty of Java.
Log.d("USR", Integer.valueOf(tblBookDetails.getChildCount()).toString());
tblBookDetails.invalidate();
tblBookDetails.refreshDrawableState();
To prevent any confusion, the Room[] array is just a simple property-holder class.
This code looks enormously convoluted, and the table is not updating. I've searched quite a bit on the Internet, and I could not find any solution for this problem.
Thank you in advance.
I see where you add tr to tblBookDetails, but I don't see anywhere where you put txt1, txt2, edit1, etc. in tr. Try adding those views to the row, and I think that should get you there, because right now you appear to be adding the TableRow, but there's nothing in it.
I'm still new in android programming, what I'm trying to do is, I want to create 5x5 dimension TableLayout. I know this can be done by using GridView BaseAdapter suing Inflate service. But for this one i try to apply using table layout. Below is the code. I create new instance of TableLayout, and new instance of Table row. On each table row, I created instance of 5 TextView. But once I open in emulator or in my phone, there is no TableRow created, it just empty blank Table Layout. Also there is no exception was thrown.
GridView navIcon = (GridView) findViewById(R.id.content);
navIcon.setAdapter(new ImageAdapter(this));
navIcon.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
// TODO Auto-generated method stub
if (position==0){
try{
TableLayout calgrid = (TableLayout) findViewById(R.id.gridtable);
Context ctxt = v.getContext();
TextView[] tView = new TextView[25];
calgrid = new TableLayout(ctxt);
int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(float) 1, getResources().getDisplayMetrics());
int counter=1;
TableRow[] tr = new TableRow[5];
for(int j=0;j<5;j++){
tr[j] = new TableRow(ctxt);
for(int i=0;i<5;i++){
tView[i] = new TextView(ctxt);
tView[i].setText(String.valueOf(counter));
tView[i].setTextSize(15);
counter+=1;
tView[i].setWidth(50 * dip);
tView[i].setPadding(20*dip, 0, 0, 0);
tView[i].setTextColor(Color.rgb( 100, 200, 200));
Toast.makeText(getApplicationContext(), "tView["+i+"] value " + String.valueOf(tView[i].getText()), Toast.LENGTH_SHORT).show();
tr[j].addView(tView[i], 50, 50);
}
calgrid.addView(tr[j]);
}
}catch(Exception e){
Log.e("MainActivity", "Error in activity", e);
Toast.makeText(getApplicationContext(),e.getClass().getName() + " " + e.getMessage(),Toast.LENGTH_LONG).show();
}
}
}
});
First of all, it is a mistake to do calgrid = (TableLayout) findViewById(R.id.gridtable); and then calgrid = new TableLayout(ctxt);, which basically says find this view now assign this variable to something completely different. Remove the second statement and it will load the table from xml which is what you want.
Second, I think it would be a good idea to simplify things for yourself because there is a lot going on here. Instead of doing all this work inside an onClick listener, do it in the onCreate method itself. Also, you seem to be using the Context from the GridView, which seems odd. Perhaps if you posted your xml layout file it could help explain what you are trying to do?
There is also a problem with indices in the array of TextViews, as tView[i] will only assign items up to 5 but the array contains 25 items. Try using tView[(j*5)+i] instead. I don't think this is causing your problems but just make sure you are assigning your items correctly.
Here is an example of how to do something along the lines of what you want
setContentView(R.layout.grid);
TableLayout tl = (TableLayout) findViewById(R.id.gridtable);
for (int j = 0; j < 5; j++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
for (int i = 0; i < 5; i++) {
TextView tView = new TextView(this);
tView.setText("TEXT" + String.valueOf((j * 5) + i + 1));
tView.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr.addView(tView);
}
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
and grid.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridtable"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
Once you get it working in the activity itself you can try to put it inside a listener attached to a GridView. Hope this helps!
I have multiple dynamic editTextBox. I want to know how can I store all its edit textbox value?
what should I write in all edit box that should be I want a store on button click event.
My code is below:
for(i = 0 ;i < 6; i++) {
TableRow tr = new TableRow(this);
TableRow tr1 = new TableRow(this);
TextView tv1 = new TextView(this);
editText1 = new EditText(this);
editText1.setWidth(300);
tr.addView(tv1);
tbl.addView(tr);
tr1.addView(editText1);
tbl.addView(tr1);
}
Here is the code.
EditText[] editText1 = new EditText[6];
for(i = 0 ;i < 6; i++) {
TableRow tr = new TableRow(this);
TableRow tr1 = new TableRow(this);
TextView tv1 = new TextView(this);
editText1[i] = new EditText(this);
editText1[i].setWidth(300);
tr.addView(tv1);
tbl.addView(tr);
tr1.addView(editText1[i]);
tbl.addView(tr1);
}
public void onClick(View v)
{
for(int i=0;i<6;i++)
{
Log.i("----value of EditText:"+i,"."+editText1[i].getText().toString());
}
}
A direct answer to your question would be:
remember your textviews when you create them (add them to an ArrayList for example)
the onPause() method of your activity is the place to retain any information you want available later (android framework can finish() your activity anytime when it's paused).
in an overrided onPause() method, cycle through your ArrayList<TextView>, retrieve each TextView content and store them for later use. (If you have a fixed number of TextViews, you may consider using SharedPreferences, but take a look at the remainder of this article to see if something better suits your needs.
You can then use onCreate() or onResume() method to repopulate your TextViews before your application shows up.
An indirect, possibly better, answer would be to separate data from UI by using a data management class that would handle saving restoring and modifying your data and a customized Adapter which would be responsible of filling the TextViews. This way you can easily change the later to address different widgets.
I wrote these codes without using editor, check it before using
String sValues="";
for (int i = 0; i < tbl.getChildCount(); i++) {
TableRow row = (TableRow)tbl.getChildAt(i);
for (int j = 0; j < row.getChildCount(); j++) {
if(row.getChildAt(j) instanceof EditText){
EditText editText1 = (EditText)row.getChildAt(j);
sValues += editText1.getText().toString();
}
}
}
I am trying to add a tableLayout at runtime to the existing LinearLayout in main.xml.
I have added a editText(R.id.editText1) in the main.xml. Here's my code. Its not working. I get a runtime error (The application has stopped unexpectedly).
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text = (EditText)findViewById(R.id.editText1);
TableLayout tblLayout = new TableLayout(this);
tblLayout.setLayoutParams(new TableLayout.LayoutParams(8,5));
tblLayout.setPadding(1,1,1,1);
for(int r=0; r<ROW_COUNT; ++r)
{
TableRow tr = new TableRow(this);
for(int c=0; c<COL_COUNT; ++c)
{
int index = r * COL_COUNT + c;
buttonList.add(new Button(this));
buttonList.get(index).setText(buttonNames[index]);
tr.addView(buttonList.get(index), 60, 30);
}
tblLayout.addView(tr);
}
LinearLayout mainLayout = (LinearLayout)findViewById(R.layout.main);
mainLayout.addView(tblLayout);
setContentView(mainLayout);
}
Any pointers would be greatly appreciated. Thanks.
Before your loop add :
Arraylist<TableRow> tr = new ArrayList<TableRow>;
In the loop instead of :
TableRow tr = new TableRow(this);
Put :
tr.add(new TableRow(this));
And finally replace your tr by tr.get(r). Something like these modifications should help you because, you erase your tr at each turn of your loop with your :
TableRow tr = new TableRow(this);
Mr. Happy Go Lucky just do one thing write setContentView(R.layout.your Main Layout); beofre text = (EditText)findViewById(R.id.editText1);
means at top of the onCreate() method because we cant define any view before setContentView.
without any setContentView you cant find any view like as findViewById(R.id.editText1);
Below line will be the first one after super.onCreate(savedInstanceState);
setContentView(mainLayout);
then only findViewByid works
I'm trying to print the value of the TextViews which are inside TableRows, using TableLayout#getChildAt(i).getChildAt(j).
When I try to log it using the above, logcat complains, saying that it's a View object and that it doesn't have the method I'm trying to use (getText()).
The only Views in the TableRows are TextViews.
// List<TextView> textViewBoxes...
private void createViews() {
...
tblLayout = new TableLayout(this);
tblRow01 = new TableRow(this);
...
for (int i = 0; i < 99; i++) {
TextView text = new TextView(this);
text.setText("Player " + i);
textViewBoxes.add(text);
}
tblRow01.addView(textViewBoxes.get(0));
...
tblLayout.addView(tblRow01);
...
// Print the contents of the first row's first TextView
Log.d(TAG, ("row1_tv1: " +
tblLayout.getChildAt(0).getChildAt(0).getText().toString));
...
}
Have you tried something like this?
TableRow row = (TableRow)tblLayout.getChildAt(0);
TextView textView = (TextView)row.getChildAt(XXX);
// blah blah textView.getText();
You can also do it in one line, but it sometimes looks ugly:
// wtf?
((TextView)((TableRow)tblLayout.getChildAt(0)).getChildAt(XXX)).getText();
Anyway... what you are doing here is casting the View to the specific type you want. You can do that without problems, since you are completely sure that each TableLayout's child is a TableRow, and you know that TableRow's children on XXX position is a TextView.