dynamically added rows in tablelayout are not visible - android

i have a table with header row designed in xml. Now i want to add data in the table ( which i get from json array).
I need to scroll data rows vertically without affecting header row, so i created another tablelayout just below the table of header row. Then for each field of each new row, i set layout params in java file and then add the row to second table layout.
But no rows are visible while data is getting printed in the log.I can't figure out where is my mistake.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/rv_table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/rv_head_row"
>
<TextView
android:id="#+id/rv_head_desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/Desc"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rv_head_unit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/Unit"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rv_head_type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/Type"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rv_head_range"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/Range"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rv_head_rv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/RV"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rv_head_remarks"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/Remarks"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/rv_data_table_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
</LinearLayout>
JAVA CODE:
protected void onCreate(Bundle savedInstanceState) {
Log.i("result value","in result value");
super.onCreate(savedInstanceState);
setContentView(R.layout.results_value);
/*
* connecting with xml
*/
rv_table = (TableLayout) findViewById(R.id.rv_table_layout);
rv_data_table = (TableLayout) findViewById(R.id.rv_data_table_layout);
rv_head_row = (TableRow) findViewById(R.id.rv_head_row);
desc = (TextView)findViewById(R.id.rv_head_desc);
unit= (TextView)findViewById(R.id.rv_head_unit);
type= (TextView)findViewById(R.id.rv_head_type);
range = (TextView)findViewById(R.id.rv_head_range);
rv= (TextView)findViewById(R.id.rv_head_rv);
remarks= (TextView)findViewById(R.id.rv_head_remarks);;
/*
* getting data from server
*/
final Bundle extras = getIntent().getExtras();
try {
js = new JSONObject(){
{
put("CaseId", extras.getString("CaseId"));
}
};
} catch (JSONException e1) {
e1.printStackTrace();
}
Log.v("~~~~~===data is", js.toString());
service = new Services(ResultValueActivity.this);
String rslt=service.getResultValue(js);
try {
JSONObject rs=new JSONObject(rslt);
String p = rs.get("d").toString(); // this is done to
// remove
// starting d: i.e seprating
// first key value pairs
JSONObject temp = new JSONObject(p);
values = temp.getJSONArray("Table"); // values is a array of JSON
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* data received, now starting filling values from json array to table
*/
JSONObject test;
TableRow row;
TextView desc1,unit1,type1,range1;
EditText rv1;
Button remarks1;
for(int i=0;i<values.length();i++)
{
try{
test = values.getJSONObject(i);
row = new TableRow(ResultValueActivity.this);
desc1 = new TextView(ResultValueActivity.this);
unit1 = new TextView(ResultValueActivity.this);
type1 = new TextView(ResultValueActivity.this);
range1 = new TextView(ResultValueActivity.this);
rv1 = new EditText(ResultValueActivity.this);
remarks1 = new Button(ResultValueActivity.this);
/*
* setting text in fields
*/
desc1.setText(test.getString("Description"));
unit1.setText(test.getString("Unit"));
type1.setText(test.getString("ResultType"));
range1.setText(test.getString("RangeValue"));
rv1.setText(test.getString("ResultValue"));
remarks1.setText("Remarks");
desc1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
unit1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
type1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
range1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
rv1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
remarks1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
row.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
Log.i(i+" result value test====>", test.toString());
row.addView(desc1);
row.addView(unit1);
row.addView(type1);
row.addView(range1);
row.addView(rv1);
row.addView(remarks1);
rv_data_table.addView(row,new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
rv_data_table.setVisibility(View.VISIBLE);
Log.i(i+" desc value test====>", desc1.getText().toString());
Log.i(i+" unit value test====>", unit1.getText().toString());
}
catch(JSONException jse)
{
jse.printStackTrace();
}}
Log.i("result Value"," it is done :D :D");
}
Log is printing all the values accurately

1st Table Layout has height of match_parent and thus was not giving space to second table layout, so i changed it to wrap content

Related

Displaying SQLite data into TableRows

I am creating an app that would display data from my SQLite database, though I have been successful in displaying the data in Custom ListViews. But I am wondering how would I be able to display data in a Table like form, I can display the first line of data though I don't know where to go from here.
This is my ActivityMain.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.gin.customlistview.MainActivity">
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SAVED MESSAGES"
android:textSize="20dp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="center_horizontal"/>
<TableRow
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_margin="1dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="15dp"
android:textColor="#000"
android:text="Phone Number"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:textStyle="bold"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="15dp"
android:textColor="#000"
android:text="Username"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="15dp"
android:textColor="#000"
android:text="Account Number"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:textStyle="bold"
android:layout_column="2"
/>
</TableRow>
<TableRow
android:id="#+id/tableRow"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp">
<TextView
android:id="#+id/txtPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000"
android:background="#FFFFFF"
android:gravity="center"
android:layout_margin="1dp" />
<TextView
android:id="#+id/txtUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
/>
<TextView
android:id="#+id/txtAccountNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center" />
</TableRow>
</TableLayout>
</LinearLayout>
This is my MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new textDatabase(this);
txtPhoneNumber = (TextView) findViewById(R.id.txtPhoneNumber);
txtUsername = (TextView) findViewById(R.id.txtUsername);
txtAccountNumber = (TextView) findViewById(R.id.txtAccountNumber);
checkPermission();
showData();
}
public static void showData() {
// databaseNumber.clear();
// databaseUsername.clear();
// databaseAccountNumber.clear();
Cursor data = db.getSMS();
if (data.getCount() == 0) {
} else {
data.moveToFirst();
do {
txtPhoneNumber.setText(data.getString(1));
txtUsername.setText(data.getString(2));
txtAccountNumber.setText(data.getString(3));
// databaseNumber.add(data.getString(1));
// databaseUsername.add(data.getString(2));
// databaseAccountNumber.add(data.getString(3));
} while (data.moveToNext());
}
data.close();
// adapters.notifyDataSetChanged();
}
Thanks in advance for any help and suggestions! :D
try something like this
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
</TableLayout>
Once You have Created the tableLayout Create a table row layout table_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/name"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/title"
android:layout_weight="0.25"
android:gravity="center"/>
</TableRow>
inside your activity iterate through the values retrieved from the sql and add it to the table
private TableLayout tableLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
tableLayout=(TableLayout)findViewById(R.id.tableLayout);
data.moveToFirst();
do {
View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item,null,false);
TextView name = (TextView) tableRow.findViewById(R.id.name);
TextView title = (TextView) tableRow.findViewById(R.id.title);
name.setText(data.getString(1));
title.setText(data.getString(2));
tableLayout.addView(tableRow);
} while (data.moveToNext());
data.close();
}
Use below code , rows will be created dynamically according to the data , result will be something similar to this
LinearLayout table = (LinearLayout) findViewById(R.id.table);
ArrayList<Tabledata> datalist = new ArrayList<>();
datalist = db.getyourSQLITEdata();
// this method for table title
createtitle(table);
for(int i=0;i<datalist.size();i++){
// this method is creating table rows and filling data
Createtable(table,i);
}
}
table title
// your table title here (if you want title for your table you can use this)
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
void createtitle(LinearLayout main){
LinearLayout row = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 7);
row.setLayoutParams(params);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setWeightSum(100);
// your table title here
String[] title={"title1","title2","title3","title4","title5","title6","title7"};
for (int i = 0; i < 7; i++) {
LinearLayout.LayoutParams textparam;
if(i == 2){
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 20.285);
}else{
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 13.285);
}
TextView col1 = new TextView(this);
col1.setText(title[i]);
col1.setTextSize(13);
col1.setPadding(0,0,0,0);
col1.setLayoutParams(textparam);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
col1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
col1.setBackground(getResources().getDrawable(R.drawable.border,null));
}else{
col1.setBackground(getResources().getDrawable(R.drawable.border));
}
row.addView(col1);
}
main.addView(row);
}
table
// your data table is created here
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
void Createtable(LinearLayout main,int pos) {
Tabledata data = datalist.get(pos);
// this is where you get data from list and show it in table
String[] yourdata = {data.getdata1(),data.getdata2(),data.getdata3(),data.getdata4(),data.getdata5(),data.getdata6(),data.getdata7()};
LinearLayout row = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 7);
row.setLayoutParams(params);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setWeightSum(100);
for (int i = 0; i < 7; i++) {
LinearLayout.LayoutParams textparam;
if(i == 2){
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 20.285);
}else{
textparam = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, (float) 13.285);
}
TextView col1 = new TextView(this);
col1.setText(yourdata[i]);
col1.setTextSize(10);
col1.setLines(1);
col1.setPadding(0,0,0,0);
col1.setLayoutParams(textparam);
col1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
col1.setBackground(getResources().getDrawable(R.drawable.border,null));
}else{
col1.setBackground(getResources().getDrawable(R.drawable.border));
}
row.addView(col1);
}
main.addView(row);
}
Xml code
<LinearLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
</LinearLayout>
TableLayout tableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new textDatabase(this);
txtPhoneNumber = (TextView) findViewById(R.id.txtPhoneNumber);
txtUsername = (TextView) findViewById(R.id.txtUsername);
txtAccountNumber = (TextView) findViewById(R.id.txtAccountNumber);
//table layout
tableLayout = (TableLayout) findViewById(R.id.table);
checkPermission();
showData();
}
public void showData() {
TextView phn_no,user_name,account_no;
// databaseNumber.clear();
// databaseUsername.clear();
// databaseAccountNumber.clear();
Cursor data = db.getSMS();
if (data.getCount() == 0) {
} else {
data.moveToFirst();
do {
phn_no=new TextView(this);
user_name=new TextView(this);
account_no=new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
params.setMargins(1,1,1,1);
params.gravity= Gravity.CENTER;
phn_no.setBackgroundColor(Color.parseColor("#FFFFFF"));
phn_no.setTextColor(Color.parseColor("#000"));
phn_no.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
phn_no.setLayoutParams(params);
phn_no.setText(data.getString(1));
user_name.setBackgroundColor(Color.parseColor("#FFFFFF"));
user_name.setTextColor(Color.parseColor("#000"));
user_name.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
user_name.setLayoutParams(params);
user_name.setText(data.getString(2));
account_no.setBackgroundColor(Color.parseColor("#FFFFFF"));
account_no.setTextColor(Color.parseColor("#000"));
account_no.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
account_no.setLayoutParams(params);
account_no.setText(data.getString(3));
TableRow row = new TableRow(this);
row.addView(phn_no);
row.addView(user_name);
row.addView(account_no);
tableLayout.addView(row);
txtPhoneNumber.setText(data.getString(1));
txtUsername.setText(data.getString(2));
txtAccountNumber.setText(data.getString(3));
// databaseNumber.add(data.getString(1));
// databaseUsername.add(data.getString(2));
// databaseAccountNumber.add(data.getString(3));
} while (data.moveToNext());
}
data.close();
// adapters.notifyDataSetChanged();
}
}
You can use this
You can use a custom listview or Recyclerview which is more or less like a listview, it's just an advanced listview to be precise. You can use this documentation to understand recyclerviews https://developer.android.com/training/material/lists-cards.html

