Custom TableLayout with multiple TextViews in rows - android

I want to crate custom TableLayout with rows like this:
TV is for TextView, i.e. I want to add 11 TextViews to the row:
Each row begins with a title and then I add 5 pairs of TextViews, so that table row is as wide, as screen is.
Here's my code:
public class FlowTable extends TableLayout {
private Context context;
public FlowTable(Context context) {
super(context);
this.context = context;
}
public FlowTable(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public void addContent(List<ResultItem> data) {
TableRow tableRow = new TableRow(context);
LayoutParams params = new LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
for (int i = 0; i < data.size(); i++) {
if (i % 5 == 0) {
this.addView(tableRow, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tableRow = new TableRow(context);
TextView tvRange = new TextView(context);
tvRange.setLayoutParams(params);
tvRange.setText(genRange(i+1));
tableRow.addView(tvRange);
}
TextView tvDistance = new TextView(context);
tvDistance.setLayoutParams(params);
tvDistance.setText(String.valueOf(data.get(i).distance));
TextView tvResult = new TextView(context);
tvResult.setLayoutParams(params);
tvResult.setText(data.get(i).result);
tableRow.addView(tvDistance);
tableRow.addView(tvResult);
}
}
private String genRange(int currIndex){
/********************/
return somestring;
}
}
Using table:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<packagename.FlowTable
android:id="#+id/flowTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
In fragment:
View root = inflater.inflate(R.layout.fragment_session_summary, container, false);
FlowTable flowTable = (FlowTable)root.findViewById(R.id.flowTable);
flowTable.addContent(data);
The problem: the screen is just empty! Nothing at all. Before I added the layout params to textview it worked, but row didn't occupy the screen width. My initial solution was based on the LinearLayout samples, because TableRow is an extention of LinearLayout. But I can't make it work.
Thanks.

Try programmatically setting all columns to stretch (didn't seem to work in XML for me):
...
flowTable.addContent(data);
flowTable.setStretchAllColumns(true);
Some other quick facts:
No need to try and specify height and width for TableRow within TableLayout because it will always be height=WRAP_CONTENT and width=MATCH_PARENT. See TableLayout documentation where this is listed in the Class Overview section
No need to try and specify height and widget for children of TableRow because they will always be height=WRAP_CONTENT and width=MATCH_PARENT. See TableRow documentation where this is listed in the Class Overview section
Might I also humbly suggest a bit of refactoring:
public class FlowTable extends TableLayout {
private TableRow mCurrentRow;
public FlowTable(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FlowTable(Context context) {
super(context);
init();
}
private void init() {
mCurrentRow = new TableRow(getContext());
mCurrentRow.addView(createAndFillTextView("0")); // title for first row
setStretchAllColumns(true);
}
public void addContent(List<ResultInfo> data) {
for (int i = 0; i < data.size(); i++) {
if ((i % 5 == 0) && (i != 0) /** Don't do this on 0! */) {
finishRowAndStartNew(i);
}
mCurrentRow.addView(createAndFillTextView(data.get(i).distance));
mCurrentRow.addView(createAndFillTextView(data.get(i).result));
}
}
private void finishRowAndStartNew(int newRowIndex) {
addView(mCurrentRow);
mCurrentRow = new TableRow(getContext());
mCurrentRow.addView(createAndFillTextView(genRange(newRowIndex+1)));
}
private String genRange(int currIndex){
/********************/
return String.valueOf(currIndex);
}
private TextView createAndFillTextView(String text) {
TextView tv = new TextView(getContext());
tv.setText(text);
return tv;
}
}

If you want to use the xml Layout for that then try this:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableRow android:weightSum="1">
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Title" android:layout_weight="1"/>
</LinearLayout>
<!-- First Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Second Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Third Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Fourth Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2" />
</LinearLayout>
<!-- Fifth Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="2dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
</TableRow>
<TableRow android:weightSum="1">
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Title" android:layout_weight="1"/>
</LinearLayout>
<!-- First Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Second Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Third Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
<!-- Fourth Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2" />
</LinearLayout>
<!-- Fifth Group -->
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="2dp">
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv1"/>
<TextView android:id="#+id/totalText"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
android:text="Tv2"/>
</LinearLayout>
</TableRow>
</TableLayout>
Hope it will help you. . .
And if you want other then xml then let me know.
Thanks.

Related

output of my json response is displaying last item in the database

I am trying to display the content of my database in a tablelayout, I have fetch the data from the database, and set it to be displayed in my android app, but when the data displays, it overwrite the data fetch earlier and only show the last item.
this is an except form my code where I think I got the code wrong
#Override
protected void onPostExecute(String result) {
/* TextView textView= (TextView)findViewById(R.id.textview);
textView.setText(result);*/
json_string = result;
try {
jsonObject = new JSONObject(json_string);
jsonArray = jsonObject.getJSONArray("server_response");
int count = 0;
String id, inv_id, refnumber, description, details, qty, unit_price, amount;
Double subTotal = 00.00;
int sn = 1;
Double dAmount;
while (count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
description = JO.getString("description");
qty = JO.getString("qty");
unit_price = JO.getString("unit_price");
amount = JO.getString("amount");
txtDescription.setText(description);
txtQty.setText(qty);
txtUnit_price.setText(unit_price);
txtAmount.setText(amount);
txtsn.setText(String.valueOf(sn));
count++;
sn++;
try {
subTotal = subTotal + Double.valueOf(amount);
// dAmount = Double.valueOf(amount);
} catch (NumberFormatException e) {
subTotal = 00.00;
}
}
txtSubTotal.setText(String.valueOf(subTotal));
double VAT = 0.05*(subTotal);
txtVat.setText(String.valueOf(VAT));
Double Total = VAT + subTotal;
txtTotal.setText(String.valueOf(Total));
} catch (JSONException e) {
e.printStackTrace();
}
}
my XML layout is as follows
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ng.proartisan.invoiceapp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="S/N"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:textStyle="bold"
/>
<TextView
android:text="Description"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="QTY"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="Unit Price"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="Amount"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
</TableRow>
<TableRow>
<TextView
android:id="#+id/sn"
android:text="S/N"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:id="#+id/description"
android:text="Description"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:id="#+id/qty"
android:text="QTY"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/unit_price"
android:text="Unit Price"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/amount"
android:text="Amount"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="SubTotal"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/subtotal"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="VAT"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/vat"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="Total"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/total"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
please help
You are setting the data into same view in loop, other way to solve your problem like create a new layout and add into your layout example: XML file layout_item_description.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="#+id/tv_sn"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:text="S/N"
android:textStyle="bold"
/>
<TextView
android:id="#+id/tv_description"
android:layout_weight="4"
android:gravity="center"
android:padding="5dp"
android:text="Description"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_qty"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:text="QTY"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_unit_price"
android:layout_weight="2"
android:gravity="center"
android:padding="5dp"
android:text="Unit Price"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_amount"
android:layout_weight="2"
android:gravity="center"
android:padding="5dp"
android:text="Amount"
android:textStyle="bold" />
</TableRow>
</TableLayout>
add this item view to your layout. Your XML Layout file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ng.proartisan.invoiceapp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="S/N"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:textStyle="bold"
/>
<TextView
android:text="Description"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="QTY"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="Unit Price"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="Amount"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/ll_data_description"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="SubTotal"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/subtotal"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="VAT"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/vat"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="Total"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/total"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
Add LinearLayout ll_data_description = (LinearLayout)findViewById(R.id.ll_data_description);
#Override
protected void onPostExecute(String result) {
/* TextView textView= (TextView)findViewById(R.id.textview);
textView.setText(result);*/
json_string = result;
try {
jsonObject = new JSONObject(json_string);
jsonArray = jsonObject.getJSONArray("server_response");
int count = 0;
String id, inv_id, refnumber, description, details, qty, unit_price, amount;
Double subTotal = 00.00;
int sn = 1;
Double dAmount;
while (count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
View layout_item_description = getLayoutInflater().inflate(R.layout.layout_item_description, null);
((TextView) layout_item_description.findViewById(R.id.tv_sn)).setText(sn+"");
description = JO.getString("description");
qty = JO.getString("qty");
unit_price = JO.getString("unit_price");
amount = JO.getString("amount");
((TextView) layout_item_description.findViewById(R.id.tv_description)).setText(sn+"");
((TextView) layout_item_description.findViewById(R.id.tv_qty)).setText(sn+"");
((TextView) layout_item_description.findViewById(R.id.tv_unit_price)).setText(sn+"");
((TextView) layout_item_description.findViewById(R.id.tv_amount)).setText(sn+"");
count++;
sn++;
ll_data_description.addView(layout_item_description);
try {
subTotal = subTotal + Double.valueOf(amount);
// dAmount = Double.valueOf(amount);
} catch (NumberFormatException e) {
subTotal = 00.00;
}
}
txtSubTotal.setText(String.valueOf(subTotal));
double VAT = 0.05*(subTotal);
txtVat.setText(String.valueOf(VAT));
Double Total = VAT + subTotal;
txtTotal.setText(String.valueOf(Total));
} catch (JSONException e) {
e.printStackTrace();
}
}

androit text view Mysql

Hello I am developing an application android, I have a problem with a text view that retrieves the data via web service, the data retrieve appears but infringe on the other text view of the page, Here is my code from my xml page:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/tvd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Détail de l'établissement"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:text="Libelle :"
android:textSize="20dp"
android:textStyle="bold"
android:layout_below="#+id/tvd"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="Code postal :"
android:textSize="20dp"
android:textStyle="bold"
android:layout_below="#+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="Ville :"
android:textSize="20dp"
android:textStyle="bold"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/tvl"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView3"
android:layout_alignParentEnd="true"
android:hint="libelle"
android:textSize="20dp"
/>
<TextView
android:id="#+id/tvcp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvl"
android:layout_alignStart="#+id/tvl"
android:layout_alignTop="#+id/textView4"
android:hint="Code postal"
android:textSize="20dp" />
<TextView
android:id="#+id/tvv"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvcp"
android:layout_alignStart="#+id/tvcp"
android:layout_alignTop="#+id/textView5"
android:hint="Ville"
android:textSize="20dp" />
<Button
android:id="#+id/btnMa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Matériel numérique associé"
android:textSize="20dp"
android:textStyle="bold|italic"
/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="20dp"
android:textStyle="bold"
android:layout_below="#+id/textView5"
android:layout_marginTop="39dp"
android:text="Présentation" />
<TextView
android:id="#+id/tvp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView"
android:layout_alignLeft="#+id/tvv"
android:layout_alignStart="#+id/tvv"
android:hint="Presentation"
android:textSize="20dp" />
Then my code that says or go data :
public class DetailEtab extends Activity {
private TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.etabdetail);
tv = (TextView) findViewById(R.id.tvl);
tv.setText(search_ville.etabSELECT.getLibelle());
tv = (TextView) findViewById(R.id.tvcp);
tv.setText(search_ville.etabSELECT.getCp());
tv = (TextView) findViewById(R.id.tvv);
tv.setText(search_ville.etabSELECT.getVille());
tv = (TextView) findViewById(R.id.tvp);
tv.setText(search_ville.etabSELECT.getPresentation());
Button btnSa = (Button) findViewById(R.id.btnMa);
btnSa.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(DetailEtab.this, MaterielAssocie.class);
startActivity(intent);
Log.i("ETAB", "servcies associes");
Toast toast = Toast.makeText(getApplicationContext(), "services associes", Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
If anyone can tell me why it does that thanks
[screenshot][1] [1]: https://i.stack.imgur.com/ImHxk.png
You have problems with your layouts. I recommend you to wrap every line into separate LinearLayout and then use LinearLayout to wrap all new LinearLayouts. Also due to long text it's better to wrap parent LinearLayout into Scroll.
NOTE: This is just example that was written inside text box that simply shows main idea and may contain mistakes
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Détail de l'établissement"
android:textSize="20dp"
android:textStyle="bold"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:text="Libelle :"
android:textSize="20dp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/tvl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="libelle"
android:textSize="20dp"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="Code postal :"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvcp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Code postal"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="Ville :"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Ville"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="39dp"
android:text="Présentation" />
<TextView
android:id="#+id/tvp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Presentation"
android:textSize="20dp" />
</LinearLayout>
<Button
android:id="#+id/btnMa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Matériel numérique associé"
android:textSize="20dp"
android:textStyle="bold|italic"
/>
</LinearLayout>
</ScrollView>
You are assigning tv to multiple different TextViews and then you are assigning multiple values to the last TextView you assign it to.
public class DetailEtab extends Activity {
private TextView tv,tv1,tv2,tv3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.etabdetail);
tv = (TextView) findViewById(R.id.tvl);
tv.setText(search_ville.etabSELECT.getLibelle());
tv1 = (TextView) findViewById(R.id.tvcp);
tv1.setText(search_ville.etabSELECT.getCp());
tv2 = (TextView) findViewById(R.id.tvv);
tv2.setText(search_ville.etabSELECT.getVille());
tv3 = (TextView) findViewById(R.id.tvp);
tv3.setText(search_ville.etabSELECT.getPresentation());
Button btnSa = (Button) findViewById(R.id.btnMa);
btnSa.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(DetailEtab.this, MaterielAssocie.class);
startActivity(intent);
Log.i("ETAB", "servcies associes");
Toast toast = Toast.makeText(getApplicationContext(), "services associes", Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
Try this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:id="#+id/tvd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Détail de l'établissement"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/tvd"
android:layout_marginTop="42dp"
android:text="Libelle :"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView3"
android:layout_marginTop="64dp"
android:text="Code postal :"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView4"
android:layout_marginTop="64dp"
android:text="Ville :"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/tvl"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_alignParentEnd="true"
android:text="hfdgsjfhgadsjfgajsdfhgggdsjfgsdjafhgjsdafhgjdhfgjsdhgfjdshfgjdshfgjhdgfjdshgfjhdgfjsdhgafjahgsdfjdshgfjhdsagfjhdsgafjhsdagfjhdsgafjhsadgfjfdgjshfgjhfgjhsdgfhsgdfjhdsgfjhgdsjfhgsdjfhgsdjfhgsdjfhgsdjfhgsdjfhgsdjhfgjsdhgfjsdhgfjsdhfgjsdhfgjdsfhgjsdhgfjdshgfjsdhgfjhsgdfjhsdgjfhdgfjhgdsf"
android:layout_alignTop="#+id/textView3"
android:hint="libelle"
android:textSize="20dp"
/>
<TextView
android:id="#+id/tvcp"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_alignLeft="#+id/tvl"
android:layout_alignStart="#+id/tvl"
android:layout_alignTop="#+id/textView4"
android:hint="Code postal"
android:text="hfdgsjfhgadsjfgajsdfhgggdsjfgsdjafhgjsdafhgjdhfgjsdhgfjdshfgjdshfgjhdgfjdshgfjhdgfjsdhgafjahgsdfjdshgfjhdsagfjhdsgafjhsdagfjhdsgafjhsadgfjfdgjshfgjhfgjhsdgfhsgdfjhdsgfjhgdsjfhgsdjfhgsdjfhgsdjfhgsdjfhgsdjfhgsdjhfgjsdhgfjsdhgfjsdhfgjsdhfgjdsfhgjsdhgfjdshgfjsdhgfjhsgdfjhsdgjfhdgfjhgdsf"
android:textSize="20dp"/>
<TextView
android:id="#+id/tvv"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_alignLeft="#+id/tvcp"
android:layout_alignStart="#+id/tvcp"
android:layout_alignTop="#+id/textView5"
android:text="hfdgsjfhgadsjfgajsdfhgggdsjfgsdjafhgjsdafhgjdhfgjsdhgfjdshfgjdshfgjhdgfjdshgfjhdgfjsdhgafjahgsdfjdshgfjhdsagfjhdsgafjhsdagfjhdsgafjhsadgfjfdgjshfgjhfgjhsdgfhsgdfjhdsgfjhgdsjfhgsdjfhgsdjfhgsdjfhgsdjfhgsdjfhgsdjhfgjsdhgfjsdhgfjsdhfgjsdhfgjdsfhgjsdhgfjdshgfjsdhgfjhsgdfjhsdgjfhdgfjhgdsf"
android:hint="Ville"
android:textSize="20dp"/>
<Button
android:id="#+id/btnMa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Matériel numérique associé"
android:textSize="20dp"
android:textStyle="bold|italic"
/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView5"
android:layout_marginTop="39dp"
android:text="Présentation"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/tvp"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_below="#+id/tvv"
android:layout_toRightOf="#id/textView"
android:layout_alignLeft="#+id/tvv"
android:layout_alignStart="#+id/tvv"
android:text="hfdgsjfhgadsjfgajsdfhgggdsjfgsdjafhgjsdafhgjdhfgjsdhgfjdshfgjdshfgjhdgfjdshgfjhdgfjsdhgafjahgsdfjdshgfjhdsagfjhdsgafjhsdagfjhdsgafjhsadgfjfdgjshfgjhfgjhsdgfhsgdfjhdsgfjhgdsjfhgsdjfhgsdjfhgsdjfhgsdjfhgsdjfhgsdjhfgjsdhgfjsdhgfjsdhfgjsdhfgjdsfhgjsdhgfjdshgfjsdhgfjhsgdfjhsdgjfhdgfjhgdsf"
android:hint="Presentation"
android:textSize="20dp"/>
</RelativeLayout>

How to sort the views added dynamically

I have this master view where I have a TableLayout to which I add new views programmatically. The master view is given below (masterLayout.xml)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayoutActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:padding="5dp"
android:stretchColumns="*">
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:weightSum="2">
<ScrollView
android:id="#+id/queryScrollView"
android:layout_width="match_parent"
android:layout_span="2"
android:padding="5dp">
<TableLayout
android:id="#+id/queryTableLayoutActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:stretchColumns="*"></TableLayout>
</ScrollView>
</TableRow>
In the above view I add new views to queryTableLayoutActivity Layout. The layout to be added is given below (childLayout.xml)
<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="#drawable/border"
android:orientation="vertical"
tools:context="com.teamtreehouse.oslist.ClassActivity">
<TextView
android:id="#+id/activeClassActivities"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|center_horizontal"
android:text="Active Class Activities"
android:textSize="18dp"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="#+id/classNameActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size"
android:layout_below="#+id/editClassActivity"
android:layout_alignRight="#+id/deleteActivity"
android:layout_alignEnd="#+id/deleteActivity" />
<TextView
android:id="#+id/courseNumberActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/classNameActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/typeActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/courseNumberActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/nameActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/typeActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/dueDateActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/nameActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/descriptionActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/dueDateActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/maxGradeActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/descriptionActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/enterGradeActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/maxGradeActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<Button
android:id="#+id/editClassActivity"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="25dp"
android:background="#drawable/icon_document_edit"
android:onClick="editListener"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/deleteActivity" />
<Button
android:id="#+id/deleteActivity"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="wrap_content"
android:background="#drawable/icon_document_delete"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="deleteActivities"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Class Name :"
android:id="#+id/classNameDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/classNameActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/courseNumberActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Course ID :"
android:id="#+id/courseIDDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/courseNumberActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/courseNumberActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Course Type :"
android:id="#+id/courseTypeDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/typeActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/nameActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Activity Name :"
android:textStyle="bold"
android:id="#+id/activityNameDisplay"
android:layout_alignTop="#+id/nameActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/dueDateActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Due Date :"
android:textStyle="bold"
android:id="#+id/dueDateDisplay"
android:layout_alignTop="#+id/dueDateActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/descriptionActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Description :"
android:textStyle="bold"
android:id="#+id/descDisplay"
android:layout_alignTop="#+id/descriptionActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/maxGradeActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Maximum Grade :"
android:id="#+id/maxGradeDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/maxGradeActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/maxGradeActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Grade :"
android:id="#+id/gradeDisplay"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/enterGradeActivity" />
</RelativeLayout>
I add the childLayout.xml to masterLayout.xml using the following code
TableLayout queryTableLayout = (TableLayout) findViewById(R.id.queryTableLayoutActivity);
LayoutInflater inflater = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
String courseIDValue = c.getString(courseIdIndex);
View newTagView = inflater.inflate(R.layout.activity_class_activities, null);
queryTableLayout.addView(newTagView, activeClassActivityIndex++);
I want to sort childLayout based on dueDateActivity field. How can I achieve this?
First of all you dont want to sort layouts - you want to sort views that you inflated with certain layout. So there is no "childLayout" there is newTagView.
Your question is a form of more general question: how to reorder n relative child views on a parent layout in certain order. This is not about changing their sizes and positions(that's easy, you just individually because you must know all new params for each) this is about reordering them. This problem breaks into 4:
Note: You can't just copy-paste code, you need to make changes based on what you specifically need (like what Parameter you need and how to compare it)
1. Establish the current order and the order in which they need to be :
use
getChildCount()
to know number of TableLayout children, use
getChildAt(i)
to traverse and access them.
Now make the Map that will determine the transition to the new order.
Map<View, Parameter> map = new HashMap<Integer, Parameter>();
for(int i = 0; i < queryTableLayout.getChildCount(); i++){
map.put(queryTableLayout.getChildAt(i),
queryTableLayout.getChildAt(i).findViewById(R.id.dueDateActivity)
.getParameter());
//since newTagView should inherit all RelativeLayout methods after infation
//getParameter() - the method from TextView(dueDateActivity) you need
Now to make the order right you need to sort the map by parameter,
which in Java 8 looks like
LinkedList<View> ordering = map.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(new Comparator<Parameter>(){
/*implement your comparator*/
})/*now when stream is sorted collect it to list*/
.collect(Collectors.toList(Map.Entry::getKey);
Now you have your ordering
2. Method to Swap
easiest way to swap views is to swap their layout params
like in this answer
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) b1.getLayoutParams();
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) b2.getLayoutParams();
b1.setLayoutParams(params2);
b2.setLayoutParams(params1);
Make a method void swap(View A, View B){...} which will work for your RelativeLayout View with that code.
3. Do the thing.
for(int i = 0; i < queryTableLayout.getChildCount(); i++)
swap(queryTableLayout.getChildAt(i), ordering.get(i));
If you want them add dynamically in already sorted place that is much easier You just need to get the right pos, and params like in (1)
and use
public void addView (View child, int index, ViewGroup.LayoutParams params)
According to bedbad's 4 steps I followed first 2 and found solution
I used this answer to help my situation.
This is my sort method (on button click)
public void sortByWeek(View v){
queryTableLayout = (TableLayout) findViewById(R.id.queryTableLayoutActivity);
int childCount = queryTableLayout.getChildCount();
Map<View,String> map = new HashMap<View,String>();
for(int i = 0; i<childCount;i++){
map.put(queryTableLayout.getChildAt(i),((TextView)queryTableLayout.getChildAt(i).findViewById(R.id.dueDateActivity)).getText().toString());
}
Map<View,String> sortedMap = sortByComparator(map,sort);
ArrayList<View> viewSet = new ArrayList<>(sortedMap.keySet());
queryTableLayout.removeAllViews();
for(int i = 0;i<childCount;i++){
queryTableLayout.addView(viewSet.get(i));
}
if(sort){
sort = false;
}
}
public Map<View,String> sortByComparator(Map<View,String> map,final boolean order){
List<Map.Entry<View,String>> list = new LinkedList<Map.Entry<View,String>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<View, String>>() {
#Override
public int compare(Map.Entry<View, String> lhs, Map.Entry<View, String> rhs) {
if(order){
return lhs.getValue().compareTo(rhs.getValue());
}else{
return rhs.getValue().compareTo(lhs.getValue());
}
}
});
Map<View,String> sortedMap = new LinkedHashMap<View,String>();
for(Map.Entry<View,String> entry: list){
sortedMap.put(entry.getKey(),entry.getValue());
}
return sortedMap;
}

Cannot add childs to linearLayout

In my app what i want is to dynamically add buttons to a linear layout based on the length of the array.For example if the length of array is 4 then ,4 buttons should be added to the linear layout.I tried doing that but always my app crashes
The linearlayout which is at the bottom of the xml is where i wanna add the buttons
XMl
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/hsjobslogo"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mrd_home"
android:layout_marginRight="10dp"
android:src="#drawable/home168"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/header"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout2">
<TextView
android:layout_width="0dp"
android:layout_height="33dp"
android:layout_weight="2"
android:layout_marginTop="04dp"
android:text="Resource"
android:gravity="center|start"
android:textColor="#fff"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:padding="10dp"
android:visibility="gone"
android:layout_marginTop="-5dp"
android:src="#drawable/edit" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.35"
android:visibility="visible"
android:id="#+id/ll_main_pi"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_pi_name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" [ "
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_gender" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=","
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_age" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" ]"
style="#style/Job_on_Call"
android:singleLine="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profession"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_profession" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" [ "
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yrs"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_exp_yrs" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Months"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_exp_months" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" ]"
style="#style/Job_on_Call"
android:singleLine="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_area" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_city" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_state" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="true"
android:id="#+id/tv_mrd_country" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="0dp"
android:layout_height="1dp"
android:gravity="bottom"
android:layout_gravity="bottom"
android:background="#drawable/divider_light"
android:layout_weight="0.2" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1.6"
android:layout_height="wrap_content"
android:background="#drawable/rectangle">
<TextView
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="Personal Info"
android:textStyle="bold"
android:textSize="18sp"
android:id="#+id/tv_mrd_header"
style="#style/Job_on_Call"
android:textColor="#fff"
android:layout_marginTop="05dp"
android:layout_marginLeft="19dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/plus_one"
android:visibility="gone"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:id="#+id/iv_mrd_add" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/edit_one"
android:visibility="visible"
android:layout_marginRight="5dp"
android:layout_marginLeft="10dp"
android:id="#+id/iv_mrd_edit" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="5dp"
android:id="#+id/iv_mrd_go_left"
android:src="#drawable/left_arrow" />
<ViewFlipper
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:visibility="visible"
android:id="#+id/mrd_view_flipper"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:id="#+id/ll_my_resource_personal_details">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nationality"
android:textStyle="bold"
android:id="#+id/tv_mrd_nationality_header"
android:layout_marginTop="02dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:id="#+id/ll_my_resource_refference_details">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold"
android:id="#+id/tv_mrd_ref_name_header"
android:layout_marginTop="02dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Document Type"
android:textStyle="bold"
android:id="#+id/dumm1"
android:layout_marginTop="05dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/mrd_profile_pic"
android:layout_margin="10dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</ViewFlipper>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="5dp"
android:id="#+id/iv_mrd_go_right"
android:src="#drawable/right_arrow" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/add_doc_circles"
android:orientation="horizontal">
The layout where i want to create buttons dynamically
</LinearLayout>
</LinearLayout>
Code
if (NewDataSet.get("Table2") instanceof JSONArray) {
isDocPresent = true;
JSONArray array = NewDataSet.getJSONArray("Table2");
numOfDocCircles = array.length();
LayoutInflater layoutInflater;
Button button = new Button(context);
for (int k = 0; k < array.length(); k++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
button.setLayoutParams(params);
button.setText("" + k);
llAddDocCircles.addView(button);
}
Error
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Please help me out whats the problem?
Move your button creation into the for loop, in your code you are basically just adding the same Button instance to the parent layout several times:
for (int k = 0; k < array.length(); k++) {
Button button = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(convertDpToPixel(30), convertDpToPixel(30));
button.setLayoutParams(params);
button.setText("" + k);
llAddDocCircles.addView(button);
}
Use below method to convert dp to pixel.
public static float convertDpToPixel(float dp){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
In your for loop call this method as:
for (int k = 0; k < array.length(); k++) {
llAddDocCircles.addView(addMoreButton("" + k));
}
And your addMoreButton() method is:
public Button addMoreButton(String text) {
Button button = new Button(context);
button.setId(buttonID);
LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
button LayoutParams.setMargins(10, 10, 10, 10); //Add this if you want margin of 10dp
button.setLayoutParams(buttonLayoutParams );
buttonArrayList.add(button);
buttonID++;
return button;
}
And you can easily access these button value by:
private static int buttonID = 0;
private ArrayList<Button> buttonArrayList= new ArrayList<Button>();
// loop to handle each button
for (int i = 0; i < buttonArrayList.size(); i++) {
Button button = buttonArrayList.get(i);
// do your stuff with each button
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}

Programatically give space between buttons in table layout

I have a activity like this.
![Image][1]
Here start ,complete and delivered are buttons.how to give space between buttons.?
Below is my code.
i have a table layout designed in .xml and since its data is dynamic i am adding rows programatically.
/*** xml code ****/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:orientation="vertical" >
<ImageView android:id="#+id/title_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/timg"/>
<TextView android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="63dp"
android:text="Alpha Restaurant"
android:gravity="center_horizontal"
android:layout_toRightOf="#+id/title_image"
android:background="#ED4040"
android:typeface="serif"
android:textColor="#FFF"
android:textStyle="bold"
android:textSize="35sp"/>
<TextView android:id="#+id/tbno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:text="Kitchen Dashboard :"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView android:id="#+id/tvtbno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_toRightOf="#+id/tbno"
android:text="KIT01"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:text="Time :"
android:layout_toRightOf="#+id/tvtbno"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView android:id="#+id/tvtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_toRightOf="#+id/time"
android:text="KIT01"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:text="Date :"
android:layout_toRightOf="#+id/tvtime"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView android:id="#+id/tvdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_toRightOf="#+id/date"
android:text="KIT01"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<ScrollView android:layout_width="fill_parent"
android:layout_height="370dp"
android:id="#+id/sc" android:layout_below="#+id/tvdate">
<TableLayout android:id="#+id/tableLayout1"
android:layout_below="#+id/tbno"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="550dp"
android:shrinkColumns="*"
android:stretchColumns="*">
<TableRow
android:id="#+id/tableRow1"
android:background="#A9D0F5"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="#+id/tv1"
android:text="Lot No"
android:layout_marginLeft="15dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv2"
android:text="Item"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv3"
android:text="Quantity"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv4"
android:text="Order"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv5"
android:text="Notes"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv6"
android:text="Start"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv7"
android:text="Completed"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv8"
android:text="Delivered"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
</TableRow>
</TableLayout>
</ScrollView>
<!-- <TextView android:id="#+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total : "
android:textSize="22sp"
android:typeface="serif"
android:textStyle="bold"
android:layout_marginLeft="230dp"
android:layout_below="#+id/tableLayout1"/>
<TextView android:id="#+id/tvtotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="500 Rs"
android:textSize="22sp"
android:typeface="serif"
android:textStyle="bold"
android:layout_toRightOf="#+id/total"
android:layout_below="#+id/tableLayout1"/>-->
<Button android:id="#+id/refresh"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Refresh"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_marginLeft="340dp"
android:layout_below="#+id/sc"/>
<Button android:id="#+id/home"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Home"
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/refresh"
android:layout_below="#+id/sc"/>
</RelativeLayout>
/**** java code ****/
#Override
protected void onPostExecute(Boolean a) {
// TODO Auto-generated method stub
super.onPostExecute(a);
progressDialog.dismiss();
int count= 0;
try{
allarray = new JSONArray(result);
System.out.println("array is " +allarray.length());
JSONObject json_data=null;
for(int j=0;j<allarray.length();j++){
json_data = allarray.getJSONObject(j);
json_data.getString("PreparationLot");
json_data.getString("Prep_MenuItem");
json_data.getString("Prep_Quantity");
json_data.getString("Prep_Order_Number");
json_data.getString("Prep_Notes");
TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);
TableRow tr = new TableRow(DashBoard.this);
if(count%2!=0) {
tr.setBackgroundColor(Color.rgb(222, 219, 219));
}else{
tr.setBackgroundColor(Color.rgb(255, 255, 255));
}
tr.setId(20);
tr.setLayoutParams(new LayoutParams());
final TextView lotno = new TextView(DashBoard.this);
lotno.setId(200+count);// define id that must be unique
lotno.setText(""+json_data.getString("PreparationLot")); // set the text for the header
lotno.setTextColor(Color.BLACK); // set the color
lotno.setPadding(10, 10, 0, 10); // set the padding (if required)
lotno.setTextSize(14);
tr.addView(lotno);
final TextView itemname = new TextView(DashBoard.this);
itemname.setId(200+count);// define id that must be unique
itemname.setText(""+json_data.getString("Prep_MenuItem")); // set the text for the header
itemname.setTextColor(Color.BLACK); // set the color
itemname.setMaxLines(3);
itemname.setPadding(10, 10, 0, 10); // set the padding (if required)
itemname.setTextSize(14);
tr.addView(itemname);
TextView qty = new TextView(DashBoard.this);
qty.setId(200+count);// define id that must be unique
qty.setText(""+json_data.getString("Prep_Quantity")); // set the text for the header
qty.setTextColor(Color.BLACK); // set the color
qty.setPadding(50, 10, 0, 10); // set the padding (if required)
qty.setTextSize(14);
tr.addView(qty);
TextView order = new TextView(DashBoard.this);
order.setId(200+count);// define id that must be unique
order.setText(""+json_data.getString("Prep_Order_Number")); // set the text for the header
order.setTextColor(Color.BLACK); // set the color
order.setPadding(40, 10, 0, 10); // set the padding (if required)
order.setTextSize(14);
tr.addView(order);
TextView notes = new TextView(DashBoard.this);
notes.setId(200+count);// define id that must be unique
notes.setText(""+json_data.getString("Prep_Notes")); // set the text for the header
notes.setTextColor(Color.BLACK); // set the color
notes.setPadding(40, 10, 0, 10); // set the padding (if required)
notes.setTextSize(14);
tr.addView(notes);
final Button start = new Button(DashBoard.this);
start.setText("Start");
start.setMaxWidth(10);
start.setTextSize(16);
start.setTypeface(null, Typeface.BOLD);
start.setBackgroundColor(Color.RED);
tr.addView(start);
final Button complete = new Button(DashBoard.this);
complete.setText("Complete");
complete.setMaxWidth(10);
complete.setTextSize(16);
complete.setTypeface(null, Typeface.BOLD);
complete.setBackgroundColor(Color.BLUE);
tr.addView(complete);
final Button delivered = new Button(DashBoard.this);
delivered.setText("Delivered");
delivered.setMaxWidth(10);
delivered.setTextSize(16);
delivered.setTypeface(null, Typeface.BOLD);
delivered.setBackgroundColor(Color.BLUE);
tr.addView(delivered);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
count++;
}
add your buttons using following code,
and if you want to increase the space then increase the leftMargin from 10 to 15 or 20.
final Button start = new Button(DashBoard.this);
ViewGroup.MarginLayoutParams lpButton = (ViewGroup.MarginLayoutParams) start.getLayoutParams();
lpButton.setMargins(10, 0, 0, 0);
start.setText("Start");
start.setMaxWidth(10);
start.setTextSize(16);
start.setTypeface(null, Typeface.BOLD);
start.setBackgroundColor(Color.RED);
start.setLayoutParams(lpButton);
tr.addView(start);
final Button complete = new Button(DashBoard.this);
complete.setText("Complete");
complete.setMaxWidth(10);
complete.setTextSize(16);
complete.setTypeface(null, Typeface.BOLD);
complete.setBackgroundColor(Color.BLUE);
complete.setLayoutParams(lpButton);
tr.addView(complete);
final Button delivered = new Button(DashBoard.this);
delivered.setText("Delivered");
delivered.setMaxWidth(10);
delivered.setTextSize(16);
delivered.setTypeface(null, Typeface.BOLD);
delivered.setBackgroundColor(Color.BLUE);
delivered.setLayoutParams(lpButton);
tr.addView(delivered);
try to giving paddingLeft or paddingRight to the buttons. Besides you can give padding to a layout that wraps them.

Categories

Resources