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.
Related
I have a horizontal linear layout that is filled with TextViews from a dynamic list, but it doesn't show all elements since it seems to be out of the parent's bounds. What I need is to fill the space below if needed.
Here's my code:
The xml:
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView_serie" android:contentDescription="imatge_serie"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom_serie"
android:textSize="20dp"
android:layout_marginLeft="8dp"
android:id="#+id/nom_serie" android:contentDescription="nom_serie" tools:text="Nom_serie"
android:layout_toRightOf="#id/imageView_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize = "16dp"
android:layout_marginLeft="8dp"
android:textStyle="bold"
android:id="#+id/any_serie" android:contentDescription="any_serie" tools:text="any_serie"
android:layout_toRightOf="#id/nom_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sinopsis"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/sinopsi_serie" android:contentDescription="sinopsis_serie" tools:text="sinopsis_serie"
android:layout_toRightOf="#id/imageView_serie"
android:layout_below = "#id/nom_serie"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:id="#+id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
android:layout_centerVertical="true"
style="?android:attr/ratingBarStyleSmall"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Puntuacio"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/puntuacio" android:contentDescription="puntuacio" tools:text="puntuacio"
android:layout_toRightOf="#id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Casting:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/casting" android:contentDescription="casting" tools:text="casting"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/cast" android:contentDescription="cast" tools:text="cast"
android:layout_toRightOf="#id/casting"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Direccio:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/direccio" android:contentDescription="direccio" tools:text="direccio"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/dir" android:contentDescription="dir" tools:text="dir"
android:layout_toRightOf="#id/direccio"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temporades:"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/nTemporades" android:contentDescription="nTemporades" tools:text="nTemporades"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comentaris"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/titol_comentaris_serie" android:contentDescription="titol_comentari_serie" tools:text="titol_comentari_serie"
/>
</RelativeLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/panell"
android:orientation="horizontal">
</LinearLayout>
And this how I generate the text views from the list:
LinearLayout panell = (LinearLayout) findViewById(R.id.panell);
for (int i = 1; i <= 20; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView btn = new TextView(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
//btn.setBackgroundColor(Color.rgb(70, 80, 90));
panell.addView(btn, params);
btn = ((TextView) findViewById(id_));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});
}
Here's my :
output
I recommend Google's flexbox-layout project: https://github.com/google/flexbox-layout
A FlexboxLayout (when configured correctly) has the ability to add any number of child TextViews in a line (like LinearLayout would), but will automatically "wrap" to the next line if a child wouldn't fit.
Why not try to use a ListView , if u add the same propreties every time
I have created a tablelayout with the header row added in the XML file and the remaining content rows filled in dynamically.
For some reason, the last row is not visible.
I looked through other related posts and everything seems okay in my code.
Appreciate if someone could take a look and point out the problem.
XML code:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.bluechipsys.mcpackagemanager.PMActivity"
android:id="#+id/view_layout">
<ProgressBar
android:id="#+id/installProgressBar"
android:indeterminate="true"
style="#android:style/Widget.Holo.ProgressBar.Large"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
<ScrollView
android:id="#+id/textAreaScroller"
android:layout_above="#+id/btn_reload"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scrollbars="vertical"
android:fadeScrollbars="false"
android:singleLine="false">
<TextView
android:text="#string/msg_default"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:id="#+id/viewLog"
android:maxLines="1000"
android:scrollHorizontally="false"
android:scrollbars="vertical"
android:singleLine="false"
android:background="#drawable/border" />
</ScrollView>
<TextView
android:id="#+id/versionInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableLayoutScroller"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/tableLayoutScroller"
android:text="#string/tv_versioninfo"
android:textStyle="bold" />
<ScrollView
android:id="#+id/tableLayoutScroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="false"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="false"
android:layout_centerHorizontal="false"
android:layout_centerInParent="false"
android:layout_alignParentStart="#+id/textAreaScroller"
android:layout_alignRight="#+id/textAreaScroller"
android:layout_alignEnd="#+id/textAreaScroller"
android:layout_above="#+id/textAreaScroller"
android:fadeScrollbars="false"
android:scrollbars="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:id="#+id/tbl_packagelist"
android:orientation="vertical"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="false"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="false"
android:layout_centerHorizontal="false"
android:layout_centerInParent="false"
android:layout_marginTop="30dp"
android:layout_alignParentStart="#+id/textAreaScroller"
android:layout_alignRight="#+id/textAreaScroller"
android:layout_alignEnd="#+id/textAreaScroller"
android:layout_above="#+id/textAreaScroller"
android:scrollHorizontally="false"
android:stretchColumns="1,2,3,4,5"
android:shrinkColumns="2"
android:background="#drawable/border_table">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="false"
android:background="#android:drawable/list_selector_background">
<CheckBox
android:id="#+id/cbSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/title_cell_shape"
android:onClick="onCheckboxClicked" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/tvState"
android:background="#drawable/title_cell_shape"
android:editable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_package"
android:id="#+id/tvPackage"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:textAlignment="center"
android:paddingLeft="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_instver"
android:id="#+id/tvInstver"
android:singleLine="false"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:textAlignment="center"
android:paddingLeft="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_latestver"
android:id="#+id/tvLatestver"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:paddingLeft="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_size"
android:id="#+id/tvSize"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:paddingLeft="2dp" />
</TableRow>
</TableLayout>
</ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_reload"
android:id="#+id/btn_reload"
android:background="#android:color/transparent"
android:drawableTop="#drawable/reload"
android:layout_marginTop="7dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_apply"
android:id="#+id/btn_apply"
android:background="#android:color/transparent"
android:drawableTop="#drawable/apply"
android:layout_alignTop="#+id/btn_reload"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_close"
android:id="#+id/btn_close"
android:background="#android:color/transparent"
android:drawableTop="#drawable/close"
android:layout_alignTop="#+id/btn_reload"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textAreaScroller"
android:layout_alignEnd="#+id/textAreaScroller" />
</RelativeLayout>
Java Code:
private void clearTableRows() {
Logger.d(TAG, "Clearing table rows");
TableLayout main_table = (TableLayout) findViewById(R.id.tbl_packagelist);
int rowCount = main_table.getChildCount();
main_table.removeViews(1, rowCount - 1); // Leave header row intact
}
// Create table rows and add to the GUI
private void createTableRows() {
Logger.d(TAG, "Creating table rows");
final TableLayout main_table = (TableLayout) findViewById(R.id.tbl_packagelist);
Drawable background;
// Define layout parameters
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams itemCBParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams itemIVParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
itemIVParams.gravity = Gravity.CENTER; // Centre the image display
TableRow.LayoutParams itemParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
ViewGroup.MarginLayoutParams mLayoutItemParams = new ViewGroup.MarginLayoutParams (itemParams);
MarginLayoutParamsCompat.setMarginStart(mLayoutItemParams, 4); // Need this workaround for API level < 17
for (MCPackage pkg: list_packages) {
// Create the TableRow and widgets to add to the TableRow
TableRow tbl_row = new TableRow(this);
CheckBox cb_select = new CheckBox(this);
ImageView iv_state = new ImageView(this);
TextView tv_package = new TextView(this);
TextView tv_instver = new TextView(this);
TextView tv_currver = new TextView(this);
TextView tv_pkgsize = new TextView(this);
int index = list_packages.indexOf(pkg);
if(index % 2 == 0) {
background = getResources().getDrawable(R.drawable.cell_shape_white);
} else {
background = getResources().getDrawable(R.drawable.cell_shape_grey);
}
// Set GUI parameters for the TableRow
tbl_row.setLayoutParams(rowParams);
tbl_row.setBackground(background);
tbl_row.setId(index); // Set ID to match index of list_packages
// Initialize the cells
cb_select.setLayoutParams(itemCBParams);
cb_select.setId(index + CHECKBOX_ID);
cb_ids[index] = (index + CHECKBOX_ID);
cb_select.setOnClickListener(checkboxOnClick());
cb_select.setOnCheckedChangeListener(checkboxOnCheckChange());
iv_state.setLayoutParams(itemIVParams);
iv_state.setImageResource(pkg.getState().getImageResourceId());
iv_state.setId(index + IVSTATE_ID);
iv_state_ids[index] = (index + IVSTATE_ID);
tv_package.setLayoutParams(itemParams);
tv_package.setText(pkg.getPackageName());
tv_package.setGravity(Gravity.FILL);
tv_package.setTextAppearance(this, android.R.style.TextAppearance_Small);
tv_package.setTextSize(12);
tv_package.setTextColor(Color.DKGRAY);
tv_package.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv_instver.setLayoutParams(itemParams);
tv_instver.setText(pkg.getInstalledVersion());
tv_instver.setGravity(Gravity.FILL);
tv_instver.setTextSize(12);
tv_instver.setTextColor(Color.BLUE);
tv_instver.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv_currver.setLayoutParams(itemParams);
tv_currver.setText(pkg.getUpdateVersion());
tv_currver.setGravity(Gravity.FILL);
tv_currver.setTextSize(12);
tv_currver.setTextColor(Color.DKGRAY);
tv_currver.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv_pkgsize.setLayoutParams(itemParams);
tv_pkgsize.setText(String.valueOf(((int) pkg.getPackageSize()) / KBINBYTES) + "K");
tv_pkgsize.setGravity(Gravity.FILL);
tv_pkgsize.setTextSize(12);
tv_pkgsize.setTextColor(Color.BLUE);
tv_pkgsize.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
// Add the cells to the row
tbl_row.addView(cb_select);
tbl_row.addView(iv_state);
tbl_row.addView(tv_package);
tbl_row.addView(tv_instver);
tbl_row.addView(tv_currver);
tbl_row.addView(tv_pkgsize);
main_table.addView(tbl_row); // Add the row to the table
tbl_row.setOnClickListener(rowClickListener);
tbl_row.setOnLongClickListener(rowLongClickListener);
registerForContextMenu(tbl_row); // Register the table row for context menu
Logger.d(TAG, "Added package: " + pkg.getPackageName());
}
}
I'm not sure if it is still a problem, but try to use NestedScrollView instead of ScrollView.
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;
}
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.
i have a custom listview i want to add one lat row to it which will calculate total of all the list when i scroll the last row gets added again and again
if(position==searchResult.size() && searchResult.size()!=1)
{
holder.checkImg.setVisibility(ImageView.GONE);
holder.fvtImg.setVisibility(ImageView.GONE);
holder.type.setVisibility(TextView.GONE);
holder.name.setVisibility(TextView.GONE);
holder.offer_price.setVisibility(TextView.GONE);
holder.real_price.setVisibility(TextView.GONE);
holder.total.setVisibility(TextView.VISIBLE);
holder.total_price.setVisibility(TextView.VISIBLE);
//DecimalFormat df = new DecimalFormat("0.00");
//holder.txt_distance.setText(df.format(mData.get(position).get("distance")).toString()+"...");
DecimalFormat twoDForm = new DecimalFormat("0.00");
String totalPrice = (twoDForm.format(mTotalPrice)+"").replace('.',',');
holder.total.setText("Total");
holder.total_price.setText("Kr. "+totalPrice);
}
else
{
holder.total.setVisibility(TextView.GONE);
holder.total_price.setVisibility(TextView.GONE);
if(isEdit){
/*LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.leftMargin=30;
holder.fvtImg.setLayoutParams(lp);*/
/* TranslateAnimation anim = new TranslateAnimation(0,35,0,0);
holder.fvtImg.setAnimation(anim);
anim.setFillAfter(true);*/
holder.fvtImg.setVisibility(ImageView.GONE);
holder.name.setPadding(10,0, 0, 0);
holder.type.setPadding(10,0, 0, 0);
holder.checkImg.setVisibility(ImageView.VISIBLE);
Layout file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/list_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/price_list_background"
/>
<CheckBox android:id="#+id/checkBox"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:visibility="gone">
</CheckBox>
<ImageView
android:id="#+id/img_pol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pol_icon_tag"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="#+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="15dp"
android:textSize="16dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginLeft="35dp"
/>
<TextView
android:id="#+id/txt_type"
android:layout_width="200dp"
android:layout_height="13dp"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
android:layout_marginLeft="35dp"
/>
<TextView
android:id="#+id/txt_real_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
android:layout_marginLeft="90dp"
/>
<TextView
android:id="#+id/txt_offer_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_marginTop="31dp"
android:layout_marginLeft="190dp"
/>
<TextView
android:id="#+id/txt_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:text="Total"
android:visibility="gone"
/>
<TextView
android:id="#+id/txt_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="230dp"
android:text="kr. 222.22"
android:visibility="gone"
/>
</RelativeLayout>
Have you considered using a footer instead of placing the last item inside the ListView?
What you do is, before you set the adapter of the ListView, you inflate a footer layout (or perhaps just a TextView, if that's all you need). Store a reference to it, and then add it to the ListView:
TextView myFooter = new TextView( context );
myListView.addFooterView( myFooter );
Then when you add items to the list or when the calculation needs to occur, you just calculate and set the text of myFooter.
myFooter.setText( myCalculatedValue );
Setting the text of the footer must ofcourse happen on the UI-thread so if you use a background thread or a AsyncTask to add to your listview, be sure you update stuff the right place.