Adding view to bottom of Table Layout - android

I am working on an android project and I have a table layout. I have the Table Layout with one row in the XML layout file for the table headings. The rest of the table is populated dynamically from the result of an API.
I'm adding the views to the table layout, but the rows I'm adding dynamically appear above the table headings.
Below is my XML layout:
<?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">
<include layout="#layout/toolbar" />
<TableLayout android:id="#+id/tblAuthenticatedDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="false"
android:layout_margin="10dp"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/cardToolbar">
<TextView
android:text="Device Name"
android:layout_width="0dp"
android:layout_weight="1"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Last Logged In"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="From"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white"/>
</TableRow>
</LinearLayout>
Below is how I am populating the rest of the table
JSONArray dataObject = jsonObject.getJSONArray(Defines.JSONElements.DATA);
for (int i = 0; i < dataObject.length(); i++)
{
TableRow row = (TableRow)getLayoutInflater().inflate(R.layout.table_row_layout, tblAuthenticatedDevices, false);
JSONObject deviceData = dataObject.getJSONObject(i);
TextView txtDeviceName = new TextView(AuthenticatedDevices.this);
txtDeviceName.setText(deviceData.getString("DeviceName"));
row.addView(txtDeviceName);
TextView txtLastLoggedIn = new TextView(AuthenticatedDevices.this);
txtLastLoggedIn.setText(deviceData.getString("LastLoggedIn"));
row.addView(txtLastLoggedIn);
TextView txtLocation = new TextView(AuthenticatedDevices.this);
txtLocation.setText(String.format("%s - %s", deviceData.getString("Country"), deviceData.getString("City")));
row.addView(txtLocation);
tblAuthenticatedDevices.addView(row);
}
Just to re-iterate, the row data that I am dynamically adding to the table appears above the row headings I've added to my XML layout. I expected the row headings to appear, then below that would appear the rows that I am adding to the table.

Okay, so I've been researching regarding TableLayouts where there is always a header included to them, and basically, all of them just adds the TableRow header first via code, in order for it to be on top. So here's what I did as a reference for you.. First, I just did a separate layout for the header.
tr_header.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textStyle="bold"
android:id="#+id/tv_device_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="device name" />
<TextView
android:id="#+id/tv_last_login"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="last_login" />
<TextView
android:id="#+id/tv_location"
android:layout_width="0dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="location" />
</TableRow>
Then in the layout of the activity where the tableLayout is, I just kept it empty, like so...
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.aalawi.myapplication.MainActivity">
<TableLayout
android:id="#+id/tblAuthenticatedDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:shrinkColumns="false"
android:stretchColumns="1" />
</LinearLayout>
And then in the code, I just add first the header, then iterated on some sample data afterwards. Also, I presume you also have a different layout for your table_rows as mentioned in the code you provided (named table_row_layout), so I just did something similar.
tl_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="#+id/tv_device_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
<TextView
android:id="#+id/tv_last_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
<TextView
android:id="#+id/tv_location"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
</TableRow>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TableLayout tl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
tl = (TableLayout) findViewById(R.id.tblAuthenticatedDevices);
List<String> deviceNames = Arrays.asList("1", "2", "3");
List<String> lastLogin = Arrays.asList("11", "22", "33");
List<String> location = Arrays.asList("111", "222", "333");
// Add Header First...
TableRow header = (TableRow) getLayoutInflater().inflate(R.layout.tr_header, null);
tl.addView(header);
// Fill additional rows...
for (int i = 0; i < deviceNames.size(); i++) {
TableRow row = (TableRow) getLayoutInflater().inflate(R.layout.tl_row, tl, false);
TextView txtDeviceName = (TextView) row.findViewById(R.id.tv_device_name);
TextView txtLastLoggedIn = (TextView) row.findViewById(R.id.tv_last_login);
TextView txtLocation = (TextView) row.findViewById(R.id.tv_location);
txtDeviceName.setText(deviceNames.get(i));
txtLastLoggedIn.setText(lastLogin.get(i));
txtLocation.setText(location.get(i));
tl.addView(row);
}
}
}
That resulted to this.

