Android - Adding layout at runtime to main layout - android

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

Related

create multiple runtime textview in android

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

Get Text from Multiple EditText with Button in Android

My problem is creating activity to enter an array manually in Android. So I tried to create multiple EditText by these code lines:
public void EnterArray(int n)
{
for(int i=0; i<n; i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
EditText txt = new EditText(this);
txt.setId(i);
layout.addView(txt, params);
}
}
The remain problem is how can I get the text from them by using the Save Button I create in the layout. Is there any resolve? I hope there's someone could help with it.
Thank you in advance ^^
You can try something like this.
int count =layout.getChildCount();
for(int i=0;i<count;i++)
{
EditText text=(EditText)layout.getChildAt(i);
String value= text.getText().toString()
}

Dynamically Update TableLayout

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.

Android Table Layout with 5x5 dimension

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!

Extending TableLayout class - adding rows

I am trying to extend the TableLayout class, such that rows will be populated automatically by the class. The issue I am having is the rows that I add inside the custom class do not display.
Ex. within the Activity:
private TextView getTableCell(String text) {
TextView tv = new TextView(this);
tv.setText(text);
tv.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
return tv;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatasetTableLayout table = (DatasetTableLayout) findViewById(R.id.table);
TableRow tr = new TableRow(this);
tr.addView(getTableCell("activity1"));
tr.addView(getTableCell("activity2"));
tr.addView(getTableCell("activity3"));
table.addView(tr);
This successfully adds a row to the table. However, within my custom class:
private TextView getTableCell(String text) {
TextView tv = new TextView(context);
tv.setText(text);
tv.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return tv;
}
private void update() {
TableRow tr = new TableRow(context);
tr.addView(getTableCell("class1"));
tr.addView(getTableCell("class2"));
tr.addView(getTableCell("class3"));
addView(tr);
}
does not successfully add a row. Well, it does add a row, as getChildCount(), and getChildAt() do return this row - but it does not get displayed.
Am I missing something?
#theresia (Can't seem to format text if I add a comment):
It does appear that it adds the TableRow:
for(int i = 0; i < getChildCount(); i++) {
TableRow row = (TableRow) getChildAt(i);
for(int j = 0; j < row.getChildCount(); j++) {
TextView tv = (TextView) row.getChildAt(j);
Log.e("DatasetTableLayout-data", tv.getText().toString() + " ");
}
}
gives me:
01-24 11:03:31.668: ERROR/DatasetTableLayout-data(1624): activity1
01-24 11:03:31.677: ERROR/DatasetTableLayout-data(1624): activity2
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): activity3
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class1
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class2
01-24 11:03:31.727: ERROR/DatasetTableLayout-data(1624): class3
My guess would be something along this line: addView(tr); Does it successfully add your TableRow to TableLayout?
Edit:
TableLayout tb = new TableLayout(this);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("row");
tr.addView(tv);
tb.addView(tr); // this is what I mean
setContentView(tb);
Your code for creating rows works fine, but if you haven't add it to the table, and later on, add the table to the parent view as well, your rows wouldn't get displayed.

Categories

Resources