I need to create a dinamic table which will be fit with rows of data. The rows I want will have an specific form, like this:
So, to get it, I used relativeLayouts. The problem is that I first tried it with an .xml to see what parameters will have the differents views that it forms, and I get it, but now I cant replicate it in the code.
Here is my code:
TableLayout linkRoutesTable = (TableLayout) findViewById(R.id.layoutLinkRoutes);
//ROW:
TableRow linkRouteRow = new TableRow(this);
linkRouteRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linkRouteRow.setId(i)
//RELATIVELAYOUT:
float px0 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics());
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(0, (int)px0)));
//CONTENT:
// BUTTON: ...
// NUMBER:...
// ADDRESS:...
// NAME: ...
relativeLayout.addView(number);
relativeLayout.addView(name);
relativeLayout.addView(address);
relativeLayout.addView(button);
linkRouteRow.addView(relativeLayout);
linkRouteTable.addView(linkRouteRow);
linkRoutesTable.refreshDrawableState();
It doesn't show anything...
The .xml show ok, here it is:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" android:orientation="vertical">
<TableRow>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_weight="1"
android:id="#+id/layoutLinkRoutes" >
<Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/button1" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></Button>
<TextView android:textStyle="bold" android:textSize="45dp" android:text="1" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_marginLeft="22dp"></TextView>
<TextView android:text="TextView" android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="#+id/button1" android:layout_toRightOf="#+id/textView1" android:layout_marginLeft="16dp"></TextView>
<TextView android:text="TextView" android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_alignLeft="#+id/textView3"></TextView>
</RelativeLayout>
</TableRow>
What Am I doing wrong in the code? I need some help, I tried everything.
If I dont use the relativeLayouts and the rows, it shows something but bad... so the problem is the rows and the relativelayout
Really thanks...
You don't have to define the layout in code to fill a table dynamically. You can infalte a layout which you define in xml like a normal layout.
So you define your table row layout one time in a xml file and infalte it multiple times into your table layout.
Check this answer.
Related
I am writing an Android app that requires me to programmatically add a TextView to a TableRow which is, in turn, added to a TableLayout. For the design to look proper, the TextView width must be the same as the TableRow width. The problem I am having is that even though I have attempted to set the width of the TextView to match using MATCH_PARENT, it is not taking effect.
Here is a sample of the Java that I am trying to use to do this:
final TableRow myTableRow = new TableRow(getApplicationContext());
final TextView myTextView = new TextView(getApplicationContext());
myTextView.setWidth(TableRow.LayoutParams.MATCH_PARENT);
myTextView.setPadding(5, 0, 5, 0);
myTextView.setTextColor(Color.BLACK);
myTextView.setTextSize(30);
myTextView.setBackgroundColor(Color.RED);
myTextView.setText("1");
myTableRow.addView(myTextView);
myTableLayout.addView(myTableRow); // Assume 'myTableLayout' has already been declared.
Here is the XML for myTableLayout:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myTableLayout">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowOne">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textViewOne"
android:layout_weight="1"
android:textSize="40sp"
android:paddingStart="5dp"
android:paddingEnd="5dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowTwo">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewTwo"
android:textColor="#000000"
android:textSize="30sp"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowThree">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewThree"
android:textColor="#000000"
android:textSize="20sp"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:layout_weight="1" />
</TableRow>
</TableLayout>
Does anyone have any idea what I could be doing wrong?
Here is a picture of the way the layout appears. The code posted here is not identical to the actual app code, but it is enough to show the problem. Specifically, the code that was removed concerns the replies. In the example code posted as part of the question, only one TextView is shown in the Java code, but in the actually project, there are two.
Layout Example Image
There are several ways you can handle this.
TableLayout and TableRow both extends linearlayout, so maybe you need to set Orientation.
you could set the layout params(margin params) like this
layout_width = 0dp ;
layout_weight = 1 ;
The best way to give height and width property to a view is to use Layout Parameters. most of times (almost all the times for me) setting width and height directly wont work.
TableRow.LayoutParams mParam=new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// set layout parameter to view
textView.setLayoutParams(mParam);
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.
I am attempting to display data from the on-board SQLite database within a table in the Android UI, but it isn't working as it should. Here is what I have:
activity_main.xml
<TableLayout
android:id="#+id/data_table"
android:stretchColumns="3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView android:layout_column="1" android:text="id" android:layout_width="0dp" android:layout_weight="1" />
<TextView android:layout_column="2" android:text="firstname" android:layout_width="0dp" android:layout_weight="1" />
<TextView android:layout_column="3" android:text="lastname" android:layout_width="0dp" android:layout_weight="1" />
</TableRow>
</TableLayout>
MainActivity.java
List<People> listOfPeople = databaseHelper.getAllPeople();
TableLayout myTableLayout = (TableLayout) findViewById(R.id.data_table);
for(People person : listOfPeople){
TableRow newTableRow = new TableRow(this);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams();
newTableRow.setLayoutParams(rowParams);
TextView idTV = new TextView(this);
idTV.setLayoutParams(new TableRow.LayoutParams(1));
idTV.setText(person.getId());
TextView firstnameTV = new TextView(this);
firstnameTV.setLayoutParams(new TableRow.LayoutParams(2));
firstnameTV.setText(person.getFirstname());
TextView lastnameTV = new TextView(this);
lastnameTV.setLayoutParams(new TableRow.LayoutParams(3));
lastnameTV.setText(person.getLastname());
newTableRow.addView(idTV);
newTableRow.addView(firstnameTV);
newTableRow.addView(lastnameTV);
myTableLayout.addView(newTableRow);
}
The table and data is displayed, but the data within the rows I built from the database are all smooshed together and unreadable. If I were to compare this to web development, it looks like all the data are in one cell of the table row.
I am very new at Android development (former JSP developer), and what I have here, I have tried to piece together from Google searches. I need to use a TableLayout so a grid layout (or any other layout) will not do.
EDIT:
I added some background color to my "cells" and discovered they are in different cells. The issue, apparently, is that they are not distributing themselves correctly as I am not sure how to add "layout_width" and "layout_weight" to the cells within Java. At least, I guess that is what the issue is.
Just change your xml to this:
<TableLayout
android:id="#+id/data_table" android:stretchColumns="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView android:layout_column="1" android:layout_weight="1" android:text="id" />
<TextView android:layout_column="2" android:layout_weight="1" android:text="firstname" />
<TextView android:layout_column="3" android:layout_weight="1" android:text="lastname" />
</TableRow>
Make sure to also set the layout_weight to 1, for all the TextViews added by java code.
In my Android application there is a screen with some data to be displayed in table format. This table will have around 5 columns and at least 50 rows.
How do I do I create a table in android?
I searched through and everybody seems to recommend to use TableLayout and textView for each cell. Using this technique seems to be a bit difficult as there are lots of textView Components.
Is there any other solution to this?
TableLayout lTableLayout;
lTableLayout = (TableLayout)findViewById(R.id.tblayout);
for(int i=0;i<10;i++){
TableRow tr1 = new TableRow(this);
tr1.setPadding(0, 5, 0, 0);
lTableLayout.addView(tr1, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView tv11 = new TextView(this);
tv11.setPadding(2, 0, 0, 0);
tv11.setTextSize(12);
tr1.addView(tv11,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); // Can give width and height in numbers also.
}
These will create 10 rows and 1 textview in each row in taken table layout.
take one table layout in your xml file like below :
<TableLayout
android:id="#+id/tblayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
Use ListView with Custom adapter , create five textview's in listview item as columns..
create listitem like this
row.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">
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
Its not necessary to user TableLayout.
you can user Custom ListView And set header of ListView as your Column name
To create custom listview see this example
I am trying to build a UI, where in I have a linear layout which has been defined in the XML.
As per the user's input, I need to add some TextViews to this linear layout. I am able to do this.
My actual problem is, when I have more text views, they are stacked next to each other and some of text views text are hidden or stretched vertically as shown in the image below.
I would like to use the whole width of the linear layout and if the text view can not fit in this row, it should be put in a new row or below the first text view.. I would like the display to be as below.
Following is my Linear layout configuration in XML:
<RelativeLayout
android:id="#+id/RL1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingTop="5dip" >
<TextView
android:id="#+id/text1"
android:layout_width="85dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="left"
android:text="Lable 1"
android:textColor="#999999" />
<LinearLayout
android:id="#+id/LL1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/button1"
android:layout_toRightOf="#+id/text1"
android:gravity="left|center_vertical"
android:orientation="horizontal"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="3dip" >
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginTop="2dip"
android:background="#drawable/plus"
android:clickable="true"
android:focusable="true"
android:paddingTop="5dip" />
</RelativeLayout>
Can any one please help me in how to realign the same in java code.
I would Like to suggest you about how you can provide equal sizes to all your views in Linear Layout,you can do that by using weight property in the XML file i.e., android:weight for that particular View and when you use this property you should give width=0dp or 0dip.I think this will solve your problem easily.
Please I suggest you first you take full Knowledge of how to use this property in the following Link:-Weight property with an example
Please see:
How to wrap Image buttons in a horizontal linear layout?
How can I do something like a FlowLayout in Android?
You also might search github for FlowLayout.java.
An alternative approach is given in:
Android - multi-line linear layout
In addition, there's a class that adds images into a TextView:
https://stackoverflow.com/a/21250752/755804
which is not the same as wrapping views in the general case, but sometimes may do the job.
i'm korean.
so i don't speak English well, but i'll help you.
first, Create 'item.xml' with four text boxes.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/set_checkbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text2"
android:text="0"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text3"
android:text="0"
android:layout_weight="4"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text4"
android:text="0"
android:layout_weight="4"
android:gravity="center"/>
</LinearLayout>
second, Create 'main.xml' where 'item.xml' will be dynamically generated.
'orientation' helps to create one line at a time.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Finally, You can create four TextView per line using the code below.
LinearLayout layout = (LinearLayout)findViewById(R.id.mainxml);
LinearLayout item = (LinearLayout)layout.inflate(this.getContext(), R.layout.itemxml, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
item.setLayoutParams(params);
CheckBox cb = item.findViewById(R.id.set_checkbox);
TextView text2 = item.findViewById(R.id.text2);
TextView text3 = item.findViewById(R.id.text3);
TextView text4 = item.findViewById(R.id.text4);
cb.setChecked(true);
text2.setText("text2");
text3.setText("text3");
text4.setText("text3");
layout.addView(item);
The 10th loop is shown in the following picture.
enter image description here