If I remove the line indicated, the "elements" are displayed, although they are to big and go partly out of the screen,
if I try to set the width and height (LayoutParams) of an element inside the tablerow, the tablelayout is displayed empty/blank.
What am I doing wrong?
Thank you.
Activity onCreate:
TableLayout tLay = (TableLayout) findViewById(R.id.tabLay);
LayoutInflater gonf = getLayoutInflater();
for(int j= 0; j<rows;j++)
{
TableRow tRow = new TableRow(this);
tRow.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
for (int i = 0; i<columns;i++)
{
FrameLayout elVw = (FrameLayout) gonf.inflate(R.layout.elemento, null);
//elVw.setLayoutParams(new FrameLayout.LayoutParams(width/columns,height/rows));
elVw.setLayoutParams(new FrameLayout.LayoutParams(100,100)); //<------------------This Line---------
TextView tVw = (TextView) elVw.findViewById(R.id.txtVwDescr);
Log.v("par",elVw.getLayoutParams().height+"");
elVw.setTag(i+","+j);
tVw.setText((String)elVw.getTag());
tRow.addView(elVw);
}
tLay.addView(tRow);
}
elemento.xml:
<FrameLayout
android:id="#+id/SingolFrame"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtVwDescr"
android:padding="10dp"
android:maxLength="30"
android:maxLines="1"
android:text="fn_High"
android:background="#b7ffffff"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_margin="0dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:id="#+id/imgGridIcon"
android:focusable="false"
android:contentDescription="Element"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:id="#+id/imgGridElementBorder"
android:focusable="false"
android:contentDescription="Element"
android:background="#drawable/bordo_icona"/>
I think you should use the parent viewGruop's layoutparams,maybe you can try use the TableLayout.LayoutParams(100,100) instead.
Related
I just started working on android a day ago and I'm working on scrolls. I have already made one but I would now like to do the same thing dynamically.
This is the code for my activity_main.xml
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal">
<LinearLayout
android:id= "#+id/linearlayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onTouch">
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="#+id/bottomlinear"
android:layout_width="match_parent"
android:layout_height="400px"
android:gravity="center"
android:background="#00ffff"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="46dp">>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/drop"
android:textSize="30sp"
android:text="Drop Zone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Total"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Success"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Fail"
android:textSize="20sp" />
</LinearLayout>
Basically I would like to add 10 images from the drawable into the horizontal scroll as image views dynamically. Any help or ideas are much appreciated.
You can just do like this.
Create an xml layout which holds the imageView.
image_item.xml
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="200dp"
android:layout_height="200dp"
android:visibility="visible"
android:adjustViewBounds="true"
/>
Now find the container where want into java file, like this.
LinearLayout containerLayout = (LinearLayout)findViewById(R.id.linearlayout1);
Now just run a for loop till 10 and add the views at runtime.
for(int a = 0 ; a < 10 ; a++)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = inflater.inflate(R.layout.image_item, null);
containerLayout.addView(inflatedView);
}
Hope this helps, Feel free to discuss if found problem.
Happy Coding :-)
I solved the problem. I'm gonna post the solution hoping that it helps someone else having a similar issue. I originally made 3 horizontal scroll views and the xml for one of them is like this
<HorizontalScrollView
android:id="#+id/HorizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="1dp"
android:background="#FFF"
android:scrollbars="none">
<LinearLayout
android:id="#+id/imgLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
The code I wrote to create Image Views inside Linear Layout of horizontal scroll view:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int j=1; j<=10; j++)
{
b1=j;
create_img1("drawable/a"+j, b1);
}
}
void create_img1(String ss, int ID)
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout1);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(200, 200);
parms.gravity = Gravity.CENTER;
parms.setMargins(20, 20, 20, 20);
final ImageView imageView = new ImageView(this);
imageView.setLayoutParams(parms);
int id = getResources().getIdentifier(ss, "id", getPackageName());
imageView.setImageResource(id);
linearLayout.addView(imageView);
imageView.setId(ID);
}
I made one for multiple scroll views with drag and drop functionality but I filtered it out and if you want to create image views inside scroll views dynamically, this is what you are looking for. Hope this helps someone else with a similar issue.
I think you are trying to do something like this:
mHScrollContentView = (ViewGroup) findViewbyId(R.id.linearlayout1);
ImageView iv1 = new ImageView(this);
iv.setImageResource(R.drawable.image_1);
ImageView iv2 = new ImageView(this);
iv.setImageResource(R.drawable.image_2);
ImageView iv3 = new ImageView(this);
iv.setImageResource(R.drawable.image_3);
ImageView iv4 = new ImageView(this);
iv.setImageResource(R.drawable.image_4);
mHScrollContentView.addView(iv1);
mHScrollContentView.addView(iv2);
mHScrollContentView.addView(iv3);
mHScrollContentView.addView(iv4);
http://sandyandroidtutorials.blogspot.in/2013/06/horizontal-listview-tutorial.html
You might get an answer here!
I am currently working at developing an app that requires a dynamically generated GridView. In the layout added to the GridView through an adapter I have a LinearLayout that takes information from an ArrayList and then it should display an ImageView followed by a TextView. I have done that programmatically, but some random space appears between the two of them.
I have tried removing the padding, removing the margin, setting the adjustViewBounds of the image to true and also removing the inner padding of the TextView and none of these things work.
Here is the .xml file containing the LinearLayout to be populated:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rounded_text_black">
<ImageView
android:id="#+id/separator"
android:layout_width="match_parent"
android:layout_height="2dp"
android:scaleType="fitXY"
android:src="#drawable/horizontal_white_line"
android:layout_centerVertical="true"/>
<LinearLayout
**android:id="#+id/orderLayout"**
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#id/separator"
android:orientation="vertical"
android:paddingTop="40dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#id/separator"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:scaleType="fitXY"
android:src="#mipmap/effects"
android:layout_gravity="center"/>
<TextView
android:id="#+id/moodText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="#style/smallText"
android:padding="0dp"/>
<TextView
android:id="#+id/tastesText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="#style/smallText"
android:textAllCaps="false"
android:padding="0dp"/>
<TextView
android:id="#+id/tastesText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="#style/smallText"
android:textAllCaps="false"
android:padding="0dp"/>
</LinearLayout>
</RelativeLayout>
Here is the code that generates the image-text pairs:
for(int i = 0; i < 3; i++){
int currentPos = orderSize - i;
if(currentPos >= 0){
LinearLayout container = new LinearLayout(v.getContext());
LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
container.setOrientation(LinearLayout.HORIZONTAL);
container.setGravity(Gravity.RIGHT);
container.setLayoutParams(containerParams);
ImageView img = new ImageView(v.getContext());
LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
imgParams.gravity=Gravity.RIGHT;
img.setLayoutParams(imgParams);
img.setPadding(0,0,0,0);
if(order.get(currentPos).delivered){
img.setImageResource(R.drawable.green_circle);
}
else{
img.setImageResource(R.drawable.yellow_circle);
}
TextView txt = new TextView(v.getContext());
txt.setTextColor(Color.WHITE);
txt.setText(order.get(currentPos).orderItem);
txt.setPadding(0,0,0,0);
container.addView(img);
container.addView(txt);
ll.addView(container);
}
And here is the result:
The red rectangle indicates the extra space.
Thank you very much.
I suggest you to create your line item view (green ball and drink name) in an xml layout, checking that all is working fine on a single line.
Then you can change your for cycle as follows:
for(int i = 0; i < 3; i++){
int currentPos = orderSize - i;
if(currentPos >= 0){
// inflate a new instance of view starting from an xml file
View tableLineView = getLayoutInflater().inflate(R.layout.table_line, null);
// Image view
ImageView iv = (ImageView)tableLineView.findViewById(R.id.ball);
if(order.get(currentPos).delivered){
iv.setImageResource(R.drawable.green_circle);
} else{
iv.setImageResource(R.drawable.yellow_circle);
}
// TextView
TextView tv = (TextView)tableLineView.findViewById(R.id.textview);
tv.setText(order.get(currentPos).orderItem);
// add to LinearLayout
ll.addView(tableLineView);
}
}
I can't test it now, if you find problems ask me.
Here you can read a discussion about view inflating
TextView txt = new TextView(this);
LinearLayout.LayoutParams txtParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
txt.setLayoutParams(txtParams);
txt.setTextColor(Color.BLACK);
txt.setText("Hello World");
txt.setPadding(0,0,0,0);
Use this and you wont see padding anymore!!
I want to show a screen with text area that is vertically scrolling and image area that is horizontally scrolling.
I have created two layouts:
activity_display.xml
<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"
tools:context=".MainActivity" >
<ScrollView
android:id="#+id/displaySV1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginBottom="20dp">
<LinearLayout
android:id="#+id/displayLL1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<HorizontalScrollView
android:id="#+id/displaySV2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginBottom="20dp">
<LinearLayout
android:id="#+id/displayLL2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
and custom_layout.xml : I want to add the custom layout in the linear layout displayLL1.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customFrame">
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/customTV1"
android:layout_gravity="start|top"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:text="Test text"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/customTV2"
android:layout_gravity="top|center_horizontal"
android:text="Test Value"
android:layout_marginTop="5dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EDIT"
android:id="#+id/editButton"
android:layout_gravity="right|top" />
</FrameLayout>
Display activity calls:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.displayLL2);
for(int i = 0; i < count1; i++)
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.displayLL1);
FrameLayout custLayout = (FrameLayout) findViewById(R.id.customFrame);
TextView label = (TextView) findViewById(R.id.customTV1);
TextView value = (TextView) findViewById(R.id.customTV2);
label.setText("Test");
value.setText("Test");
linearLayout.addView(custLayout);
}
for (int x = 0; x < count2; x++)
{
final ImageView image = new ImageView(this);
image.setBackgroundResource(R.drawable.ic_launcher);
linearLayout2.addView(image);
}
I am able to populate the image part if text part is commented out. But with the above code nothing is displayed on the screen.
Any idea where I might be going wrong?
Thank you!
Probably you have a NullPointerException.
Because there is no customTV1 and customTV2 ids in your activity xml, so calling findViewById(R.id.customTV1); will return a null value, and label.setText("Test"); will throw a NullPointerException.
Actually calling FrameLayout custLayout = (FrameLayout) findViewById(R.id.customFrame); will not add your custom xml to the activity one, you need to inflate it.
Try this code:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.displayLL1);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.displayLL2);
LayoutInflater inflater = LayoutInflater.from(yourActivity.this);
for(int i = 0; i < count1; i++){
View custLayout= inflater.inflate(R.layout.custom_layout, null, false);
TextView label = (TextView) custLayout.findViewById(R.id.customTV1);
TextView value = (TextView) custLayout.findViewById(R.id.customTV2);
label.setText("Test");
value.setText("Test");
linearLayout.addView(custLayout);
}
for (int x = 0; x < count2; x++){
ImageView image = new ImageView(this);
image.setBackgroundResource(R.drawable.ic_launcher);
linearLayout2.addView(image);
}
I am trying this for 2 days,I am trying to make UIs for the view having two columns in first row,then in second row having three columns,and in third again two columns and so on..And also I have to implement dynamic data(means num of rows/columns are dynamic).
I don't have any idea to implement this type of view.I have also try stragged grid view but this is for dynamic views..but in this I have static 2,3,2,3... columns for 1,2,3,4..rows.Please help me.Any tutorial will be most helpful.
Finally I have solved this problem.I have tried with Raghunandan and user3278416's suggestion.And did all the stuff in runtime except Table layout.Here is the xml file:
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/table_layout"
>
And the the two layouts one is for small layout(the second row) and one is for large one(first row):
<?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" >
<RelativeLayout
android:layout_width="130dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="#drawable/big_product_bg" >
<ImageView
android:id="#+id/img_offersrowlayout"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="#drawable/img_bydefault" />
<TextView
android:id="#+id/txt_title_offerrowlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/img_offersrowlayout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="BenQ Digital camera"
android:textColor="#color/green_app"
android:textSize="15dp"
android:textStyle="bold" />
The small one:
<RelativeLayout
android:layout_width="90dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="#drawable/big_product_bg" >
<ImageView
android:id="#+id/img_offersrowlayout"
android:layout_width="90dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="#drawable/img_bydefault" />
<TextView
android:id="#+id/txt_title_homesmallrowlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/img_offersrowlayout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="BenQ Digital camera"
android:textColor="#color/green_app"
android:textSize="15dp"
android:textStyle="bold" />
Now the Java Code:
int leftMargin_small=2;
int topMargin_small=5;
int rightMargin_small=2;
int bottomMargin_small=5;
int j = 0;
while (Show.size()>j) {
try {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableRow.LayoutParams param = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
param.setMargins(leftMargin_small, topMargin_small, rightMargin_small, bottomMargin_small);
LinearLayout layout1 = new LinearLayout(getApplicationContext());
TableRow.LayoutParams param_layout = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
layout1.setLayoutParams(param_layout);
View question = inflater.inflate(R.layout.offers_rowlayout, null);
question.setLayoutParams(param);
//question.setId(pos);
TextView title = (TextView) question.findViewById(R.id.txt_title_offerrowlayout);
title.setText(""+">>"+Show.get(j).get("name"));
View question1 = inflater.inflate(R.layout.offers_rowlayout, null);
//question.setId(pos);
question1.setLayoutParams(param);
TextView title1 = (TextView) question1.findViewById(R.id.txt_title_offerrowlayout);
title1.setText(""+">>"+Show.get(j+1).get("name"));
View question_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question_small.setLayoutParams(param);
TextView title_small = (TextView) question_small.findViewById(R.id.txt_title_homesmallrowlayout);
title_small.setText(""+">>"+Show.get(j).get("name"));
View question1_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question1_small.setLayoutParams(param);
TextView title1_small = (TextView) question1_small.findViewById(R.id.txt_title_homesmallrowlayout);
title1_small.setText(""+">>"+Show.get(j+1).get("name"));
View question2_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question2_small.setLayoutParams(param);
TextView title2_small = (TextView) question2_small.findViewById(R.id.txt_title_homesmallrowlayout);
title2_small.setText(""+">>"+Show.get(j+2).get("name"));
if (gett) {
TableRow tr = new TableRow(getApplicationContext());
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,1);
// trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(trParams);
layout1.addView(question);
layout1.addView(question1);
tr.addView(layout1);
tablee.addView(tr);
gett = false;
j = j+2;
}
else
{
TableRow tr = new TableRow(getApplicationContext());
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,1);
layout1.addView(question_small);
layout1.addView(question1_small);
layout1.addView(question2_small);
tr.addView(layout1);
tablee.addView(tr);
gett = true;
j = j+3;
}
} catch (Exception e) {
e.printStackTrace();
}
}
Here Show is the arraylist of hashmap type and tablee is the tablelayout object which I have made in Xml file.
I have a little bit problem that displaying list data to TextView in Android. My scenario is I have a TableLayout with Default One TableRow. Inside table row, I has been created new LinearLayout. Then, Four TextView created inside LinearLayout. I adding some default value to this textview. My default value is
1, 2014-02-05, S02, 20
Here my code snippet for above scenari0.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/sync_history_table_color"
android:id="#+id/history_table"
android:orientation="vertical"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="1dp"
android:id="#+id/row_start">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="30dp" android:id="#+id/row_linear" android:layout_weight="1"
android:background="#color/isp_home_color">
<TextView android:layout_width="0dp" android:layout_height="match_parent" android:text="#string/sync_default_no"
android:id="#+id/history_display_no" android:layout_weight="0.165"
android:gravity="center_vertical|left" android:paddingLeft="10dp"
android:textColor="#color/sync_history_text_color"/>
<TextView android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_date"
android:id="#+id/history_display_date"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_order"
android:id="#+id/history_display_orderid"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_qty"
android:id="#+id/history_display_quantity" android:layout_weight="0.5" android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
</LinearLayout>
</TableRow>
</TableLayout>
Actually, I want to create dynamically TableRow, LinearLayout and TextView. Then, I put the list result to add these. The list result come from sqlite db. But, I can't get what I want. Here my code for looping the list and display the result. But, I got my some default value. Please pointing to me how can I get this. Thanks.
Here snippet for display list.
public void displayHistory(){
List<IspHistoryListObject> historyList = sqlaccess.getAllItems();
TableLayout showRow = (TableLayout) findViewById(R.id.history_table);
int count = 0;
for(IspHistoryListObject hl : historyList) {
count++;
TableRow tr = new TableRow(this);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LinearLayout.LayoutParams(50,30));
TextView tv_sync_no = new TextView(this);
TextView tv_sync_date = new TextView(this);
TextView tv_sync_orderid = new TextView(this);
TextView tv_sync_qty = new TextView(this);
tv_sync_no.setText(String.valueOf(count));
tv_sync_date.setText(hl.getSyncDate().substring(0,10));
tv_sync_orderid.setText(hl.getSyncOrderIdRef());
tv_sync_qty.setText(String.valueOf(hl.getQty()));
ll.addView(tv_sync_no);
ll.addView(tv_sync_date);
ll.addView(tv_sync_orderid);
ll.addView(tv_sync_qty);
tr.addView(ll);
showRow.addView(tr);
}
}
try this way here I gave sample or demo code you have to modify as per your requirement and still have problem let me know
main layout
1. table.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
</TableLayout>
table row layout
2. table_item.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="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/history_display_no"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_date"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_orderid"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_quantity"
android:layout_weight="0.25"
android:gravity="center"/>
</TableRow>
activity class
3. Activity
public class MyActivity extends Activity {
private TableLayout tableLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
tableLayout=(TableLayout)findViewById(R.id.tableLayout);
for (int i=0;i<5;i++){
View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item,null,false);
TextView history_display_no = (TextView) tableRow.findViewById(R.id.history_display_no);
TextView history_display_date = (TextView) tableRow.findViewById(R.id.history_display_date);
TextView history_display_orderid = (TextView) tableRow.findViewById(R.id.history_display_orderid);
TextView history_display_quantity = (TextView) tableRow.findViewById(R.id.history_display_quantity);
history_display_no.setText(""+(i+1));
history_display_date.setText("2014-02-05");
history_display_orderid.setText("S0"+(i+1));
history_display_quantity.setText(""+(20+(i+1)));
tableLayout.addView(tableRow);
}
}
}
check out context hierachy
for(IspHistoryListObject hl : historyList) {
count++;
TableRow tr = new TableRow(showRow.getContext());
LinearLayout ll = new LinearLayout(tr.getContext());
ll.setLayoutParams(new LinearLayout.LayoutParams(50,30));
TextView tv_sync_no = new TextView(ll.getContext());
TextView tv_sync_date = new TextView(ll.getContext());
TextView tv_sync_orderid = new TextView(ll.getContext());
TextView tv_sync_qty = new TextView(ll.getContext());
tv_sync_no.setText(String.valueOf(count));
tv_sync_date.setText(hl.getSyncDate().substring(0,10));
tv_sync_orderid.setText(hl.getSyncOrderIdRef());
tv_sync_qty.setText(String.valueOf(hl.getQty()));
ll.addView(tv_sync_no);
ll.addView(tv_sync_date);
ll.addView(tv_sync_orderid);
ll.addView(tv_sync_qty);
tr.addView(ll);
showRow.addView(tr);
}
With listview you can easily do this:
ActivityMain.java
ListView lst = (ListView) findViewById(R.id.listView);
View header = getLayoutInflater().inflate(R.layout.list_header, null);
lst.addHeaderView(header);
lst.setAdapter(new CustomerListAdapter(this,4));
list_header.xml is header layout