In my app I have a table with somewhat complex layout: it is a 3x3 table, with the top and left being static headers, and the other four cells each containing a table again with my information.
This internal table has two options, and I programmatically select which of the two to use, and add them to the table. So far so good.
Now when updating the information in the table, a second table is added to the cell, instead of the original one being replaced. So after a few updates, my main table becomes huge, and the cells in it really high with a number of internal tables. Of course that's not the idea.
private int setTable(int minAPI, int maxAPI, TableLayout tableId) {
// Get the levels that belong to the API.
String minLevel = getLevel(minAPI);
String maxLevel = getLevel(maxAPI);
// Get an inflater to inflate the table layouts.
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Get the layout of the API/Level table.
// Which one to use for the levels depends on whether the levels are
// the same or not.
TextView textLevelFrom = null;
TextView textLevelTo = null;
TableLayout tableLayout = null;
if (minLevel.equals(maxLevel)) {
Log.v(TAG, "Setting up api_table1.");
tableLayout = (TableLayout) inflater.inflate(R.layout.api_table1, tableId);
textLevelFrom = (TextView) tableLayout.findViewById(R.id.textLevel);
textLevelFrom.setText(minLevel);
textLevelFrom.setTextColor(getColour(minAPI));
}
else {
Log.v(TAG, "Setting up api_table2.");
tableLayout = (TableLayout) inflater.inflate(R.layout.api_table2, tableId);
textLevelFrom = (TextView) tableLayout.findViewById(R.id.textLevelFrom);
textLevelFrom.setText(minLevel);
textLevelFrom.setTextColor(getColour(minAPI));
textLevelTo = (TextView) tableLayout.findViewById(R.id.textLevelTo);
textLevelTo.setText(maxLevel);
textLevelTo.setTextColor(getColour(maxAPI));
}
Log.v(TAG, "Populating the table.");
TextView textAPIFrom = (TextView) tableLayout.findViewById(R.id.textAPIFrom);
textAPIFrom.setText(""+minAPI);
textAPIFrom.setTextColor(getColour(minAPI));
TextView textAPITo = (TextView) tableLayout.findViewById(R.id.textAPITo);
textAPITo.setText(""+maxAPI);
textAPITo.setTextColor(getColour(maxAPI));
textLevelFrom.setText(minLevel);
textLevelFrom.setTextColor(getColour(minAPI));
return tableLayout.getId();
}
The TableLayout TableId is previously found by a call like
tableCurrentGeneral = (TableLayout) findViewById(R.id.CurrentGeneral);
one for each of the four cells. The main table is defined in main.xml; the cells of these table are an empty TableLayout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.squirrel.hkairpollution.MySupportMapFragment"
/>
<!-- class="com.google.android.gms.maps.SupportMapFragment" -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tableAPI"
android:background="#color/translucent_white"
android:layout_alignBottom="#id/map">
<!-- First row: headers. -->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/general"
android:textStyle="bold"
android:textColor="#color/black"
android:gravity="center"
android:paddingRight="8dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/roadside"
android:textStyle="bold"
android:textColor="#color/black"
android:gravity="center" />
</TableRow>
<!-- Second row: current API -->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/current"
android:textStyle="bold"
android:textColor="#color/black"
android:paddingRight="5dip"
android:gravity="center" />
<!-- Current API, general stations -->
<TableLayout
android:id="#+id/CurrentGeneral"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="8dip" />
<TableLayout
android:id="#+id/CurrentRoadside"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<!-- Third row: forecast API -->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/forecast"
android:textStyle="bold"
android:textColor="#color/black"
android:paddingRight="5dip"
android:gravity="center" />
<!-- Forecast API, general stations -->
<TableLayout
android:id="#+id/ForecastGeneral"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="8dip" />
<TableLayout
android:id="#+id/ForecastRoadside"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<ProgressBar
android:id="#+id/progressBar1"
android:visibility="invisible"
android:layout_width="48dip"
android:layout_height="48dip"
android:layout_alignTop="#id/map"
android:layout_alignLeft="#id/map"/>
<ImageView
android:id="#+id/refresh"
android:layout_width="48dip"
android:layout_height="48dip"
android:contentDescription="#string/refresh"
android:layout_alignTop="#id/map"
android:layout_alignLeft="#id/map"
android:src="#drawable/ic_menu_refresh" />
</RelativeLayout>
The line
tableLayout = (TableLayout) inflater.inflate(R.layout.api_table1, tableId);
correctly links the api_table1 resp. api_table2 layouts to the desired cells, but all the time this layout is added, it's not being replaced.
I unsuccessfully tried handing back the id of the just added view, to remove it later:
tableAPI.removeView(findViewById(tableCurrentRoadsideId));
How can this be done?
Finally solved it through a slightly different route.
I wrongly believed that when you attach a view to a cell again, it is a second view that is attached to it. Instead, my table was appending more rows to itself.
And also so much for no documentation on the actual workings of TableLayout.removeView()
The solution: at the start of setTable a line that removes all the views from that table. That clears up this table, after which I can re-attach my layout, and all is as it should be.
private void setTable(int minAPI, int maxAPI, TableLayout tableId) {
tableId.removeAllViews();
...
Related
I am new to Android. I tried to add TableRows to a TableLayout but it doesn't work. I have no idea why it doesn't show. I have tried to look from the internet, but seems like my problem isn't the same. I have no exceptions or erros in the logcat. I tried to debug and found out that the views are added but for some reason they don't show. Does anyone have an idea about this? Thanks in advance.
My code:
private void addInfosToTable(){ //this is called in onCreate() function
//these are just dummy infos i want to test the how the table works
Cell cell = new Cell("1234",5000,3000);
cell.setStatus(Cell.Status.COMPLETE);
Cell cell1 = new Cell("1234",5000,3000);
cell1.setStatus(Cell.Status.FAILED);
addCellRowToTable(cell);
addCellRowToTable(cell1);
}
private void addCellRowToTable(Cell cell){
//init cell info
String cellID = cell.getCellID();
double targetSpeed = cell.targetSpeed();
double targetPoint = cell.targetPoint();
Cell.Status status = cell.getStatus();
//create tableRow
TableRow tableRow = new TableRow(this);
tableRow.addView(getTextView(cellID,true));
tableRow.addView(getTextView("n.a"+"/ "+"n.a",false));
tableRow.addView(getTextView(targetSpeed+"/ "+targetPoint,false));
tableRow.addView(getStatusView(status));
tableRow.setPadding(0,R.dimen.table_marginVertical,R.dimen.table_marginVertical,0);
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT));
//add tableRow to tableLayout
this.cellTable.addView(tableRow);
}
private TextView getTextView(String text, boolean isFirst){
TextView textView = new TextView(this);
textView.setTextSize(R.dimen.table_textSize);
textView.setText(text);
textView.setTextColor(ContextCompat.getColor(this,R.color.black));
if (isFirst){
textView.setPadding(R.dimen.table_marginVertical,0,0,0);
}
return textView;
}
private ImageView getStatusView(Cell.Status status){
ImageView imageView = new ImageView(this);
switch (status){
case COMPLETE:
imageView.setImageResource(R.drawable.success);
return imageView;
default:
imageView.setImageResource(R.drawable.failed);
return imageView;
}
}
My table has 4 columns, and it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="superapp.networkapp.MainActivity"
android:focusableInTouchMode="true">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/warning_box"
android:padding="10dp"
android:layout_margin="3dp"
android:weightSum="1">
<TextView
android:layout_width="276dp"
android:layout_height="wrap_content"
android:text="#string/warning_text"
android:id="#+id/main_boxText"
android:layout_margin="3dp"
android:textColor="#color/black"
android:textStyle="bold"
android:layout_weight="0.49"
android:textSize="12sp"
android:textIsSelectable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/main_boxIcon"
android:src="#drawable/warning_icon"
android:layout_weight="0.47" />
</LinearLayout>
<TableLayout
android:stretchColumns = "*"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/cellListTable">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:paddingTop="#dimen/table_marginVertical"
android:paddingBottom="#dimen/table_marginVertical"
android:id="#+id/cellTableHeading">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/cells"
android:id="#+id/cell"
android:layout_column="0"
android:textColor="#color/white"
android:textColorHighlight="#color/blue"
android:textSize="#dimen/table_textSize"
android:textIsSelectable="true"
android:paddingLeft="#dimen/table_marginVertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/table_current_info"
android:id="#+id/currentInfo"
android:layout_column="1"
android:textColor="#color/white"
android:textColorHighlight="#color/blue"
android:textSize="#dimen/table_textSize"
android:textIsSelectable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/table_target_info"
android:id="#+id/targetInfo"
android:layout_column="2"
android:textColor="#color/white"
android:textColorHighlight="#color/blue"
android:textSize="#dimen/table_textSize"
android:textIsSelectable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/status"
android:id="#+id/status"
android:layout_column="3"
android:textColor="#color/white"
android:textColorHighlight="#color/blue"
android:textSize="#dimen/table_textSize"
android:textIsSelectable="true"
android:textAlignment="textEnd"
android:paddingRight="#dimen/table_marginVertical" />
</TableRow>
</TableLayout>
</LinearLayout>
I have no idea why it doesn't show. I have tried to look from the internet, but seems like my problem isn't the same. I have no exceptions or erros in the logcat. I tried to debug and found out that the views are added but for some reason they don't show. Does anyone have an idea about this? Thanks in advance.
UPDATE
I edited the layout height of the default view to all wrap_content. I also added this line of code: before adding the tableRow to the tableLayout. But it still does not work.
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT));
you have to define a layout for each table row and bind the textViews and ImageView to the corresponding elements in the layout.
New to android, I would like to have a view like below with actual values.
My code to dummy values is as below:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<TableRow
android:background="#607D8B"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Due On" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
</TableRow>
<TableRow
android:background="#ECEFF1"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Access to Knowledge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="12 Feb 2016" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Renew" />
</TableRow>
<TableRow
android:background="#ECEFF1"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Achivement" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="16 Feb 2016" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Renew" />
</TableRow>
<TableRow
android:background="#ECEFF1"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="God of small things" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="22 Feb 2016" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Renew" />
</TableRow>
<Button
android:text="Renew all" />
</TableLayout>
I am getting the values from XML from webservices and parsing it to show in my table view.
I am using DocumentBuilderFactory to parse my XML. Here my XML file is in String str.
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource src = new InputSource();
src.setCharacterStream(new StringReader(str));
Document doc = builder.parse(src);
String title = `doc.getElementsByTagName("title").item(0).getTextContent();`
Normally I use
TitleTxt.setText("Title: " + title);
PublisherTxt.setText("Publisher: " + publisher)
to show the results in my view.
I have multiple questions:
Q1 what modifications are needed in layout file to show - actual values and not the dummy values?
Q2 how to get the array values in XML parsing to show in my layout file?
Q3 how to show the XML parsed results in my table view - dynamically at runtime?
These are related queries, so had to put in the same question. Some of it may sound simple, but being new to Android, I am not able to figure out. Also tried to find a sample code to do it but could not find. Not sure if I am using the right keywords to search for what I am looking for.
Any links to sample code or which exact keywords to look for to search my issue solution or suggestions will also be appreciated.
Thanks in advance.
In order to add new Views to another at runtime, you can use the View.add method. So, in order to make the above table "dynamic", you can alter your layout file to just contain the TableLayout. And later, at runtime you programmatically add TableRows to the TableLayout.
Basically, there are two options how to add a TableRow then.
First, you can construct your TableRow, e.g.:
TextView tvTitle = new TextView(...);
TextView tvDueOn = new TextView(...);
Button btnRenew = new TextView(...);
TableRow tr = new TableRow(...);
tr.add(tvTitle);
tr.add(tvDueOn);
tr.add(btnRenew);
TableLayout lytTable = (TableLayout)findViewById(...);
lytTable.add(tr);
This is just a minimal version, but with adding the views weight, layout_width and layout_height, you can achieve this.
Second, you can design your TableRow in a layout XML file, which you can then load and add to the TableLayout at runtime using LayoutInflater.
lytTableRow.xml
<TableRow
android:id="+#id/lytTableRow"
android:background="#607D8B"
android:padding="5dp">
<TextView
android:id="+#id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="+#id/tvDueOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="+#id/btnRenew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
</TableRow>
code in Activity
TableLayout lytTable = (TableLayout)findViewById(R.id.lytTable);
View tableRow = getLayoutInflater().inflate(R.layout.lytTableRow, null);
TextView tvTitle = (TextView)tableRow.findViewById(R.id.tvTitle);
TextView tvDueOn = (TextView)tableRow.findViewById(R.id.tvDueOn);
Button btnRenew = (Button)tableRow.findViewById(R.id.btnRenew);
tvTitle.setText(...);
Both code-parts can be put into a loop for instance, in order to add your multiple rows - but pay attention to set tags/ids to your views using view.setTag in order to be able to distinguish them later in the Button click listener for instance.
Concerning the data, depending on what kind of XML parser you use, you can loop through every item in the data and add them to your TableLayout using one of the options shown above. According to your question, you use a DOM parser. You can loop through the items using a NodeList retrieved from doc.getElementsByTagName("parentnodename") where parentnodename is the element name which contains "title", "dueon" etc.
A page on my app has a table layout page, and one row of it seems to not be working. My goal is to evenly split the page between the left cell and right cell. The left cell contains a string, the right cell an image and string. I have tried to do so using weighSum. The left cell got weight of 4/8, and right cell was put into a linear layout, within which the image got weight of 1/8 and the string got weight 3/8. However, in the layout the left cell is taking up the great majority of the row, about 75% and I'm not sure why.
My rows below this one attempt almost the same thing, but don't have the same problem.
My xml(cut for relevancy):
<TableRow
android:layout_width="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Class"
android:id="#+id/classTextView"
android:layout_marginTop="14dp"
android:layout_weight="1"
android:gravity="center"/>
<LinearLayout>
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_height="match_parent"
android:background="#663300"/>
<ImageView
tools:ignore="ContentDescription"
android:id="#+id/classicon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/barbarianicon50"
android:layout_gravity="end"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Class Value"
android:id="#+id/classValueView"
android:layout_marginTop="14dp"
android:layout_weight="3"
android:gravity="left"/>
</LinearLayout>
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#663300"/>
<TableRow android:layout_width="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Strength"
android:id="#+id/strengthTextView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
<LinearLayout>
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_weight="0"
android:layout_height="match_parent"
android:background="#663300"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Strength Value"
android:id="#+id/strengthValView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Intelligence"
android:id="#+id/intelligenceTextView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
<LinearLayout>
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_weight="0"
android:layout_height="match_parent"
android:background="#663300"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Intelligence Value"
android:id="#+id/intelligenceValView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
</LinearLayout>
</TableRow>
The first table row is the problematic one - it should be evenly split between the left and right cells, but is mostly towards the right.
The second table row is an example of it working correctly - this row has 2 pairs of cells, but each of the cells, and the pairs of cells are correctly splitting the width between themselves evenly.
I can't figure out why the first doesn't work. I appreciate any help!
I have finally succeeded in fixing the layout!
The method used was to <include /> another layout as a row element inside this one. All these row elements had the same data, so I then programmatically filled the row elements with their necessary data whenever the view changed. You could also do this as a ListView, with each row in the ListView being another pair of elements, but I'm not sure how well that works for the 4 element wide rows.
The XML of each row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/statNameString"
android:id="#+id/statName"
android:layout_marginTop="30dp"
android:gravity="center" />
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_height="match_parent"
android:background="#663300"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/statValString"
android:id="#+id/statVal"
android:layout_marginTop="30dp"
android:gravity="center" />
</LinearLayout>
The top row(which represented the Class of the Character in my app) needed to include an ImageView, so it had a slightly changed layout. The ImageView was placed after the bar View, and before the last TextView.
The XML including each row into the final layout, for the Land layout:
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
layout="#layout/horizontal_class_row_layout"
android:id="#+id/strValues"
android:layout_column="0" />
<include
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
layout="#layout/horizontal_class_row_layout"
android:id="#+id/intValues"
android:layout_column="0" />
</TableRow>
If you only wanted 1 pair of elements in each row, you can simply remove one of the include tags. As mentioned before, you could also use the ListView to do these, and fill the data, and I will probably modify it to do that eventually.
Finally, you programmatically fill the elements with their data in the Java, so they don't just say "Class" and "Class Value" or whatever default data:
private void fillViewValues() {
//Gather views
LinearLayout llClass = (LinearLayout)findViewById(R.id.classValues);
LinearLayout llStr = (LinearLayout)findViewById(R.id.strValues);
LinearLayout llDex = (LinearLayout)findViewById(R.id.dexValues);
LinearLayout llCon = (LinearLayout)findViewById(R.id.conValues);
LinearLayout llInt = (LinearLayout)findViewById(R.id.intValues);
LinearLayout llWis = (LinearLayout)findViewById(R.id.wisValues);
LinearLayout llCha = (LinearLayout)findViewById(R.id.chaValues);
//Get the layout's locations
TextView classNameView = (TextView) llClass.findViewById(R.id.statName);
TextView classValView = (TextView) llClass.findViewById(R.id.statVal);
ImageView classImage = (ImageView) llClass.findViewById(R.id.classicon);
TextView strNameView = (TextView) llStr.findViewById(R.id.statName);
TextView strValView = (TextView) llStr.findViewById(R.id.statVal);
TextView dexNameView = (TextView) llDex.findViewById(R.id.statName);
TextView dexValView = (TextView) llDex.findViewById(R.id.statVal);
TextView conNameView = (TextView) llCon.findViewById(R.id.statName);
TextView conValView = (TextView) llCon.findViewById(R.id.statVal);
TextView intNameView = (TextView) llInt.findViewById(R.id.statName);
TextView intValView = (TextView) llInt.findViewById(R.id.statVal);
TextView wisNameView = (TextView) llWis.findViewById(R.id.statName);
TextView wisValView = (TextView) llWis.findViewById(R.id.statVal);
TextView chaNameView = (TextView) llCha.findViewById(R.id.statName);
TextView chaValView = (TextView) llCha.findViewById(R.id.statVal);
//Fill those values
//Names of sections
classNameView.setText(getResources().getString(R.string.classString));
strNameView.setText(getResources().getString(R.string.strString));
dexNameView.setText(getResources().getString(R.string.dexString));
conNameView.setText(getResources().getString(R.string.conString));
intNameView.setText(getResources().getString(R.string.intString));
wisNameView.setText(getResources().getString(R.string.wisString));
chaNameView.setText(getResources().getString(R.string.chaString));
//Values
classValView.setText(classString);
setImage(classImage, classString);
strValView.setText(String.format("%d", finalStats[0]));
dexValView.setText(String.format("%d", finalStats[1]));
conValView.setText(String.format("%d", finalStats[2]));
intValView.setText(String.format("%d", finalStats[3]));
wisValView.setText(String.format("%d", finalStats[4]));
chaValView.setText(String.format("%d", finalStats[5]));
}
This follows the advice at at this StackOverflow post.
One thing I would like to note, is that when you change the layout, you need to reset the content view to your current layout. In my case, the function looked like:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.activity_see_character);
fillViewValues();
}else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.activity_see_character);
fillViewValues();
}
}
where fillViewValues() is the previous function. I believe that this sets the layout to either the portrait or landscape version of the layout, which can then have its elements accessed. If you don't do this when you try to access the elements it can't find them and gives a NullPointerException.
I really need help
I have a ResultSet as a result of select query from MySQL DB. how can I display it in table layout?? I have setup a layout for tableview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="#+id/myFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/some_hint" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="10dp"
android:text="#string/some_text" android:textSize="20sp" />
<TableLayout
android:id="#+id/producttablelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerPadding="#dimen/activity_horizontal_margin"
android:scrollbars="horizontal|vertical"
android:showDividers="none|beginning|middle|end" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|bottom|left|right"
android:paddingTop="#dimen/activity_horizontal_margin" >
<TextView
android:id="#+id/ttt1"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ttt2"
android:text="Column 12"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
</LinearLayout>
I want to be able to add ResultSet data into it. Something like:
super.onCreate(savedInstanceState);
String s = null;
setContentView(R.layout.activity_list_all_products);
// Show the Up button in the action bar.
setupActionBar();
context333=this;
TableLayout myTable = (TableLayout)findViewById(R.id.producttablelayout);
try
{
connect2 = DriverManager.getConnection(LogonActivity.url, LogonActivity.user, LogonActivity.password);
statement2 = connect2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
preparedStatement2 = connect2.prepareStatement("select article_code,article_desc from products limit 4");
resultSet2=preparedStatement2.executeQuery();
resultSet2.beforeFirst();
while (resultSet2.next()) {
how to add next record here to the table layout??
}
Please help
You can create a new Views (it seems you are populating your table rows with TextViews) and populate them with the results from your query as you wish. Then you can create a new TableRow with a constructor (see the documentation).
Add the views to to table row instnace using:
tableRowInstance.addView(viewInstance);
and then add the tableRow to the tableLayout the same way:
myTable.addView(tableRowInstance)
TableLayouts and TableRows are both subclasses of View. You should do some research on ViewGroup, View, and how they are structured. This link provides nice visualizations.
I want to create a multirow Button list. Something like this :
but I want to do it dynamically(in code). Is there a way to tell layout to do this automatically? Or i have to do this myself usingRelativeLayout.LayoutParams. I can do this by code but I should control so many things and I was wondering if there is another easier way to do this. For example tell layout to add elements in the next row when the current one is full!
You could also do this with LinearLayout and make all the buttons the same size using weight.
As for your question:
i can do this by code but i should control so many things and i was
wondering if there is another easier way to do this. for example tell
layout to add elements in the next row when the current one is full!
This is potentially possible if you measure the screen width and height and use the Functions in in the View class to figure out the specifics of that particular view and its children.
Alternative
But as mentioned in the comments, there are other views that you can use to solve your problem like GridView.
You can also use a table layout ,
Create first row of tabllayout in xml like this
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/goBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginTop="12dp"
/>
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80"
android:background="#f0ffff" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/data_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#006400"
android:orientation="vertical" >
<TableRow
android:id="#+id/second"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:text="Number1"
android:textColor="#006400" >
</TextView>
<Button
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:text="Number2"
android:textColor="#006400" />
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:text="Distance"
android:textColor="#006400" >
</TextView>
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="5dp"
android:paddingTop="8dp"
android:text="F/G/H/S"
android:textColor="#006400" >
</TextView>
</TableRow>
</TableLayout>
</ScrollView>
</FrameLayout>
</HorizontalScrollView>
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/savescore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I created this xml for four three texts and on button in a single row
refer to the table in onCreate
TableLayout extendeedTable = (TableLayout)findViewById(R.id.data_table);
add rows like
while (extendeedTable.getChildCount() > 1)
{
// while there are at least two rows in the table widget, delete
// the second row.
extendeedTable.removeViewAt(1);
}
// collect the current row information from the database and
// store it in a two dimensional ArrayList
// iterate the ArrayList, create new rows each time and add them
// to the table widget.
// Here value is the number of rows you want in table
for (int position=0; position < value ; position++)
{
TableRow tableRow= new TableRow(this);
tableRow.setBackgroundDrawable(null);
// ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(Integer.toString(position + 1));
idText.setGravity(Gravity.CENTER);
idText.setTextColor(Color.BLACK);
idText.setWidth(10);
idText.setHeight(45);
idText.setBackgroundResource(R.drawable.text2);
tableRow.addView(idText);
textOne = new Button(this);
textOne.setText("CLUB");
textOne.setBackgroundResource(R.drawable.text2);
textOne.setGravity(Gravity.CENTER);
textOne.setTextColor(Color.BLACK);//left top right bottom
textOne.setWidth(10);
textOne.setHeight(45);
textOne.setId(1+position);
tableRow.addView(textOne);
allbtns.add(textOne);
// textOne.setOnClickListener(this);
textOne.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// do something when the button is clicked
final Button button = (Button) arg0;
System.out.println("value of button is "+
button.getId());
dialog1.setTitle(" SELECT CLUB ");
textTwo = new EditText(this);
textTwo.setText("");
textTwo.setBackgroundResource(R.drawable.text2);
textTwo.setGravity(Gravity.CENTER);
textTwo.setTextColor(Color.BLACK);
textTwo.setWidth(10);
textTwo.setHeight(45);
textTwo.setInputType(InputType.TYPE_CLASS_NUMBER);
tableRow.addView(textTwo);
allEds1.add(textTwo);
textTwo.setId(position +1);
textThree = new EditText(this);
textThree.setText("");
textThree.setWidth(10);
textThree.setHeight(45);
textThree.setTextColor(Color.BLACK);
textThree.setBackgroundResource(R.drawable.text2);
textThree.setGravity(Gravity.CENTER);
textThree.setInputType(InputType.TYPE_CLASS_TEXT);
tableRow.addView(textThree);
allEds2.add(textThree);
textThree.setId(position +1);
extendeedTable.addView(tableRow);
}
for this i took help from here
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/7/
and
its xml
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
after some more google search i finally found the best way to do this. it's so clean and simple, using Adapters and grids.
thanks for all the answers
here is a Tutorial: Creating a Custom Adapter for Gridview(ButtonAdapter)