Add this before you create your data rows to create the header:
TableRow rowHeader = new TableRow(context);
rowHeader.setBackgroundColor(Color.parseColor("#c0c0c0"));
rowHeader.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
//Title header for each column here
String[] headerText={"ID","NAME","TYPE"};
for(String c:headerText) {
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv.setGravity(Gravity.CENTER);
tv.setTextSize(18);
tv.setPadding(5, 5, 5, 5);
tv.setText(c);
rowHeader.addView(tv);
}
tableLayout.addView(rowHeader);

I've realised what I've done wrong when I took another look this morning.
I've ended the Table Layout (i.e. I have <TableLayout... />) then I've added my TableRow but its outside the TableLayout.
Instead I have put my headings inside the TableLayout tag as below and it works as expected.
<TableLayout android:id="#+id/tblAuthenticatedDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="false"
android:layout_margin="10dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/cardToolbar">
<TextView
android:text="Device Name"
android:layout_width="0dp"
android:layout_weight="1"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Last Logged In"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="From"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white"/>
</TableRow>
</TableLayout>

Related

how to create 2 column layout with merging column?

how to create this kind of layout in android
i have tried using table layout. but getting error in creating this.
please check my code i am able to display images. 1 image in 1 row. but i want 2 small images in 1 row. and continues.
here is my code
private void getBannerList() {
final Call<BannerList_Model> itemMainResponse = APIHandler.getApiService().bannerList(getFiledMapForItemList());
itemMainResponse.enqueue(new Callback<BannerList_Model>() {
#Override
public void onResponse(Response<BannerList_Model> response, Retrofit retrofit) {
BannerList_Model itemResponse = null;
itemResponse = response.body();
if (itemResponse == null) {
UIUtils.showToast(getContext(), "Server Error !");
} else {
String msg = itemResponse.getMsg().toString();
if (msg.equals("success")) {
userList = itemResponse.getBannerList();
if(userList != null) {
TableLayout table = (TableLayout) view.findViewById(R.id.tblBanner_list);
table.setColumnStretchable(0,true);
table.setColumnStretchable(1,true);
int tsize = userList.size();
TextView[] t1 = new TextView[tsize];
ImageView[] img = new ImageView[tsize];
for(int i=0; i<userList.size();i++)
{
TableRow row = new TableRow(getContext());
String banner_size = userList.get(i).getBannerSize().toString();
img[i] = new ImageView(getContext());
Picasso.with(getContext())
.load(Constants.VIDEO_URL + userList.get(i).getBannerUrl())
.into(img[i]);
row.addView(img[i]);
table.addView(row);
}
}
}
}
}
}
});
}
here is the video how i want layout
http://shrivivah.com/layout.mp4
if there is 2 small images than it will display in 1 row.
if there is 2 medium or full size image than it will display in 1 row each.
if 1 small and 2 medum than 1 image per row to display.
layout image
Your mistake is that you are adding TextView to TableRow(row) but you didn't add row to TableLayout. Here is an example for what you need. You can set some margins and add button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
</TableRow>
</TableLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.tops.stackoverflow.MainActivity">
<LinearLayout
android:id="#+id/llShortcutParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Button1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

Dynamically added tablerow only last row not visible

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.

How to get the width of each child contained in the list_item using ArrayAdapter