How to create Edittext programmatically inside of DialogBox depends on Array size?

I have created Dialog Box with 4 EditText. But now I need to do this by dynamically depends on API response JsonObject count.
Here is my response:
{
"additional_charges":{
"1":"121324324",
"2":"245657687",
"3":"379809733",
"4":"4467797894"
}
}
If "additional_charges", has "1","2","3" ---> I have to show 3
Edittext.
If "additional_charges", has "1","2","3","4" ---> I have to show 4
Edittext in Dialog box.
How to do this?
dialog.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fare_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:id="#+id/currentlocTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="center"
android:lines="1"
android:marqueeRepeatLimit="marquee_forever"
android:padding="5dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="#string/additionalcharge"
android:textColor="#color/button_accept"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:id="#+id/chargeType"
android:orientation="vertical"
android:padding="3dp"
android:weightSum="1">
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="#dimen/com_facebook_share_button_compound_drawable_padding"
android:background="#color/hintcolor" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<Button
android:id="#+id/cancelBut"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0.5"
android:background="#color/white"
android:text="Cancel"
android:textColor="#color/button_accept" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/com_facebook_share_button_compound_drawable_padding"
android:background="#color/hintcolor" />
<Button
android:id="#+id/submitBut"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#color/white"
android:text="Submit"
android:textColor="#color/button_accept" />
</LinearLayout>
</LinearLayout>
Dialog.class:
private void ShowDialog(final String completeUrl, Context context) {
if (NotificationAct.passenger_mobile != null) {
final View view = View.inflate(OngoingAct.this, R.layout.additional_charges, null);
cDialog = new Dialog(OngoingAct.this, R.style.dialogwinddow);
cDialog.setContentView(view);
cDialog.setCancelable(false);
cDialog.show();
FontHelper.applyFont(OngoingAct.this, cDialog.findViewById(R.id.alert_id));
LinearLayout linlay_container = (LinearLayout) findViewById(R.id.chargeType);
//length from json response
int arrayLength = 4;
EditText editText[] = new EditText[0];
for (int i = 0; i < arrayLength; i++) {
editText[i] = new EditText(this);
editText[i].setBackgroundResource(R.color.white);
editText[i].setId(i);
linlay_container.addView(editText[i]);
}
final Button button_success = (Button) cDialog.findViewById(R.id.submitBut);
final Button button_failure = (Button) cDialog.findViewById(R.id.cancelBut);
button_failure.setVisibility(View.VISIBLE);
}
}
Firstly, parse the Json response and get the length of the array.
Then, create a layout container in the Dialog box where you want to add the EditText's.
Then, you can iterate through for loop by creating new EditText object and adding it to the container according to the array list.
Have a look on a code snippets,
LinearLayout linlay_container=(LinearLayout)findViewById(R.id.linlay_container);
//length from json response
int arrayLength=?;
for (int i = 0; i <arrayLength ; i++) {
EditText editText=new EditText(this);
linlay_container.addView(editText);
}
Here, linlay_container is the container layout of Dialog box in which you want to add EditText and arraylength is the array size from your json response.

