How to display data in the tableLayout - android

Based on the source code from this webpage, it doesn't work because the data isn't displayed in the table. I also have tried a similar code but it didn't work.
What part am I missing?
The result don't display without any error message.
Today, I'm a newbie in Android.
Info:
*using Android studio with API 25
Thank you!
XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jfdimarzio.myapplication.MainActivity">
</TableLayout>
Java
package com.jfdimarzio.myapplication;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init()
{
TableLayout stk = (TableLayout) findViewById(R.id.activity_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" Sl.No ");
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Product ");
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Unit Price ");
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" Stock Remaining ");
tv3.setTextColor(Color.WHITE);
tbrow0.addView(tv3);
stk.addView(tbrow0);
for (int i = 0; i < 25; i++) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setText("" + i);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText("Product " + i);
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
TextView t3v = new TextView(this);
t3v.setText("Rs." + i);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setText("" + i * 15 / 32 * 10);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);
stk.addView(tbrow);
}
}
}

Related

how to set the dynamic table layout to full screen of the particular layout?

I am trying to fetch the database using php,i fetched and shown in dynamic table layout,the rows where created according to the number of rows in the database,but the table layout is in center of the screen but i need it to be fit in whole screen of the mobile/tab.
i tried to change the height and width of the all layout used in the particular screen but nothing is changed can anyone help me to fix this.
this is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".Colleagues_Schedule">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#3d455b"
android:layout_alignParentLeft="true" >
<HorizontalScrollView
android:id="#+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/table_main"
android:layout_margin="1dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:onClick="Back"
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PREVIOUS" />
<Button
android:onClick="Home"
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HOME" />
<Button android:onClick="Next"
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
main activity
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.nex3z.notificationbadge.NotificationBadge;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import static java.util.Calendar.DATE;
public class Colleagues_Schedule extends AppCompatActivity {
int i;
String result;
int size=20;
Context context;
int count = 0;
NotificationBadge mBadge;
Button buttonname, btnlogout, btntoday, btnyesterday, btntomorrow;
JSONArray jsonArray_New = null;
String[] ScheduleDates, StartTimes, Endtimes,Names;
String Send_Yesterday, Send_Today, Send_Tomorrow;
String ScheduleDate, StartTime, Endtime, Today, Yesterday, Tomarrow, Date,Name;
java.util.Date date1, Todaydate, Yesterdaydate, Tomarrowdate,date2;
TextView yes_date, yes_ST, yes_ET, tod_date, tod_ST, tod_ET, tom_date, tom_ST, tom_ET;
private ArrayList<String> ScheduleDateArray = new ArrayList<>();
private ArrayList<String> StartTimeArray = new ArrayList<>();
private ArrayList<String> EndtimeArray = new ArrayList<>();
private ArrayList<String> NameArray = new ArrayList<>();
SharedPreferences mPrefs;
GridLayout mainGrid2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_colleagues__schedule);
result = getIntent().getStringExtra("rootJson");
init();
}
#SuppressLint("ResourceType")
public void init() {
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" Name ");//date
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Date ");//name
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Start Time ");//starttime
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" End Time ");//endtime
tv3.setTextColor(Color.WHITE);
tbrow0.addView(tv3);
stk.addView(tbrow0);
try {
JSONObject jsonObjects = new JSONObject(result);
jsonArray_New = jsonObjects.getJSONArray("VIEW_SCHEDULE");
//Fetching the data from the php file
try {
for (int i = 0; i < jsonArray_New.length(); i++) {
JSONObject jsonObject = jsonArray_New.getJSONObject(i);
Name = jsonObject.getString("Name");
NameArray.add(Name);
ScheduleDate = jsonObject.getString("ScheduleDate");
ScheduleDateArray.add(ScheduleDate);
StartTime = jsonObject.getString("StartTime");
StartTimeArray.add(StartTime);
Endtime = jsonObject.getString("Endtime");
EndtimeArray.add(Endtime);
}
} catch (JSONException e) {
e.printStackTrace();
}
//Initilize the arraylist to array
Names = new String[NameArray.size()];
ScheduleDates = new String[ScheduleDateArray.size()];
StartTimes = new String[StartTimeArray.size()];
Endtimes = new String[EndtimeArray.size()];
for (int i = 0; i < ScheduleDateArray.size(); i++) {
Names[i] = NameArray.get(i);
ScheduleDates[i] = ScheduleDateArray.get(i);
StartTimes[i] = StartTimeArray.get(i);
Endtimes[i] = EndtimeArray.get(i);
}
//Search for Today schedule
for(int i=0;i<jsonArray_New.length();i++)
{
TableRow tbrow = new TableRow(this);
tbrow.setBackgroundResource(R.drawable.row_border);
TextView t1v = new TextView(this);
TextView t2v = new TextView(this);
TextView t3v = new TextView(this);
TextView t4v = new TextView(this);
tbrow.setBackgroundResource(R.drawable.row_border);
t1v.setText(Names[i]);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
t2v.setText(ScheduleDates[i] );
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
t3v.setText( StartTimes[i]);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
t4v.setText(Endtimes[i]);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);
stk.addView(tbrow);
tbrow.setClickable(true); //allows you to select a specific row
tbrow.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TableRow tablerow = (TableRow) view;
TextView sample = (TextView) tablerow.getChildAt(1);
String result=sample.getText().toString();
Toast toast = Toast.makeText(Colleagues_Schedule.this, result, Toast.LENGTH_LONG);
toast.show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
and this is my output scrren.
output screen
Try giving layout params to every row you add dynamically and update your init() function like below:
#SuppressLint("ResourceType")
public void init() {
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
//added layout params and weight
tbrow0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
TextView tv0 = new TextView(this);
tv0.setText(" Name ");//date
tv0.setTextColor(Color.WHITE);
//added layout params and weight
tv0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Date ");//name
tv1.setTextColor(Color.WHITE);
//added layout params and weight
tv1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Start Time ");//starttime
tv2.setTextColor(Color.WHITE);
//added layout params and weight
tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" End Time ");//endtime
tv3.setTextColor(Color.WHITE);
//added layout params and weight
tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow0.addView(tv3);
//added layout params and weight
stk.addView(tbrow0, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
try {
JSONObject jsonObjects = new JSONObject(result);
jsonArray_New = jsonObjects.getJSONArray("VIEW_SCHEDULE");
//Fetching the data from the php file
try {
for (int i = 0; i < jsonArray_New.length(); i++) {
JSONObject jsonObject = jsonArray_New.getJSONObject(i);
Name = jsonObject.getString("Name");
NameArray.add(Name);
ScheduleDate = jsonObject.getString("ScheduleDate");
ScheduleDateArray.add(ScheduleDate);
StartTime = jsonObject.getString("StartTime");
StartTimeArray.add(StartTime);
Endtime = jsonObject.getString("Endtime");
EndtimeArray.add(Endtime);
}
} catch (JSONException e) {
e.printStackTrace();
}
//Initilize the arraylist to array
Names = new String[NameArray.size()];
ScheduleDates = new String[ScheduleDateArray.size()];
StartTimes = new String[StartTimeArray.size()];
Endtimes = new String[EndtimeArray.size()];
for (int i = 0; i < ScheduleDateArray.size(); i++) {
Names[i] = NameArray.get(i);
ScheduleDates[i] = ScheduleDateArray.get(i);
StartTimes[i] = StartTimeArray.get(i);
Endtimes[i] = EndtimeArray.get(i);
}
//Search for Today schedule
for (int i = 0; i < jsonArray_New.length(); i++) {
TableRow tbrow = new TableRow(this);
tbrow.setBackgroundResource(R.drawable.row_border);
//added layout params and weight
tbrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
TextView t1v = new TextView(this);
TextView t2v = new TextView(this);
TextView t3v = new TextView(this);
TextView t4v = new TextView(this);
tbrow.setBackgroundResource(R.drawable.row_border);
t1v.setText(Names[i]);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
//added layout params and weight
t1v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t1v);
t2v.setText(ScheduleDates[i]);
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
//added layout params and weight
t2v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t2v);
t3v.setText(StartTimes[i]);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
//added layout params and weight
t3v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t3v);
t4v.setText(Endtimes[i]);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
//added layout params and weight
t4v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
tbrow.addView(t4v);
//added layout params and weight
stk.addView(tbrow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
tbrow.setClickable(true); //allows you to select a specific row
tbrow.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TableRow tablerow = (TableRow) view;
TextView sample = (TextView) tablerow.getChildAt(1);
String result = sample.getText().toString();
Toast toast = Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG);
toast.show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
And provide android:fillViewport="true" to your ScrollView with id=scrollView1 and HorizontalScrollView with id=hscrll1

Adding Table rows Dynamically in Android

I am trying to create a layout where I need to add table rows dynamically. Below is the table layout xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/displayLinear"
android:background="#color/background_df"
android:orientation="vertical" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/display_row"
android:layout_marginTop="280dip" >
</TableLayout>
The activity file where rows are being added dynamically is
public void init(){
menuDB = new MenuDBAdapter(this);
ll = (TableLayout) findViewById(R.id.displayLinear);
TableRow row=(TableRow)findViewById(R.id.display_row);
for (int i = 0; i <2; i++) {
checkBox = new CheckBox(this);
tv = new TextView(this);
addBtn = new ImageButton(this);
addBtn.setImageResource(R.drawable.add);
minusBtn = new ImageButton(this);
minusBtn.setImageResource(R.drawable.minus);
qty = new TextView(this);
checkBox.setText("hello");
qty.setText("10");
row.addView(checkBox);
row.addView(minusBtn);
row.addView(qty);
row.addView(addBtn);
ll.addView(row,i);
}
}
But when I run this, I am getting below error
08-13 16:27:46.437: E/AndroidRuntime(23568): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.roms/com.example.roms.DisplayActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I understand that this is due to command ll.addView(row,i); but when I remove this its adding all stuff in a single row rather tan creating a new row for next item. I tried with giving index too as row.addView(addBtn,i) but still its not populating correctly. Please advise. Thanks.
Create an init() function and point the table layout.
Then create the needed rows and columns.
public void init() {
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" Sl.No ");
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Product ");
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Unit Price ");
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" Stock Remaining ");
tv3.setTextColor(Color.WHITE);
tbrow0.addView(tv3);
stk.addView(tbrow0);
for (int i = 0; i < 25; i++) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setText("" + i);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText("Product " + i);
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
TextView t3v = new TextView(this);
t3v.setText("Rs." + i);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setText("" + i * 15 / 32 * 10);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);
stk.addView(tbrow);
}
}
Call init function in your onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
}
Layout file like:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#3d455b"
android:layout_alignParentLeft="true" >
<HorizontalScrollView
android:id="#+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/table_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
Will look like:
You shouldn't be using an item defined in the Layout XML in order to create more instances of it. You should either create it in a separate XML and inflate it or create the TableRow programmaticaly. If creating them programmaticaly, should be something like this:
public void init(){
TableLayout ll = (TableLayout) findViewById(R.id.displayLinear);
for (int i = 0; i <2; i++) {
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
checkBox = new CheckBox(this);
tv = new TextView(this);
addBtn = new ImageButton(this);
addBtn.setImageResource(R.drawable.add);
minusBtn = new ImageButton(this);
minusBtn.setImageResource(R.drawable.minus);
qty = new TextView(this);
checkBox.setText("hello");
qty.setText("10");
row.addView(checkBox);
row.addView(minusBtn);
row.addView(qty);
row.addView(addBtn);
ll.addView(row,i);
}
}
You also can, as Fredigato said, declare a RelativeLayout in a separate Layout file. Then instantiate it using:
for(int i = 0; i < 6; i ++){
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout row = (RelativeLayout) inflater.inflate(R.layout.table_view,null);
quizesTableLayout.addView(row,i);
}
In this approach you can easily design one custom row using XML and reuse it.
Now, to be able to change the children views in the instantiated RelativeLayout. You can call row.childAt(index).
So lets say you have a TextView in the RelativeLayout, you can use:
TextView tv = (TextView) row.childAt(0);
tv.setText("Text");
change code of init like following,
public void init(){
menuDB = new MenuDBAdapter(this);
ll = (TableLayout) findViewById(R.id.displayLinear);
ll.removeAllViews()
for (int i = 0; i <2; i++) {
TableRow row=(TableRow)findViewById(R.id.display_row);
checkBox = new CheckBox(this);
tv = new TextView(this);
addBtn = new ImageButton(this);
addBtn.setImageResource(R.drawable.add);
minusBtn = new ImageButton(this);
minusBtn.setImageResource(R.drawable.minus);
qty = new TextView(this);
checkBox.setText("hello");
qty.setText("10");
row.addView(checkBox);
row.addView(minusBtn);
row.addView(qty);
row.addView(addBtn);
ll.addView(row,i);
}
Activity
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="#+id/mytable"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</HorizontalScrollView>
Your Class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testtable);
table = (TableLayout)findViewById(R.id.mytable);
showTableLayout();
}
public void showTableLayout(){
Date date = new Date();
int rows = 80;
int colums = 10;
table.setStretchAllColumns(true);
table.bringToFront();
for(int i = 0; i < rows; i++){
TableRow tr = new TableRow(this);
for(int j = 0; j < colums; j++)
{
TextView txtGeneric = new TextView(this);
txtGeneric.setTextSize(18);
txtGeneric.setText( dateFormat.format(date) + "\t\t\t\t" );
tr.addView(txtGeneric);
/*txtGeneric.setHeight(30); txtGeneric.setWidth(50); txtGeneric.setTextColor(Color.BLUE);*/
}
table.addView(tr);
}
}
You can use an inflater with TableRow:
for (int i = 0; i < months; i++) {
View view = getLayoutInflater ().inflate (R.layout.list_month_data, null, false);
TextView textView = view.findViewById (R.id.title);
textView.setText ("Text");
tableLayout.addView (view);
}
Layout:
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:paddingLeft="15dp"
android:paddingBottom="10dp"
>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:gravity="center"
/>
</TableRow>
public Boolean addArtist(String artistName){
SQLiteDatabase db= getWritableDatabase();
ContentValues data=new ContentValues();
data.put(ArtistMaster.ArtistDetails.COLUMN_ARTIST_NAME,artistName);
long id = db.insert(ArtistMaster.ArtistDetails.TABLE_NAME,null,data);
if(id>0){
return true;
}else{
return false;
}
}
I am trying the above code with modification as per my requirement. The table is created properly. Bu the problem is when I am opening the activity 1st time, the table is not displaying. It is displaying 2nd time only.
My code is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getSupportActionBar().hide();
setContentView(R.layout.activity_aggre_ascendingorder);
drawerLayout = findViewById(R.id.drawer_layout);
init();
}
#SuppressLint("ResourceType")
public void init() {
stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText("Actual Values ");
tv0.setWidth(330);
tv0.setGravity(Gravity.CENTER);
tv0.setTextColor(Color.WHITE);
tv0.setTextSize(12);
tv0.setBackgroundResource(R.drawable.border4);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText("Meter Values ");
tv1.setWidth(330);
tv1.setGravity(Gravity.CENTER);
tv1.setTextColor(Color.WHITE);
tv1.setTextSize(12);
tv1.setBackgroundResource(R.drawable.border4);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText("Error ");
tv2.setWidth(315);
tv2.setGravity(Gravity.CENTER);
tv2.setTextColor(Color.WHITE);
tv2.setTextSize(12);
tv2.setBackgroundResource(R.drawable.border4);
tbrow0.addView(tv2);
Intent z = getIntent();
input1 = z.getIntExtra(EDIT, 1);
input2 = z.getIntExtra(TEXT, 1);
if (input2 == 0) {
x = 400;
} else {
x = input1 / input2;
}
stk.addView(tbrow0);
try {
for (int i = 0; i < x + 1; i++) {
TableRow tbrow = new TableRow(this);
final EditText t1v = new EditText(this);
t1v.setId(1);
if (input2 == 0) {
t1v.setHint("");
} else {
t1v.setText("" + i * input2);
}
final EditText t2v;
TextView t3v = null;
t1v.setTextColor(Color.BLACK);
t1v.setGravity(Gravity.CENTER);
t1v.setTextSize(12);
t1v.setInputType(InputType.TYPE_CLASS_NUMBER);
t1v.setBackgroundResource(R.drawable.border);
t2v = new EditText(this);
t2v.setHint("");
t2v.setTextColor(Color.BLACK);
t2v.setGravity(Gravity.CENTER);
t2v.setTextSize(12);
t2v.setInputType(InputType.TYPE_CLASS_NUMBER);
t2v.setBackgroundResource(R.drawable.border);
t3v = new TextView(this);
t3v.setHint("");
t3v.setTextColor(Color.BLACK);
t3v.setGravity(Gravity.CENTER);
t3v.setTextSize(12);
t3v.setBackgroundResource(R.drawable.border);
String stb = "0,0,0";
if((Integer.parseInt(Prefs.getString("aggasstabLength",""))-1) > i){
stb = Prefs.getString("aggasstabVal_" + (i + 1), "");
}
//t1v.setText(stb.split(",")[0]);
t2v.setText(stb.split(",")[1]);
t3v.setText(stb.split(",")[2]);
tbrow.addView(t1v);
tbrow.addView(t2v);
tbrow.addView(t3v);
stk.addView(tbrow);
`
Can you clear me what is the mistake I am doing on this

extend tablerow view

I am trying to programmatically create this xml view by extending tablerow view:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:weightSum="1.0" >
<Button
android:id="#+id/tester_button"
style="?android:attr/buttonStyleSmall"
android:layout_weight=".2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="A" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_weight=".7"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_weight=".1"
android:layout_width="0dip"
android:layout_height="wrap_content" />
</TableRow>
This is my class of the extended row:
public class CategoryRow extends TableRow {
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
LayoutParams innerParams = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
CheckBox check;
Button arrowButton;
TextView categoryTitle;
public CategoryRow(Context context) {
super(context);
this.setLayoutParams(rowParams);
this.setGravity(Gravity.CENTER);
this.setWeightSum(1);
check = new CheckBox(context);
innerParams.weight = (float) 0.1;
check.setLayoutParams(innerParams);
arrowButton = new Button(context);
innerParams.weight = (float) 0.2;
arrowButton.setLayoutParams(innerParams);
arrowButton.setText("A");
categoryTitle = new TextView(context);
innerParams.weight = (float) 0.7;
categoryTitle.setLayoutParams(innerParams);
categoryTitle.setTextAppearance(context, android.R.attr.textAppearanceLarge);
//add views to row
this.addView(arrowButton);
this.addView(categoryTitle);
this.addView(check);
}
public void setCategoryTitle(String title){
categoryTitle.setText(title);
}
public void checkCategory(){
check.setChecked(true);
}
public void unCheckCategory(){
check.setChecked(false);
}
}
in my activity I am adding the CategoryRow view to the TableLayout by addView function.
public class NotificationCategorise extends Activity {
TableLayout categoryConteiner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_categorise);
categoryConteiner = (TableLayout) findViewById(R.id.categories_conteiner);
CategoryRow categoryRow = new CategoryRow(this);
categoryRow.setCategoryTitle("bla");
categoryConteiner.addView(categoryRow);
}
My problem is that it the CategoryRow not added to the activity sceen.
I have changed the extended class to this code, and it worked fine.
import android.content.Context;
import android.view.Gravity;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TableRow;
import android.widget.TextView;
public class CategoryRow extends TableRow {
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams innerParams1 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
LayoutParams innerParams2 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
LayoutParams innerParams3 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
CheckBox check;
Button arrowButton;
TextView categoryTitle;
public CategoryRow(Context context) {
super(context);
this.setLayoutParams(rowParams);
this.setWeightSum(1f);
check = new CheckBox(context);
innerParams1.weight = 0.1f;
check.setLayoutParams(innerParams1);
arrowButton = new Button(context);
innerParams2.weight = 0.2f;
arrowButton.setLayoutParams(innerParams2);
arrowButton.setText("A");
categoryTitle = new TextView(context);
categoryTitle.setGravity(Gravity.CENTER);
innerParams3.weight = 0.7f;
categoryTitle.setLayoutParams(innerParams3);
categoryTitle.setTextAppearance(context, android.R.attr.textAppearanceLarge);
//add views to row
this.addView(arrowButton);
this.addView(categoryTitle);
this.addView(check);
}
public void setCategoryTitle(String title){
categoryTitle.setText(title);
}
public void checkCategory(){
check.setChecked(true);
}
public void unCheckCategory(){
check.setChecked(false);
}
}
Ok, I don't think you can pass in the GRAVITY.CENTER as an argument for the LayoutParams constructor for the row. You change it for each individual View added to the row, but not for the row itself. Change:
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
to
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
See if that works.

dynamically creating tablelayout (android)

I want to create inner tablelayout and add there 1 row with 2 columns, following code shows nothing, why?
Here's main activity:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
TextView tv2=new TextView(this);
tv1.setLayoutParams(params);
tv2.setLayoutParams(params);
tv1.setText("Hello1!");
tv2.setText("Hello2!");
TableLayout layoutINNER = new TableLayout(this);
layoutINNER.setLayoutParams(params);
TableRow tr = new TableRow(this);
tr.setLayoutParams(params);
tr.addView(tv1);
tr.addView(tv2);
layoutINNER.addView(tr);
LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
main.addView(layoutINNER);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/android:main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
UPDATE:
Well, finally i solved this problem, it was just needed to use TableRow.LayoutParams with TextViews, not LinearLayout.LayoutParams or some other
Here is the working code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
TextView tv2=new TextView(this);
android.widget.TableRow.LayoutParams trparams = new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(trparams);
tv2.setLayoutParams(trparams);
tv1.setText("Hello1!");
tv2.setText("Hello2!");
TableLayout layoutINNER = new TableLayout(this);
layoutINNER.setLayoutParams(params);
TableRow tr = new TableRow(this);
tr.setLayoutParams(params);
tr.addView(tv1);
tr.addView(tv2);
layoutINNER.addView(tr);
LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
main.addView(layoutINNER);
}
}

Dynamically add ListView in the code to ScrollView

I am trying to add a ListView to a ScrollView inside the code. But it's not working for some reason. I am new to this. Can somebody help me out?
package net.pocketmagic.android.HeaderContentFooterGUI;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
/* HeaderContentFooterGUI
*
* Create a sample GUI with a header bar, scroll-able content and a footer
*
* (C) 2011 Radu Motisan, radu.motisan#gmail.com
* www.pocketmagic.net
* All rights reserved.
*/
public class HeaderContentFooterGUI extends Activity {
final static int idTopLayout = Menu.FIRST + 100,
idBack = Menu.FIRST + 101,
idBotLayout = Menu.FIRST + 102;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Hide titlebar
requestWindowFeature(Window.FEATURE_NO_TITLE);
//Create our top content holder
RelativeLayout global_panel = new RelativeLayout (this);
global_panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
global_panel.setGravity(Gravity.FILL);
// +++++++++++++ TOP COMPONENT: the header
RelativeLayout ibMenu = new RelativeLayout(this);
ibMenu.setId(idTopLayout);
ibMenu.setBackgroundDrawable(getResources().getDrawable(R.drawable.line));
int ibMenuPadding = (int) 6;
ibMenu.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
topParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
global_panel.addView(ibMenu,topParams);
// textview in ibMenu : card holder
TextView cTV = new TextView(this);
cTV.setText("Abirami Mega Mall");
cTV.setTextColor(Color.rgb(255,255,255));
int nTextH = 18;
cTV.setTextSize(nTextH);
cTV.setTypeface(Typeface.create("arial", Typeface.BOLD));
RelativeLayout.LayoutParams lpcTV = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpcTV.addRule(RelativeLayout.CENTER_IN_PARENT);
ibMenu.addView(cTV, lpcTV);
// +++++++++++++ BOTTOM COMPONENT: the footer
nTextH = 13;
ibMenuPadding = (int) 15;
RelativeLayout ibMenuBot = new RelativeLayout(this);
ibMenuBot.setId(idBotLayout);
ibMenuBot.setBackgroundDrawable(getResources().getDrawable(R.drawable.line));
ibMenuBot.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams botParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
botParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
global_panel.addView(ibMenuBot,botParams);
// textview in ibMenu : card holder
TextView cTVBot = new TextView(this);
cTVBot.setId(5001);
cTVBot.setText("Home");
cTVBot.setTextColor(Color.rgb(179,116,197));
cTVBot.setTextSize(nTextH);
cTVBot.setTypeface(Typeface.create("arial", Typeface.NORMAL));
cTVBot.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams lpcTVBot = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpcTVBot.addRule(RelativeLayout.CENTER_VERTICAL);
ibMenuBot.addView(cTVBot, lpcTVBot);
TextView cTVBot2 = new TextView(this);
cTVBot2.setId(5002);
cTVBot2.setText("Booking History");
cTVBot2.setTextColor(Color.rgb(179,116,197));
cTVBot2.setTextSize(nTextH);
cTVBot2.setTypeface(Typeface.create("arial", Typeface.NORMAL));
cTVBot2.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams lpcTVBot2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpcTVBot2.addRule(RelativeLayout.RIGHT_OF,cTVBot.getId() );
lpcTVBot2.addRule(RelativeLayout.CENTER_VERTICAL);
ibMenuBot.addView(cTVBot2, lpcTVBot2);
TextView cTVBot3 = new TextView(this);
cTVBot3.setId(5003);
cTVBot3.setText("Help?");
cTVBot3.setTextColor(Color.rgb(179,116,197));
cTVBot3.setTextSize(nTextH);
cTVBot3.setTypeface(Typeface.create("arial", Typeface.NORMAL));
cTVBot3.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams lpcTVBot3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpcTVBot3.addRule(RelativeLayout.RIGHT_OF,cTVBot2.getId() );
lpcTVBot3.addRule(RelativeLayout.CENTER_VERTICAL);
ibMenuBot.addView(cTVBot3, lpcTVBot3);
TextView cTVBot4 = new TextView(this);
cTVBot4.setId(5004);
cTVBot4.setText("About Us");
cTVBot4.setTextColor(Color.rgb(179,116,197));
cTVBot4.setTextSize(nTextH);
cTVBot4.setTypeface(Typeface.create("arial", Typeface.NORMAL));
cTVBot4.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams lpcTVBot4= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpcTVBot4.addRule(RelativeLayout.RIGHT_OF,cTVBot3.getId() );
lpcTVBot4.addRule(RelativeLayout.CENTER_VERTICAL);
ibMenuBot.addView(cTVBot4, lpcTVBot4);
// +++++++++++++ MIDDLE COMPONENT: all our GUI content
LinearLayout midLayout = new LinearLayout (this);
midLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
midLayout.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams midParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
midParams.addRule(RelativeLayout.ABOVE,ibMenuBot.getId());
midParams.addRule(RelativeLayout.BELOW,ibMenu.getId());
global_panel.addView(midLayout,midParams );
//scroll - so our content will be scrollable between the header and the footer
ScrollView vscroll = new ScrollView(this);
vscroll.setId(6004);
vscroll.setFillViewport(false);
midLayout.addView(vscroll);
//below code not working *******
ListView maListViewPerso = (ListView) findViewById(vscroll.getId());
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("titre", "Movie Ticket");
map.put("description", "Movie Ticket Booking");
map.put("img", String.valueOf(R.drawable.reel_icon));
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "Snow World");
map.put("description", "SnowWorld Tickets");
map.put("img", String.valueOf(R.drawable.reel_icon));
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "Kissing Car");
map.put("description", "Kissing Car Tickets");
map.put("img", String.valueOf(R.drawable.reel_icon));
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "Events");
map.put("description", "Events in Mall");
map.put("img", String.valueOf(R.drawable.reel_icon));
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "Screens");
map.put("description", "Screens # Abirami");
map.put("img", String.valueOf(R.drawable.reel_icon));
listItem.add(map);
SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.main_menu_list,
new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
maListViewPerso.setAdapter(mSchedule);
setContentView(global_panel);
}
}
main_menu_list.xml
------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/img"
android:layout_gravity="center_vertical"
android:padding="10px"
android:layout_width="70dp"
android:layout_height="70dp"/>
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10px"
android:paddingRight="10px"
android:layout_weight="1"
>
<TextView android:id="#+id/titre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="26px"
android:textStyle="bold"
/>
<TextView android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Do not add a ListView to a ScrollView if you don't know exaclty what you are doing.
Do not add a ListView to a ScrollView also if you are the best android programmer in the World.
ListViews are not supposed to get in ScrollView. Even if you hack, and i mean big hack, both views by extending them and got them to scroll , also be itemclickable etc etc , something will break eventually.
Also do not add a ListView to a ScrollView.
Watch this
You should first get the ListView in you layout using findViewById(android.R.id.list) then you cant add the items into ListView and then add this to scrollview.
You cannot get reference to the ListView this way. You should execute findViewById(/Id of the listview element goes here/).
Edit:
Suppose you have:
<ListView android:layout_height="wrap_content" android:id="#+id/listView1" android:layout_weight="1" android:layout_width="fill_parent"></ListView>
You can get access to the listview by:
ListView l = findViewById(R.id.listView1);
Hope this helps!
Got the solution....here is the complete the code...
package net.pocketmagic.android.HeaderContentFooterGUI;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class HeaderContentFooterGUI extends Activity {
final static int idTopLayout = Menu.FIRST + 100,
idBack = Menu.FIRST + 101,
idBotLayout = Menu.FIRST + 102;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Hide titlebar
requestWindowFeature(Window.FEATURE_NO_TITLE);
//Create our top content holder
RelativeLayout global_panel = new RelativeLayout (this);
global_panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
global_panel.setGravity(Gravity.FILL);
// +++++++++++++ TOP COMPONENT: the header
RelativeLayout ibMenu = new RelativeLayout(this);
ibMenu.setId(idTopLayout);
ibMenu.setBackgroundDrawable(getResources().getDrawable(R.drawable.line));
int ibMenuPadding = (int) 6;
ibMenu.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
topParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
global_panel.addView(ibMenu,topParams);
// textview in ibMenu : card holder
TextView cTV = new TextView(this);
cTV.setText("Abirami Mega Mall");
cTV.setTextColor(Color.rgb(255,255,255));
int nTextH = 18;
cTV.setTextSize(nTextH);
cTV.setTypeface(Typeface.create("arial", Typeface.BOLD));
RelativeLayout.LayoutParams lpcTV = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpcTV.addRule(RelativeLayout.CENTER_IN_PARENT);
ibMenu.addView(cTV, lpcTV);
// +++++++++++++ BOTTOM COMPONENT: the footer
nTextH = 13;
ibMenuPadding = (int) 15;
RelativeLayout ibMenuBot = new RelativeLayout(this);
ibMenuBot.setId(idBotLayout);
ibMenuBot.setBackgroundDrawable(getResources().getDrawable(R.drawable.line));
ibMenuBot.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams botParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
botParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
global_panel.addView(ibMenuBot,botParams);
// textview in ibMenu : card holder
TextView cTVBot = new TextView(this);
cTVBot.setId(5001);
cTVBot.setText("Home");
cTVBot.setTextColor(Color.rgb(179,116,197));
cTVBot.setTextSize(nTextH);
cTVBot.setTypeface(Typeface.create("arial", Typeface.NORMAL));
cTVBot.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams lpcTVBot = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpcTVBot.addRule(RelativeLayout.CENTER_VERTICAL);
ibMenuBot.addView(cTVBot, lpcTVBot);
TextView cTVBot2 = new TextView(this);
cTVBot2.setId(5002);
cTVBot2.setText("Booking History");
cTVBot2.setTextColor(Color.rgb(179,116,197));
cTVBot2.setTextSize(nTextH);
cTVBot2.setTypeface(Typeface.create("arial", Typeface.NORMAL));
cTVBot2.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams lpcTVBot2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpcTVBot2.addRule(RelativeLayout.RIGHT_OF,cTVBot.getId() );
lpcTVBot2.addRule(RelativeLayout.CENTER_VERTICAL);
ibMenuBot.addView(cTVBot2, lpcTVBot2);
TextView cTVBot3 = new TextView(this);
cTVBot3.setId(5003);
cTVBot3.setText("Help?");
cTVBot3.setTextColor(Color.rgb(179,116,197));
cTVBot3.setTextSize(nTextH);
cTVBot3.setTypeface(Typeface.create("arial", Typeface.NORMAL));
cTVBot3.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams lpcTVBot3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpcTVBot3.addRule(RelativeLayout.RIGHT_OF,cTVBot2.getId() );
lpcTVBot3.addRule(RelativeLayout.CENTER_VERTICAL);
ibMenuBot.addView(cTVBot3, lpcTVBot3);
TextView cTVBot4 = new TextView(this);
cTVBot4.setId(5004);
cTVBot4.setText("About Us");
cTVBot4.setTextColor(Color.rgb(179,116,197));
cTVBot4.setTextSize(nTextH);
cTVBot4.setTypeface(Typeface.create("arial", Typeface.NORMAL));
cTVBot4.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
RelativeLayout.LayoutParams lpcTVBot4= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpcTVBot4.addRule(RelativeLayout.RIGHT_OF,cTVBot3.getId() );
lpcTVBot4.addRule(RelativeLayout.CENTER_VERTICAL);
ibMenuBot.addView(cTVBot4, lpcTVBot4);
// +++++++++++++ MIDDLE COMPONENT: all our GUI content
LinearLayout midLayout = new LinearLayout (this);
midLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
midLayout.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams midParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
midParams.addRule(RelativeLayout.ABOVE,ibMenuBot.getId());
midParams.addRule(RelativeLayout.BELOW,ibMenu.getId());
global_panel.addView(midLayout,midParams );
ListView maListViewPerso = new ListView(this);
maListViewPerso.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("titre", "Movie Ticket");
map.put("description", "Movie Ticket Booking");
map.put("img", String.valueOf(R.drawable.reel_icon));
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "Snow World");
map.put("description", "SnowWorld Tickets");
map.put("img", String.valueOf(R.drawable.reel_icon));
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "Kissing Car");
map.put("description", "Kissing Car Tickets");
map.put("img", String.valueOf(R.drawable.reel_icon));
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "Events");
map.put("description", "Events in Mall");
map.put("img", String.valueOf(R.drawable.reel_icon));
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "Screens");
map.put("description", "Screens # Abirami");
map.put("img", String.valueOf(R.drawable.reel_icon));
listItem.add(map);
SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.main_menu_list,
new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
maListViewPerso.setAdapter(mSchedule);
midLayout.addView(maListViewPerso);
setContentView(global_panel);
}
}

Categories

Resources