I passed a EditText view to a table row addView() method. like this:
myTablerow.addView(new EditText (this))
Q1: How do I set properties like size, colour for the EditText etc
Q2: How do I get the input value from the EditText?
Full code from the author: (P.s. should update in the question instead of my answer)
(It looks like your post is mostly code; please add some more details.)
if (cursor.getCount() > 0) {
// looping through all rows and adding to list
for (int i = 0; i < numberOfRows; i++)
{
TableRow tr = new TableRow(this);
TableRow trheader = new TableRow(this);
if (i == 0)
trheader.setBackgroundColor(Color.parseColor("#344C58"));
else
tr.setBackgroundColor(Color.parseColor("#203F50"));
for (int j = 0; j < numberOfColumns; j++)
{
txtGeneric = new TextView(this);
TextView txtGenericHeader = new TextView(this);
//txtGenericHeader.setGravity(Gravity.LEFT);
if (i == 0) {
txtGeneric.setTextColor(Color.parseColor("#ffffff"));
for (int z = 0; z < numberOfColumns; z++) {
txtGenericHeader.setTextSize(12);
txtGenericHeader.setAllCaps(false);
//txtGeneric.setBackgroundColor(Color.parseColor("#768F9E"));
txtGenericHeader.setTextColor(Color.parseColor("#768F9E"));
txtGenericHeader.setText(cursor.getColumnName(j));
txtGenericHeader.setGravity(Gravity.CENTER_HORIZONTAL);
}
trheader.addView(txtGenericHeader);
}
txtGeneric.setTextSize(12);
txtGeneric.setTextColor(Color.parseColor("#768F9E"));
txtGeneric.setText(mArrayList.get(counter++));
txtGeneric.setGravity(Gravity.CENTER_HORIZONTAL);
tr.addView(txtGeneric);
}
tr.addView(new EditText(this));
tr.addView(new EditText(this));
table.addView(trheader);
table.addView(tr);
cursor.moveToNext();
}
db.close();
sv.addView(table);
lay.addView(sv);
}//EOF PARENT IF
You can do this for setting the properties.
EditText edtText = new EditText(this);
edtText.setTextColor(someColor);
edtText.setTextSize(someSize);
myTablerow.addView(edtText);
And for getting the value from EditText you can use.
String edtTextValue = edtText.getText().toString();
you may refer to this
Q1: #setTextColor() , #setTextSize()
Q2: #getText()
I have an app in which I am showing data from JSON. I am displaying data in a dynamic textview on the right and left side of the relative layout. Now I want to add this layout in an existing layout so that I can apply an OnClickListener on the textview. Right now I am getting data into a string and then setting that string into static textviews in the left and right side of the layout.
How would it be possible to generate textview dynamically on the basis of number of data I am getting from JSON ?
for (Region object : temp.phonelist.regionList)
{
if (object.getCCInfoShortDesc() != null || !(object.getCCInfoShortDesc().equals(null)))
{
Log.i("nullexception", "nullexception");
holder.tvDescription.setText(object.getCCInfoShortDesc());
holder.tvDescription.setVisibility(View.VISIBLE);
}else {
Log.i("nullexception1", "nullexception1");
holder.tvDescription.setVisibility(View.GONE);
}
leftContent += object.getCCInfoLeft() + ":" + "\n";
rightContent += object.getCCInfoRight() + "\n";
}
Log.i("lefftcontent", leftContent);
Log.i("rightcontent", rightContent);
if (leftContent != null) {
holder.tvData2.setText(leftContent);
holder.tvData2.setVisibility(View.VISIBLE);
}
if (rightContent != null) {
holder.tvData1.setText(rightContent);
holder.tvData1.setVisibility(View.VISIBLE);
}
You can do it in this way..
final int Count = < Number of TextViews>; // total number of textviews to add
final TextView[] TextViewsARR = new TextView[N]; // create an empty array;
for (int i = 0; i < Count; i++) {
// create a new textview
final TextView rowTextView = new TextView(this);
// set some properties of rowTextView or something
rowTextView.setText("This is row #" + i);
// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);
// save a reference to the textview for later
TextViewsARR [i] = rowTextView;
}
I have a sample below that generates a checkbox dynamically, If you
observe i am generating the checkbox based on the cursor count.
You can adapt this saple to your needs
Instead of checkbox use a texview
Give any layout like linear, relative etc and generate views
dynamically
private CheckBox chkBoxMealType[] = null;
mCursor = db.rawQuery("SELECT meal_type_id,meal_type_name FROM meal_type_mas", null);
if(mCursor.moveToFirst()){
do{
if(chkBoxMealTypeCnt==0){
chkBoxMealType=new CheckBox[mCursor.getCount()];
}
//create a general view for checkbox
chkBoxMealType[chkBoxMealTypeCnt]= new CheckBox(getActivity());
//Create params for veg-checkbox
//Reason:: we don't need to worry about the data exist in cuisine_type_mas table
chkBoxMealType[chkBoxMealTypeCnt].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
chkBoxMealType[chkBoxMealTypeCnt].setTextColor(getResources().getColor(R.color.searchGoldLight));
chkBoxMealType[chkBoxMealTypeCnt].setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
chkBoxMealType[chkBoxMealTypeCnt].setTag(mCursor.getString(mCursor.getColumnIndexOrThrow(meal_type_mas.COLUMN_MEAL_TYPE_ID)));
chkBoxMealType[chkBoxMealTypeCnt].setText(WordUtils.capitalizeFully(mCursor.getString(mCursor.getColumnIndexOrThrow(meal_type_mas.COLUMN_MEAL_TYPE_NAME))));
mealTypeContainer.addView(chkBoxMealType[chkBoxMealTypeCnt]);
//since cursor count starts from 0 last count must be allowed
chkBoxMealTypeCnt++;
}while(mCursor.moveToNext());
Log.d("", "");
}
I have another sample..... Download this project(Click Here) and run in your editor
Snapshot::
Firstly you need to add a View in your layout ... Like you may try using LinearLayout or HorizontalLayout ... and then attach/add your dynamic textview to that layout.
Pragmatically you may try like this
packageButtons = new ArrayList<TextView>(); // Create your textview arraylist like this
for(int a = 0; a < your_text_view_from_json.size(); a++){
final TextView rowTextView;
rowTextView = new TextView(getApplicationContext());
rowTextView.setText(taxi_type_spin.get(a).taxi_type);
rowTextView.setTextSize(15);
rowTextView.setTextColor(getResources().getColor(R.color.white));
LinearLayout.LayoutParams lparam = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
//rowTextView.setPadding(0, 0, 0, 0);
packageButtons.add(rowTextView);
rowTextView.setLayoutParams(lparam);
rowTextView.setId(a);
final int b = a;
// get value of clicked item over here ..
rowTextView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button btn = (Button)v;
String get_value = btn.getText().toString();
//Toast.makeText(getApplicationContext(), "Button name is : " + get_value + " AND ID IS : " + rowTextView.getId(), Toast.LENGTH_LONG).show();
if(taxi_type_spin.get(b).taxi_type.equalsIgnoreCase(Utils.Hourly_Package))
{
setTaxiType(rowTextView.getId(),true);
ll_spin.setVisibility(View.VISIBLE);
}
else
{
setTaxiType(rowTextView.getId(),false);
ll_spin.setVisibility(View.GONE);
}
setSelectedButtonColor(b);
}
});
// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);
NOTE rowTextView .. this is your default view attached to your XML file
Hope it helps!
private void setLayout(LinearLayout llayout,
final ArrayList<String> items) {
for (int i = 0; i < items.size(); i++) {
LinearLayout row = null;
LayoutInflater li = getLayoutInflater();
row = (LinearLayout) li.inflate(R.layout.custom_item,
null);
ImageView image = (ImageView) row.findViewById(R.id.image);
llayout.addView(row);
}
}
I would suggest you to use ArrayList, because the class java.util.ArrayList provides resizable-array, which means that items can be added and removed from the list dynamically.
and get value from ArrayList something like this:
for(int i=0; i<arrayList.size(); i++)
{
textName.setText(object.getName());
}
How it is possible to genrate textview dynamically
on the basis of number of data i am getting from json.
You need to create TextView and add it to the parent layout each time you iterate on the forloop. So you will have textView for each of the element of the temp.phonelist.regionList
sample:
for (Region object : temp.phonelist.regionList)
{
TextView tx = new TextView(context); //creating a new instance if textview
//yourstuff goes here
tx.setText(text_you_want);
yourView.addView(tx); //this is to add the textView on each iteration
}
here is your solution do this way,Take one Layout(Linear or Relative) and add control dynamically....
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < 4; i++)
{
TextView txtDemo = new TextView(getActivity());
txtDemo .setTextSize(16);
txtDemo .setLayoutParams(lp);
txtDemo .setId(i);
lp.setMargins(0, 10, 0, 0);
txtDemo .setPadding(20, 10, 10, 10);
txtDemo .setText("Text View"+ i);
linearlayout.addView(txtDemo );
}
}
I'm a noob to android development and I am having trouble deleting rows from a TableLayout that that is instantiated with one row inside a viewpager. When user click a button 12 more rows are added. Then when users click the button again those 12 rows should be deleted and another set of rows should be added. However, whenever I try to delete the rows from the TableLayout, only the odd number rows are deleted instead of all the rows. Any help resolving this is greatly appreciated.
ViewPager (where tablelayout instantiated)
case 2:
resId = R.layout.suggestedremedy_layout;
view = inflater.inflate(resId, null);
remedyDescription = (TextView)view.findViewById(R.id.remedy_description);
remedyCause = (TextView)view.findViewById(R.id.remedy_cause);
remedyHerbs = (TextView)view.findViewById(R.id.remedy_herbs);
remedyFoods = (TextView)view.findViewById(R.id.remedy_foods);
remedyTable = (TableLayout)view.findViewById(R.id.remedy_table);
TableRow titleRow = new TableRow(RemedyActivity.this);
TextView titleSupplement = new TextView(RemedyActivity.this);
TextView titleDosage = new TextView(RemedyActivity.this);
TextView titleComment = new TextView(RemedyActivity.this);
titleSupplement.setText("Supplement");
titleDosage.setText("Dosage");
titleComment.setText("Comment");
titleSupplement.setGravity(Gravity.CENTER);
titleDosage.setGravity(Gravity.CENTER);
titleComment.setGravity(Gravity.CENTER);
titleRow.addView(titleSupplement);
titleRow.addView(titleDosage);
titleRow.addView(titleComment);
remedyTable.addView(titleRow);
((ViewPager) collection).addView(view, 0);
return view;
Button Click (where rows after title row are deleted and new rows added)
//Updating Table
if(remedyTable.getChildCount()>0){
for (int i = 0; i < remedyTable.getChildCount(); i++) {
if(i>0){
remedyTable.removeViewAt(i); //Only removing odd rows, instead off all rows. Why?
}
}
}
remedyItemsSupplements = new ArrayList<String>();
remedyItemsDosage = new ArrayList<String>();
remedyItemsComments = new ArrayList<String>();
remedyDescription.setText(R.string.acne_description);
remedyCause.setText(R.string.acne_cause);
remedyHerbs.setText(R.string.acne_herbs);
remedyFoods.setText(R.string.acne_foods);
remedyListSupplements = res.getStringArray(R.array.acne_supplements);
remedyListDosage = res.getStringArray(R.array.acne_dosage);
remedyListComments = res.getStringArray(R.array.acne_comment);
Collections.addAll(remedyItemsSupplements, remedyListSupplements);
Collections.addAll(remedyItemsDosage, remedyListDosage);
Collections.addAll(remedyItemsComments, remedyListComments);
for (int i = 0; i < remedyItemsSupplements.size(); i++) {
TableRow remedyRow = new TableRow(RemedyActivity.this);
TextView remedySupplement = new TextView(RemedyActivity.this);
TextView remedyDosage = new TextView(RemedyActivity.this);
TextView remedyComment = new TextView(RemedyActivity.this);
remedySupplement.setText(remedyItemsSupplements.get(i));
remedyDosage.setText(remedyItemsDosage.get(i));
remedyComment.setText(remedyItemsComments.get(i));
remedySupplement.setBackgroundResource(R.drawable.table_background);
remedyDosage.setBackgroundResource(R.drawable.table_background);
remedyComment.setBackgroundResource(R.drawable.table_background);
remedySupplement.setHeight(80);
remedySupplement.setWidth(500);
remedyDosage.setHeight(80);
remedyComment.setHeight(80);
remedyRow.addView(remedySupplement);
remedyRow.addView(remedyDosage);
remedyRow.addView(remedyComment);
remedyTable.addView(remedyRow);
}
In your loop
for (int i = 0; i < remedyTable.getChildCount(); i++) {
After you remove child at 0, the child at index one becomes 0, and your loop goes on to uindex one, so it just leaves it there.
Instead do this
while (view.getChildCount() > 0) {
view.removeChildAt(0);
}
I am trying to add my query results to a table row inside a table layout. I keep getting java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. When i call:
((ViewGroup)table.getParent()).removeView(table);
I get an null pointer error. Any help on this would be great. Here`s the full code.
runOnUiThread(new Runnable() {
public void run() {
TableLayout table = (TableLayout)findViewById(R.id.resultstable);
int count = response.getPropertyCount();
System.out.println(count);
for(int i = 0; i < count; i++)
{
resultset.add(response.getProperty(i));
}
//TextView tv = new TextView(this);
for(int j = 0; j < resultset.size(); j++)
{
//if(resultset.get(j).toString() == "resFName")
//{
tv.setText(resultset.get(j).toString());
//((ViewGroup)row.getParent()).removeView(row);
row.addView(tv);
//((ViewGroup)table.getParent()).removeView(table);
table.addView(row);
//}
}
}
});
You are adding same TextView object to the row each time. In each iteration, create a new TextView and a TableRow.
for(int j = 0; j < resultset.size(); j++)
{
//if(resultset.get(j).toString() == "resFName")
//{
TextView tv = new TextView(context);
tv.setText(resultset.get(j).toString());
//((ViewGroup)row.getParent()).removeView(row);
TableRow row = new TableRow(context);
row.addView(tv);
//((ViewGroup)table.getParent()).removeView(table);
table.addView(row);
//}
}
You are adding the same view object to the parent. So, you are getting IllegalStateException.
If you seperate child views with "id" attribute, adding will be successful. You can give different ids to the child views via hashcode method.
I need to add a number of rows to my Table layout and to each row I need to add two Image Buttons. Number of rows can be different each time I start the Activity - I look into a SQLite database and for each row in the database I need to add an Image Button. And there will be two Image Buttons in each row of Table layout. So far, I have this code:
db.open();
int count = db.getCount();
boolean newRow = true;
for (int i = 0; i < count; i++) {
if (newRow == true) {
newRow = false;
row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
ImageButton button = new ImageButton(this);
row.addView(button);
tableLayout.addView(row);
}
db.close();
TableRow row is defined above this block of code simply as variable for this Activity. My Table layout (tableLayout in code) is defined in XML code of the Activity layout:
<TableLayout
android:id="#+id/tableLayoutContacts"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
The code crashes at the line
tableLayout.addView(row);
I wasnt able to resolve this. Any ideas? Thanks!!
I think the problem is different
It seems that Your code will have only one row and you are adding that row again and again to tableLayout as shown below.
tableLayout.addView(row);
This will lead to IllegalStateException at addViewInner Function of ViewGroup
try this
db.open();
int count = db.getCount();
boolean newRow = false;
for (int i = 0; i < count; i++) {
if (i % 2 == 0)
newRow = true;
if (newRow == true) {
newRow = false;
row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tableLayout.addView(row);
}
ImageButton button = new ImageButton(this);
row.addView(button);
}
db.close();