Place buttons into multiple columns android

As they look in the picture: http://es.tinypic.com/r/mug8b8/8
In the month of having a single column, have several with botons
The genre buttons dynamically by a query
The function that generates the buttons:
private void createEmpresas() {
Button b;
JSONObject json = null;
int count = 0;
LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
JSONObject jobj = obj_sqlite.get_descripcion_empresas();
try {
count = Integer.parseInt(jobj.getString("cont"));
json = new JSONObject(jobj.getString("json"));
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
for (int x = 1; x <= count; x++) {
try {
JSONObject json_row = new JSONObject(json.getString("row" + x));
b = new Button(this);
b.setText(json_row.getString("descripcion"));
b.setId(json_row.getInt("empresa"));
b.setTextSize(10);
b.setPadding(8, 3, 8, 3);
b.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lm.addView(b);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"Yipee.." +v.getId(), Toast.LENGTH_SHORT)
.show();
}
});
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
}
}
my xml is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bg_main"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.php_mysql_sqlite.MainActivity" >
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button6"
android:layout_alignBottom="#+id/button6"
android:layout_alignParentRight="true"
android:layout_marginRight="17dp"
android:background="#null"
android:onClick="loginOut"
android:text="#string/btLogOut"
android:textColor="#color/Red" />
<Button
android:id="#+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_centerHorizontal="true"
android:onClick="syncSqliteMysql"
android:text="#string/syn" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="borrarTabla"
android:text="#string/delTable" />
<ScrollView
android:layout_width="285dp"
android:layout_height="330dp"
android:layout_marginTop="1dp" >
<LinearLayout
android:id="#+id/linearMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignLeft="#+id/button5"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button4"
android:background="#color/White"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Thanks for the help
PS: If you give me negative points, leave a comment of reason, as has happened before and not why
here are some answers that may help
1-
In android how do I add buttons into a TableLayout from an array of buttons programmatically?
2-
How do I programmatically add buttons into layout one by one in several lines?
3-
Add an array of buttons to a GridView in an Android application