I am creating a ListView which has some some static headers as shown in the following picture:
I want that the header columns (Date, Status, Latitude, and Longitude) adapts their width according to the width of their according column.
What i am trying is to get the Layout of the list item associated with the adapter so that i can find the width of its childs (but it seems to be impossible) in my ListActivity.
then i have tried in my custom ArrayAdapter's getView method to retrieve the width of each TextView associated with the list item as shown in the following code: (But it returns null, I think it can not return width before redering the textView.)
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
DispatchHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new DispatchHolder();
holder.tvcurrentDateTime = (TextView) row
.findViewById(R.id.tvCurrentDateTimeLog);
holder.tvAction = (TextView) row.findViewById(R.id.tvActionLog);
holder.tvLatitude = (TextView) row.findViewById(R.id.tvlatitudeLog);
holder.tvLongitude = (TextView) row
.findViewById(R.id.tvlogitudeLog);
row.setTag(holder);
} else {
holder = (DispatchHolder) row.getTag();
}
holder.tvcurrentDateTime.setText(data.get(position).getTime());
int i = holder.tvcurrentDateTime.getWidth() <------- Here it is return 0
holder.tvAction.setText(data.get(position).getAction());
holder.tvLatitude.setText(data.get(position).getLatitude());
holder.tvLongitude.setText(data.get(position).getLogitude());
return row;
}
I have searched on it but i could not find a way to do so.
Here is the related code:
My ListActivity:
public class Activity_Log extends ListActivity {
PickupStatusDao puDao;
LogAdapter adapter;
List<LogModel> listOfDispatch;
private Object mItem;
private View childView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_xml);
puDao = new PickupStatusDao(this);
List<LogModel> lstResult = puDao.readAllLogData();
adapter = new LogAdapter(this, R.layout.log_item_row, lstResult);
setListAdapter(adapter);
}
}
My list_item (R.layout.log_item_row):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/BodyTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow2"
style="#style/BodyRow"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvCurrentDateTimeLog"
style="#style/BodyText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="this time"
android:textSize="20sp" />
<TextView
android:id="#+id/tvActionLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="this is action"
android:textSize="20sp" />
<TextView
android:id="#+id/tvlogitudeLog"
style="#style/BodyText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="logitude"
android:textSize="20sp" />
<TextView
android:id="#+id/tvlatitudeLog"
style="#style/BodyText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="latuide"
android:textSize="20sp" />
</TableRow>
</TableLayout>
List Activity Layout (R.layout.activity_log_xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/HeaderTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow style="#style/HeaderRow" >
<TextView
style="#style/HeaderText"
android:layout_width="239dp"
android:text="Date" />
<TextView
style="#style/HeaderText"
android:layout_width="156dp"
android:text="Status" />
<TextView
style="#style/HeaderText"
android:layout_width="121dp"
android:text="Latitude" />
<TextView
style="#style/HeaderText"
android:layout_width="121dp"
android:text="Longitude" />
</TableRow>
</TableLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
</LinearLayout>
Unfortunately, this just isn't something you can do efficiently with an AdapterView. Since they tie together an arbitrarily long data source, but only display a small subset of the data at any one time, there is no efficient way to determine what the maximum width of the data will be without measuring a view for each piece of data.
I would suggest maybe doing some work when initializing the data source to find the average length of the text that will be displayed, and setting each column to some value that makes sense for the data. Or, just use the same values for each column (be that some combination of layout_weights or some hardcoded values).
I have achieved this by XML android:layout_weight property, it works on all android devices with different densities, Here is the updated UI picture:
the updated XML of ListActivity (activity_log_xml.xml) and for list_item (log_item_row.xml) are as follows respectively:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/HeaderTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
style="#style/HeaderRow"
android:weightSum="10" >
<TextView
android:id="#+id/dateHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Date" />
<TextView
android:id="#+id/statusHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_weight="4"
android:text="Status" />
<TextView
android:id="#+id/latitudeHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_weight="1.5"
android:text="Latitude" />
<TextView
android:id="#+id/longitudeHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_weight="1.5"
android:text="Longitude" />
</TableRow>
</TableLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/BodyTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow2"
style="#style/BodyRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10" >
<TextView
android:id="#+id/tvCurrentDateTimeLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="this time" />
<TextView
android:id="#+id/tvActionLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="this is action" />
<TextView
android:id="#+id/tvlogitudeLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="logitude" />
<TextView
android:id="#+id/tvlatitudeLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="latuide" />
</TableRow>
</TableLayout>

Align table rows vertically inside table to cover-up entire area

I have a table that covers-up entire screen and contains variable number of rows.
I would like to know how can i distribute these rows vertically uniformly, so that they take up entire tables' space.
I have tried layout_weight, and weightSum, but its not working.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1,2"
android:id="#+id/maintable"
android:weightSum="3" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_weight="1" >
<TextView
android:text="text 1_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="text 2_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="text 3_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_weight="1" >
<TextView
android:text="text 1_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="text 2_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="text 3_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_weight="1" >
<TextView
android:text="text 1_3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="text 2_3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="text 3_3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableRow>
</TableLayout>
</LinearLayout>
Your code looks wrong as your are not setting 'fill parent' constraint for the vertical
axis
<TableRow
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_weight="1" >
should be
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
Please check below code. This will cover entire screen with table rows uniformly
grid_layout.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
grid_layout is filled with text views in code as below
public class GridLayoutActivity extends Activity {
private static final int NO_OF_ROWS = 5;
private static final int NO_OF_COLUMS = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
TableLayout gridview = (TableLayout) findViewById(R.id.tableLayout);
for (int rowCnt = 0 ; rowCnt < NO_OF_ROWS; rowCnt++) {
TableRow row = new TableRow(this);
TableLayout.LayoutParams tableLayoutLP = new TableLayout.LayoutParams();
tableLayoutLP.weight = 1;
row.setLayoutParams(tableLayoutLP);
for (int column = 0; column < NO_OF_COLUMS; column++) {
TextView tv = new TextView(this);
TableRow.LayoutParams tableRowLP = new TableRow.LayoutParams();
tableRowLP.setMargins(2, 2, 2, 2);
tableRowLP.weight=1;
tableRowLP.height = TableRow.LayoutParams.MATCH_PARENT;
tv.setTextColor(Color.WHITE);
tv.setGravity(Gravity.CENTER);
tv.setBackgroundColor(Color.DKGRAY);
tv.setLayoutParams(tableRowLP);
tv.setText("YourText");//Add your text here
row.addView(tv);
}
gridview.addView(row);
}
}
}