Error in Adding radiobuttons to radiogroup in tableLayout in android programaticall/dynamically?

RadioGroup rg = new RadioGroup(this);
for (int i = 0; i < e.length(); i++)
{
JSONObject arrayElement = e.getJSONObject(i);
TableRow newRow = new TableRow(this);
// add views to the row
TextView tv1 = new TextView(this);
tv1.setText(arrayElement.getString("name"));
newRow.addView(tv1); // you would actually want to set properties on this before adding it
arr[j++]=arrayElement.getString("name");
TextView tv2 = new TextView(this);
tv2.setText(arrayElement.getString("specialization"));
newRow.addView(tv2);
arr[j++]=arrayElement.getString("specialization");
TextView tv3 = new TextView(this);
tv3.setText(arrayElement.getString("emailid"));
newRow.addView(tv3);
arr[j++]=arrayElement.getString("emailid");
TextView tv4 = new TextView(this);
tv4.setText(arrayElement.getString("day"));
newRow.addView(tv4);
arr[j++]=arrayElement.getString("day");
TextView tv5 = new TextView(this);
String t1=arrayElement.getString("tf");
t1=t1.substring(11,16);
tv5.setText(t1);
newRow.addView(tv5);
arr[j++]=arrayElement.getString("tf");
TextView tv6 = new TextView(this);
String t2=arrayElement.getString("tt");
t2=t2.substring(11,16);
tv6.setText(t2);
newRow.addView(tv6);
arr[j++]=arrayElement.getString("tt");
TextView tv7 = new TextView(this);
tv7.setText(arrayElement.getString("place"));
newRow.addView(tv7);
arr[j++]=arrayElement.getString("place");
// add the row to the table layout
RadioButton rb = new RadioButton(this);
rg.addView(rb);
newRow.addView(rb);
tl.addView(newRow);
}
// tl.addView(rg);
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
}
My problem is that i want to dynamically add a radiobutton infront of each row of my result, which the above code does fine , But since to select one of the row i need to get all the radiobuttons in a radioGroup.This is where i am stuck.it is giving me this error
03-23 22:43:37.807: E/AndroidRuntime(551): Caused by:
java.lang.IllegalStateException: The specified child already has a
parent. You must call removeView() on the child's parent first.
i have no idea what the above is , this line in the above code is giving me the error ->rg.addView(rb);
xml file
<ScrollView android:id="#+id/ScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<HorizontalScrollView android:id="#+id/HorizontalScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal|vertical"
>
<TableLayout
android:id="#+id/tl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.73"
>
<TableRow
android:id="#+id/Heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="16sp"
android:background="#layout/shape"/>
<TextView
android:id="#+id/Specialization"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Specialization"
android:textSize="16sp"
android:background="#layout/shape"/>
<TextView
android:id="#+id/Emailid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Emailid"
android:textSize="16sp"
android:background="#layout/shape"/>
<TextView
android:id="#+id/Day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day"
android:textSize="16sp"
android:background="#layout/shape" />
<TextView
android:id="#+id/tf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From"
android:textSize="16sp"
android:background="#layout/shape"/>
<TextView
android:id="#+id/tt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To "
android:textSize="16sp"
android:background="#layout/shape"/>
<TextView
android:id="#+id/pl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Place"
android:textSize="16sp"
android:background="#layout/shape"/>
</TableRow>
</TableLayout>
</HorizontalScrollView>
</ScrollView>
This is happening because you are adding rb to two different GroupViews (rg and newRow). RadioGroup is a GroupView on it's own and can't add vies to it as wel as to other GroupView.
I think that you are going to have to handle the single selection yourself. Possibly by attaching the same selection listener and keeping a copy of the selected radio button so you can deselect it when another one is selected. On the other hand if you want to have multiple selection you can keep a List with the RadioButton instances and then when needing to check which ones are selected just run though it.
Hope it helps.

Android scrollable TableLayout with a dynamic fixed header

I have a TableLayout inside a ScrollView, that is I have a scrollable TableLayout! It is populated dynamically when I create Dialog's in different places of an Activity.
Actually everything works fine, but the fact that for every case there's a different header (with a title for each column) on that table and it scrolls along with the rows. As the content of that table is dynamic, the amount (as well as the width) of columns is unknown.
So, basically, that's my problem. I don't see point in posting code, as I need mostly a suggestion/workaround, but if that would help overcome the issue, let me know. Would appreciate some samples very much.
Here is a way :
http://sdroid.blogspot.com/2011/01/fixed-header-in-tablelayout.html
Note that there is a way to have this work every time : what you have to do is create a similar dummy row in the header, and set the texts in both dummies to the longest string you can find in the row (in number of chars, or, even better, do the comparison with Paint.getTextWidths)
package com.test.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class TestProjectTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout tl_head1 = (TableLayout)findViewById(R.id.tl_head);
TableLayout tl_child1 = (TableLayout)findViewById(R.id.tl_child);
String headcol1="";
TableRow tr[]= new TableRow[40];
TextView tv[] = new TextView[1000];
for(int i=0; i <=30; i++)
{
tr[i]=new TableRow(this);
for(int j=1; j<=5; j++)
{
tv[j]=new TextView(this);
tv[j].setId(j);
tv[j].setText("testingmultiple data hello"+""+j);
tv[j].setTextColor(Color.WHITE);
if(headcol1.length() < tv[j].getText().length())
{
headcol1=null;
headcol1=tv[j].getText().toString();
}
tr[i].addView(tv[j]);
}
tl_child1.addView(tr[i]);
}
TableRow trhead= new TableRow(this);
TextView tvhead[] = new TextView[5];
for(int i=0;i<=4;i++)
{
tvhead[i] = new TextView(this);
tvhead[i].setTextColor(Color.WHITE);
tvhead[i].setHeight(0);
tvhead[i].setText(headcol1);
trhead.addView(tvhead[i]);
}
tl_head1.addView(trhead);
}
}
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hsv_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/ll_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tl_head"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tr_head"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv_hidden1"
android:layout_gravity="center_horizontal"
android:text="12"
android:textColor="#FFFFFF"
/>
<TextView
android:id="#+id/tv_hidden2"
android:layout_gravity="center_horizontal"
android:text="123"
android:textColor="#FFFFFF"
/>
<TextView
android:id="#+id/tv_hidden3"
android:layout_gravity="center_horizontal"
android:text="1234"
android:textColor="#FFFFFF"
/>
<TextView
android:id="#+id/tv_hidden4"
android:layout_gravity="center_horizontal"
android:text="12345"
android:textColor="#FFFFFF"
/>
<TextView
android:id="#+id/tv_hidden5"
android:layout_gravity="center_horizontal"
android:text="123456"
android:textColor="#FFFFFF"
/>
</TableRow>
</TableLayout>
<ScrollView
android:id="#+id/sv_child"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/tl_child"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</LinearLayout>
</HorizontalScrollView>
Android scrollable TableLayout with a dynamic fixed header is very simple. Follow this steps
Step 1:
Create a TableLayout and use android:layout_alignParentTop="true" this will keep this layout always in top.
Step 2:
With in ScrollView create another TableLayout for contents with out Rows (For Dynamic content).
Step 3:
Add Rows and columns dynamically. While adding each column (TextView) assign the width to an temporary variable.
Ex
tvContentText.measure(0, 0);
tempmaxwidth=tvContentText.getMeasuredWidth();
Step 4:
Check for max width and assign max width to main variable.
Ex:
if (tempmaxwidth > maxwidth)
maxwidth=tempmaxwidth;
Step 5:
After assigning content details. Assign the max width of each column to its header column.
Ex:
TextView tvh1 = (TextView) findViewById(R.id.h1);
tvh1.setWidth(maxwidth);
The xml and code is below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TableLayout
android:id="#+id/headertbl"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow>
<TextView
android:id="#+id/h1"
android:layout_height="wrap_content"
android:text="H1"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp" />
<TextView
android:id="#+id/h2"
android:layout_height="wrap_content"
android:text="H2"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp" />
<TextView
android:id="#+id/h3"
android:layout_height="wrap_content"
android:text="H3"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp" />
<TextView
android:id="#+id/h4"
android:layout_height="wrap_content"
android:text="H4"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp" />
</TableRow>
</TableLayout>
<ScrollView
android:layout_below="#id/headertbl"
android:id="#+id/container"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<TableLayout
android:id="#+id/contenttbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
</TableLayout>
</ScrollView>
</RelativeLayout>
MainActivity.xml
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout tl = (TableLayout) findViewById(R.id.contenttbl);
int tempmaxwidth=0,maxwidth=0;
int tempmaxwidth1=0,maxwidth1=0;
int tempmaxwidth2=0,maxwidth2=0;
int tempmaxwidth3=0,maxwidth3=0;
for(int i=0;i<50;i++)
{
TableRow newRow = new TableRow(getApplicationContext());
newRow.setId(i);
TextView tvContentText = new TextView(getApplicationContext());
if(i!=6)
tvContentText.setText("Content 1");
else
tvContentText.setText("---- Content with Max width");
tvContentText.setTextColor(Color.BLACK);
tvContentText.setTextSize(16f);
tvContentText.measure(0, 0);
tempmaxwidth=tvContentText.getMeasuredWidth();
if (tempmaxwidth>maxwidth)
maxwidth=tempmaxwidth;
TextView tvContentText1 = new TextView(getApplicationContext());
//tvContentText1.setText(Integer.toString(maxwidth));
tvContentText1.setText("Content 2");
tvContentText1.setTextColor(Color.BLACK);
tvContentText1.setTextSize(16f);
tvContentText1.measure(0, 0);
tempmaxwidth1=tvContentText1.getMeasuredWidth();
if (tempmaxwidth1>maxwidth1)
maxwidth1=tempmaxwidth1;
TextView tvContentText2 = new TextView(getApplicationContext());
tvContentText2.setText("Content 3");
tvContentText2.setTextColor(Color.BLACK);
tvContentText2.setTextSize(16f);
tvContentText2.measure(0, 0);
tempmaxwidth2=tvContentText2.getMeasuredWidth();
if (tempmaxwidth2>maxwidth2)
maxwidth2=tempmaxwidth2;
TextView tvContentText3 = new TextView(getApplicationContext());
tvContentText3.setText("Content 4");
tvContentText3.setTextColor(Color.BLACK);
tvContentText3.setTextSize(16f);
tvContentText3.measure(0, 0);
tempmaxwidth3=tvContentText3.getMeasuredWidth();
if (tempmaxwidth3>maxwidth3)
maxwidth3=tempmaxwidth3;
newRow.addView(tvContentText);
newRow.addView(tvContentText1);
newRow.addView(tvContentText2);
newRow.addView(tvContentText3);
tl.addView(newRow);
}
// Assigning Max width to header column
TextView tvh1 = (TextView) findViewById(R.id.h1);
tvh1.setWidth(maxwidth);
TextView tvh2 = (TextView) findViewById(R.id.h2);
tvh2.setWidth(maxwidth1);
TextView tvh3= (TextView) findViewById(R.id.h3);
tvh3.setWidth(maxwidth2);
TextView tvh4= (TextView) findViewById(R.id.h4);
tvh4.setWidth(maxwidth3);
}
Well for me I didn't want to go through the trouble of worrying about alignment of columns so I created this first of all:
public static Boolean SET_TABLE_HEADER;
Then I created this function to set my TableHeader:
public void setTableHeader(TableLayout tbl) {
TableRow tr = new TableRow(context);
TextView tcView = new TextView(context);
tcView.setText("Class");
tcView.setTextColor(Color.BLACK);
tcView.setAllCaps(true);
tcView.setTypeface(null, Typeface.BOLD);
tcView.setGravity(Gravity.CENTER_HORIZONTAL);
tr.addView(tcView);
TextView tfView = new TextView(context);
tfView.setText("Fee");
tfView.setTextColor(Color.BLACK);
tfView.setAllCaps(true);
tfView.setTypeface(null, Typeface.BOLD);
tfView.setGravity(Gravity.CENTER_HORIZONTAL);
tr.addView(tfView);
TextView tqView = new TextView(context);
tqView.setText("Available");
tqView.setTextColor(Color.BLACK);
tqView.setAllCaps(true);
tqView.setTypeface(null, Typeface.BOLD);
tqView.setGravity(Gravity.CENTER_HORIZONTAL);
tr.addView(tqView);
// Adding row to Table
tbl.addView(tr);
// Table Header Has Been Set
SET_TABLE_HEADER = false;
}
Then inside my loop:
if (SET_TABLE_HEADER) {
setTableHeader(tbl);
}
Where tbl is the instance of my TableLayout in my view. This works for me.
Of course you will have to initialize SET_TABLE_HEADER to true at the point where you start creating your table.

Categories

Resources