android -> relative-layout changes button text alignment on click

This is my main.xml:`
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:addStatesFromChildren="false"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/header"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_width="fill_parent"
android:layout_alignParentTop="true">
<TableRow>
<TextView
android:id="#+id/leftHeader"
android:textColor="#android:color/black"
android:layout_margin="1dp"
android:gravity="center_horizontal"
android:layout_weight="1"
android:background="#ffcccccc"
android:text="textview1"
android:layout_width="30dip" />
<TextView
android:id="#+id/rightHeader"
android:textColor="#android:color/black"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_margin="1dp"
android:background="#android:color/white"
android:text="textview2"
android:layout_width="70dip" />
</TableRow>
</TableLayout>
<ScrollView
android:id="#+id/table_scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_below="#+id/header"
android:layout_above="#+id/buttonTable">
<TableLayout
android:id="#+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<TableRow
android:id="#+id/maintableRow">
<TextView
android:id="#+id/maintableLeft"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginRight="1dp"
android:layout_marginLeft="1dp"
android:text="textview1"
android:layout_width="30dip" />
<TextView
android:id="#+id/maintableRight"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:text="textview2"
android:layout_width="70dip" />
</TableRow>
</TableLayout>
</ScrollView>
<TableLayout
android:id="#+id/buttonTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_alignParentBottom="true">
<TableRow>
<Button
android:id="#+id/button_ResetData"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Reset Data"
android:layout_gravity="center"
android:layout_weight="1" />
<Button
android:id="#+id/button_Refresh"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Refresh"
android:gravity="center"
android:layout_weight="1" />
</TableRow>
</TableLayout>
`
After I press one of the buttons its text alignment goes to the left, how can I make it stay in center?
something similar happens with the first table (header) (textview1, textview2) but here it also reduses the textsize the amount of the text in the view...
EDIT: I managed to find the code that causes the change but I don't know how to fix this.
Maybe some one can explain me why does this happen and how to fix it?
private void fillTableView(List<BatteryInfo> list) {
TextView leftHeader = (TextView) findViewById(R.id.leftHeader);
TextView rightHeader = (TextView) findViewById(R.id.rightHeader);
LayoutParams leftHeaderLayoutParams = leftHeader.getLayoutParams();
LayoutParams rightHeaderLayoutParams = rightHeader.getLayoutParams();
TableLayout contentTable = (TableLayout) findViewById(R.id.maintable);
contentTable.removeAllViews();
for (int i = list.size() - 1; i >= 0; i--) {
TextView tv1 = new TextView(this);
tv1.setTextColor(Color.BLACK);
tv1.setGravity(Gravity.CENTER);
tv1.setBackgroundColor(Color.LTGRAY);
tv1.setLayoutParams(leftHeaderLayoutParams);
tv1.setText(String.valueOf(list.get(i).getLevel()));
TextView tv2 = new TextView(this);
tv2.setTextColor(Color.BLACK);
tv2.setGravity(Gravity.CENTER);
tv2.setBackgroundColor(Color.WHITE);
tv2.setLayoutParams(rightHeaderLayoutParams);
tv2.setText(list.get(i).getDurationTimeInString());
TableRow tblRow = new TableRow(this);
tblRow.addView(tv1);
tblRow.addView(tv2);
contentTable.addView(tblRow);
}
}
After I use removeAllViews or any addView function the layout of the top most table changes as I explained before. Why does this happen?
You can fix this by changing the android:layout_width="30dip" and android:layout_width="70dip" in leftHeader and rightHeader to android:layout_width="fill_parent". I have to admit that I'm not entirely certain why that is, though.
Instead of clearing and re-filling a TableLayout every time, though, you might want to consider using a ListAdapter. It's much more suited to what you're doing (and will probably perform better, too).
UPDATE NUMBER 2:
Notice the change in the main.xml and the change in the onCreate method. This will make your header static while letting your listview scroll, and changing from TableLayout to LinearLayout for your buttons will fix the alignment issue.
Here's how to do this with a custom adapter instead of TableLayout (which isn't really meant for dynamic data like this):
Change you main.xml to this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:addStatesFromChildren="false" android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:id="#+id/header">
<TextView android:id="#+id/leftHeader" android:textColor="#android:color/black"
android:layout_margin="1dp" android:gravity="center_horizontal"
android:layout_weight="1" android:background="#ffcccccc" android:text="textview1"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="#+id/rightHeader" android:textColor="#android:color/black"
android:gravity="center_horizontal" android:layout_weight="1"
android:layout_margin="1dp" android:background="#android:color/white"
android:text="textview2" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:id="#+id/buttonTable" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_alignParentBottom="true" >
<Button android:id="#+id/button_ResetData"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Reset Data" android:layout_gravity="center"
android:layout_weight="1" />
<Button android:id="#+id/button_Refresh"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Refresh" android:gravity="center"
android:layout_weight="1" />
</LinearLayout>
<ListView android:id="#+id/listView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"
android:fadingEdge="none"
android:layout_below="#id/header" android:layout_above="#id/buttonTable">
</ListView>
Add XML layout files for your rows (row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="#+id/maintableLeft"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_marginRight="1dp" android:layout_marginLeft="1dp"
android:gravity="center_horizontal" android:background="#ffcccccc"
android:textColor="#android:color/black" android:layout_width="fill_parent" />
<TextView android:id="#+id/maintableRight"
android:layout_height="wrap_content" android:background="#ffcccccc"
android:textColor="#android:color/black" android:layout_weight="1"
android:layout_marginLeft="1dp" android:layout_marginRight="1dp"
android:gravity="center_horizontal" android:layout_width="fill_parent" />
</LinearLayout>
Now everything is in place to create a custom class from BaseAdapter to display your information (and make your layout work better):
class CustomAdapter extends BaseAdapter {
private List<BatteryInfo> info;
private LayoutInflater inflater;
public CustomAdapter(Context context, List<BatteryInfo> x) {
this.info = x;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return info.size();
}
public Object getItem(int position) {
return info.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
v = inflater.inflate(R.layout.row, parent, false);
BatteryInfo b = (BatteryInfo) getItem(position);
((TextView) v.findViewById(R.id.maintableLeft)).setText(b
.getDurationTimeInString());
((TextView) v.findViewById(R.id.maintableRight)).setText(b
.getLevel());
return v;
}
}
Modify your onCreate method to inflate the header, add it, and then bind your data to your ListView using the new CustomAdapter (I've created a fake BatteryInfo class for this example, you'll need to modify things slightly to get this working):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView lv = (ListView) this.findViewById(R.id.listView);
final Context context = this;
Button refresh = (Button) this.findViewById(R.id.button_Refresh);
refresh.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
List<BatteryInfo> x = new ArrayList<BatteryInfo>();
x.add(new BatteryInfo());
x.add(new BatteryInfo());
x.add(new BatteryInfo());
x.add(new BatteryInfo());
lv.setAdapter(new CustomAdapter(context, x));
}
});
}

Categories

Resources