my app needs to display data in a list, I walked through some tutorials and figured out to use a header.xml and row.xml. It works fine but now I would like to add more stuff on my header.xml and I found out that the entire header.xml is scrolling with the row.xml as well, which I dont really want.
Any solution that doesn't require me to rewrite and change my code style completely?
Activity:
public class HistoryActivity extends ListActivity
{
private static final String TAG = "HistoryActivity";
ListView lv;
SimpleAdapter sd;
RecordDAO dao = new RecordDAO(HistoryActivity.this);
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
lv = getListView();
lv.addHeaderView(getLayoutInflater().inflate(
R.layout.header, null, false));
}
#Override
protected void onResume()
{
super.onResume();
ArrayList<Record> Records = new ArrayList<Record>();
Records = (ArrayList<Record>) dao.findAll();
ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int x = Records.size()-1; x >=0; x--)
{
map = new HashMap<String, String>();
map.put(....// all my data
aList.add(map);
}
sd = new SimpleAdapter(this, aList, R.layout.row,
new String[]
{ "date", "name", "time",
"rating" }, new int[]
{ R.id.date, R.id.name, R.id.time,
R.id.rating});
lv.setAdapter(sd);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3)
{
TextView tx = (TextView) view.findViewById(R.id.date);
String s = tx.getText().toString();
Intent intent = new Intent(HistoryActivity.this, EditRecordActivity.class);
intent.putExtra("date", s);
startActivity(intent);
}
});
}
private void insertNewRecord()
{
dao.add(newRecord);
}
}
header.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rating"
android:textColor="#FFFFFF"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/Rating"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
</LinearLayout>
Don't use ListActivity. I think it is a bad practice. Use a regular Activity. Just insert a ListView with an id "#android:id/list" into header.xml.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rating"
android:textColor="#FFFFFF"
android:textSize="16dp" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
And you can get the list reference by doing:
ListView lv = (ListView)findViewById(R.id.list);
Setting the Header of a ListView means that it will be the first item of your list, but still will scroll with the list itself.
In your case, you can create some kind of layout (similar to header.xml) and place it above the ListView.
Instead of ListActivity, extend from Activity
Create a main_layout.xml that define some views and a ListView.
main_layout
<include header.xml/>
<listview>
If you don't want your header and footer views to scroll with your ListView, then make sure that you use the addHeaderView() and addFooterView() BEFORE you call your setAdapter() method. I would recommend moving the setAdapter() method to your onCreate() method.
Related
I am work on ArrayList.I want to display ArrayList in ListView ,I don't have any idea about using ArrayList in ListView.
In the picture "Cumputer Science" is the subject ,it should come under sub and other values also come under each one.
//layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:background="#drawable/login"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_marginTop="175dp" android:orientation="horizontal" android:background="#android:color/darker_gray"
android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="20dp">
<TextView android:layout_width="wrap_content" android:layout_height="match_parent"
android:textSize="16dp" android:text="Sub" android:layout_marginLeft="5dp"/>
<TextView android:layout_width="wrap_content" android:layout_height="match_parent"
android:textSize="16dp" android:text="Mon" android:layout_marginLeft="20dp"/>
<TextView android:layout_width="wrap_content" android:layout_height="match_parent"
android:textSize="16dp" android:text="Tue" android:layout_marginLeft="23dp"/>
<TextView android:layout_width="wrap_content" android:layout_height="match_parent"
android:textSize="16dp" android:text="Wed" android:layout_marginLeft="23dp"/>
<TextView android:layout_width="wrap_content" android:layout_height="match_parent"
android:textSize="16dp" android:text="Thu" android:layout_marginLeft="23dp"/>
<TextView android:layout_width="wrap_content" android:layout_height="match_parent"
android:textSize="16dp" android:text="Fri" android:layout_marginLeft="23dp" android:layout_marginRight="2dp"/>
</LinearLayout>
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_marginLeft="5dp">
<ListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="#+id/getAttdnce_lv">
</ListView>
</LinearLayout>
</LinearLayout>
Try to use TableLayout and then create rows dynamically according to your need.
public class MainActivity extends Activity {
public static final String[] Subject = new String[] { "Maths","Computer", "english"};
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, Subject);
lv.setAdapter(adapter);
}
}
I want to display only 5 rows in a ListView and rest of them should be scrollable.So far i have tried this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/txtFilename"
android:layout_width="375dp"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<ListView
android:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarSize="2dp"
android:
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
android:padding="5dp" >
<Button
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"/>
<Button
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CANCEL"/>
</LinearLayout>
</LinearLayout>
and in java code
public class MainActivity extends Activity {
private ListView list1;
private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",
"item 1", "item 2", "item3", "item 4" };
#SuppressWarnings("unchecked")
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//parentLayout.add(childLayout);
setContentView(R.layout.activity_main);
list1 = (ListView) findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in
// list.
list1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, array));
}
have any idea how to achieve it
Here i am Just Modifying Your Layout just check it out!!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/txtFilename"
android:layout_width="375dp"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="300dip">
<ListView
android:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarSize="2dp"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
android:padding="5dp" >
<Button
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"/>
<Button
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CANCEL"/>
</LinearLayout>
</LinearLayout>
I have written Demo for this Like:
if you want to fix Height of ListView to 5 items. then you have to set Height of ListView pragmatically by calculating listItems height.
First you need to create Layout for List Item and give it some fixed height, here i'm added height in Tag cause runtime i was getting layouts height as 0.
This is ListItem Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="fill_parent"
android:background="#android:color/black"
android:tag="40"
android:layout_height="40dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXX"
android:textColor="#android:color/darker_gray"
android:textSize="30sp" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/text"
android:text="ZZZ"
android:textColor="#android:color/darker_gray"
android:textSize="30sp" />
</RelativeLayout>
and to set Height of ListView:
list = (ListView) findViewById(R.id.listView1);
adapter = new Adapter(MainActivity.this, setter);
list.setAdapter(adapter);
RelativeLayout.LayoutParams lstViewParams = (LayoutParams) list.getLayoutParams();
View lisItemInflatedView = View.inflate(this, R.layout.item, null);
RelativeLayout relLayoutInLstItem = (RelativeLayout) lisItemInflatedView.findViewById(R.id.relativeLayout);
final float scale = this.getResources().getDisplayMetrics().density;
int pixels = (int) ( Integer.parseInt( relLayoutInLstItem.getTag().toString() ) * scale + 0.5f);
int listHeigt = pixels * 5 + 5;
lstViewParams.height = listHeigt;
here's the screenshot..
Hope this will Help.
I'm developing an application with ListView for Student Marklist creation.
In this application, the List have 10 students. There are four grades provided for the exam which was conducted to those Students. One student can adapt only one grade from the four.
The teacher will assign the grade to the student in ListView.
My xml file Studentlist.xml is as follow:
<?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" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
and my row.xml file is as follow:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500dp"
android:layout_height="fill_parent"
android:padding="10dp" >
<TableRow>
<TextView
android:id="#+id/SNo"
style="#style/text"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/StudNo"
style="#style/text"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/StudName"
style="#style/text"
android:layout_width="180dp"
android:layout_height="wrap_content" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="40dp"
android:layout_height="wrap_content" />
</RadioGroup>
</TableRow>
Now I'm trying for the output in the form of:
1 0001 AAAA <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4>
2 0002 BBBB <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4>
How can I use the Adapter functionalities?
ArrayAdapter<String,String,String,RadiGroup> studList=new ArrayAdapter<String,String,String,RadiGroup>();
Can I use like this, and how to develop the customized Adapter for the ListView?
Suggest me for the best solution!
This is a sample example of custom adapter for listview. Modify it as your need:
XML layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/main"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Header -->
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black" >
<TextView
android:id="#+id/item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="30dip"
android:text="Latest News - Your Healing Place"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:width="100dip" />
<TextView
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:height="30dip"
android:width="20dip" />
</LinearLayout>
<View android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
<LinearLayout android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/black" >
</ListView>
</LinearLayout>
</LinearLayout>
news.java :
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
String[] from = new String[] {"details", "date"};
int[] to = new int[] { R.id.item1, R.id.item2};
ListView lv= (ListView)findViewById(R.id.listview);
HashMap<String, String> map = new HashMap<String, String>();
map.put("details", "This is a sample message");
map.put("date", "24-07-2003");
fillMaps.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
You can use a class inheriting from ArrayAdapter, and overriding its getView() method.
public class StudentAdapter extends ArrayAdapter<Student> {
protected LayoutInflater inflater;
public StudentAdapter(final Context context) {
super(context, 0);
inflater = (LayoutInflater) ((Context) context)
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
// Note: You should optimize here with re-using convertView
View rowView = inflater.inflate(R.layout.user_address_row_layout,
parent, false);
TextView sNo = (TextView) rowView.findViewById(R.id.sNo);
sNo.setText(getItem(position).number);
// same for every field of the row
// ...
return rowView;
}
}
I couldn't find an answer for my problem, I have a Main XML with some TextViews and 1 ListView, the problem is when my List appear on my Main xml it adds the Texviews that are outside the listview, so I get in each Item a TOTAL textview and the list go in all the XML layout, not at the bottom, so I am not sure what am doing wrong
thx in advanced for your help
Regards
here are my files
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:stretchColumns="1" >
<TableRow>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Expenses $"
android:textColor="#android:color/white"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvTotalGastos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#android:color/white"
android:textSize="25sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/TableLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/TableLayout01"
android=""
android:orientation="horizontal" >
<TableRow>
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Date"
android:textColor="#android:color/white" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Description"
android:textColor="#android:color/white" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Amount"
android:textColor="#android:color/white" />
</TableRow>
</TableLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
My Row xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="#+id/tvDate"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Date" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Description" />
<TextView
android:id="#+id/tvAmount"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="10dip"
android:text="Amount" />
</LinearLayout>
and my Java class
public class GastosListActivity extends ListActivity {
private DatabaseHandler dbh;
private List<Datos> datos;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.gastoslist);
//ListView list2= (ListView) findViewById(R.id.listview);
// new obj databasehandler
dbh = new DatabaseHandler(this);
datos = dbh.getAllData();
ArrayList<Map<String, String>> list = buildData();
String[] from = { "Date", "Description","Amount" };
int[] to = { R.id.tvDate, R.id.tvDescription,R.id.tvAmount };
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.gastoslist, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (Datos dn : datos) {
list.add(putData(dn.get_fecha(), dn.get_concepto(), dn.get_cantidad()));
}
return list;
}
private HashMap<String, String> putData(String date, String description, String amount) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("Date", date);
item.put("Description", description);
item.put("Amount", amount);
return item;
}
}
Problem Solve, I change the code using Cursor instead of arraylist
dbh = new DatabaseHandler(this);
Cursor cursor = dbh.obtenerDatos();
String[] from = new String[] { dbh.COLUMN_FECHA,
dbh.COLUMN_CONCEPTO, dbh.COLUMN_CANTIDAD };
int[] to = new int[] { R.id.tvDate, R.id.tvDescription,
R.id.tvAmount };
cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor,
from, to);
listContent.setAdapter(cursorAdapter);
This is my main.xml:`
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:addStatesFromChildren="false"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/header"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_width="fill_parent"
android:layout_alignParentTop="true">
<TableRow>
<TextView
android:id="#+id/leftHeader"
android:textColor="#android:color/black"
android:layout_margin="1dp"
android:gravity="center_horizontal"
android:layout_weight="1"
android:background="#ffcccccc"
android:text="textview1"
android:layout_width="30dip" />
<TextView
android:id="#+id/rightHeader"
android:textColor="#android:color/black"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_margin="1dp"
android:background="#android:color/white"
android:text="textview2"
android:layout_width="70dip" />
</TableRow>
</TableLayout>
<ScrollView
android:id="#+id/table_scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_below="#+id/header"
android:layout_above="#+id/buttonTable">
<TableLayout
android:id="#+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<TableRow
android:id="#+id/maintableRow">
<TextView
android:id="#+id/maintableLeft"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginRight="1dp"
android:layout_marginLeft="1dp"
android:text="textview1"
android:layout_width="30dip" />
<TextView
android:id="#+id/maintableRight"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:text="textview2"
android:layout_width="70dip" />
</TableRow>
</TableLayout>
</ScrollView>
<TableLayout
android:id="#+id/buttonTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_alignParentBottom="true">
<TableRow>
<Button
android:id="#+id/button_ResetData"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Reset Data"
android:layout_gravity="center"
android:layout_weight="1" />
<Button
android:id="#+id/button_Refresh"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Refresh"
android:gravity="center"
android:layout_weight="1" />
</TableRow>
</TableLayout>
`
After I press one of the buttons its text alignment goes to the left, how can I make it stay in center?
something similar happens with the first table (header) (textview1, textview2) but here it also reduses the textsize the amount of the text in the view...
EDIT: I managed to find the code that causes the change but I don't know how to fix this.
Maybe some one can explain me why does this happen and how to fix it?
private void fillTableView(List<BatteryInfo> list) {
TextView leftHeader = (TextView) findViewById(R.id.leftHeader);
TextView rightHeader = (TextView) findViewById(R.id.rightHeader);
LayoutParams leftHeaderLayoutParams = leftHeader.getLayoutParams();
LayoutParams rightHeaderLayoutParams = rightHeader.getLayoutParams();
TableLayout contentTable = (TableLayout) findViewById(R.id.maintable);
contentTable.removeAllViews();
for (int i = list.size() - 1; i >= 0; i--) {
TextView tv1 = new TextView(this);
tv1.setTextColor(Color.BLACK);
tv1.setGravity(Gravity.CENTER);
tv1.setBackgroundColor(Color.LTGRAY);
tv1.setLayoutParams(leftHeaderLayoutParams);
tv1.setText(String.valueOf(list.get(i).getLevel()));
TextView tv2 = new TextView(this);
tv2.setTextColor(Color.BLACK);
tv2.setGravity(Gravity.CENTER);
tv2.setBackgroundColor(Color.WHITE);
tv2.setLayoutParams(rightHeaderLayoutParams);
tv2.setText(list.get(i).getDurationTimeInString());
TableRow tblRow = new TableRow(this);
tblRow.addView(tv1);
tblRow.addView(tv2);
contentTable.addView(tblRow);
}
}
After I use removeAllViews or any addView function the layout of the top most table changes as I explained before. Why does this happen?
You can fix this by changing the android:layout_width="30dip" and android:layout_width="70dip" in leftHeader and rightHeader to android:layout_width="fill_parent". I have to admit that I'm not entirely certain why that is, though.
Instead of clearing and re-filling a TableLayout every time, though, you might want to consider using a ListAdapter. It's much more suited to what you're doing (and will probably perform better, too).
UPDATE NUMBER 2:
Notice the change in the main.xml and the change in the onCreate method. This will make your header static while letting your listview scroll, and changing from TableLayout to LinearLayout for your buttons will fix the alignment issue.
Here's how to do this with a custom adapter instead of TableLayout (which isn't really meant for dynamic data like this):
Change you main.xml to this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:addStatesFromChildren="false" android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:id="#+id/header">
<TextView android:id="#+id/leftHeader" android:textColor="#android:color/black"
android:layout_margin="1dp" android:gravity="center_horizontal"
android:layout_weight="1" android:background="#ffcccccc" android:text="textview1"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="#+id/rightHeader" android:textColor="#android:color/black"
android:gravity="center_horizontal" android:layout_weight="1"
android:layout_margin="1dp" android:background="#android:color/white"
android:text="textview2" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:id="#+id/buttonTable" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_alignParentBottom="true" >
<Button android:id="#+id/button_ResetData"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Reset Data" android:layout_gravity="center"
android:layout_weight="1" />
<Button android:id="#+id/button_Refresh"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Refresh" android:gravity="center"
android:layout_weight="1" />
</LinearLayout>
<ListView android:id="#+id/listView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"
android:fadingEdge="none"
android:layout_below="#id/header" android:layout_above="#id/buttonTable">
</ListView>
Add XML layout files for your rows (row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="#+id/maintableLeft"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_marginRight="1dp" android:layout_marginLeft="1dp"
android:gravity="center_horizontal" android:background="#ffcccccc"
android:textColor="#android:color/black" android:layout_width="fill_parent" />
<TextView android:id="#+id/maintableRight"
android:layout_height="wrap_content" android:background="#ffcccccc"
android:textColor="#android:color/black" android:layout_weight="1"
android:layout_marginLeft="1dp" android:layout_marginRight="1dp"
android:gravity="center_horizontal" android:layout_width="fill_parent" />
</LinearLayout>
Now everything is in place to create a custom class from BaseAdapter to display your information (and make your layout work better):
class CustomAdapter extends BaseAdapter {
private List<BatteryInfo> info;
private LayoutInflater inflater;
public CustomAdapter(Context context, List<BatteryInfo> x) {
this.info = x;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return info.size();
}
public Object getItem(int position) {
return info.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
v = inflater.inflate(R.layout.row, parent, false);
BatteryInfo b = (BatteryInfo) getItem(position);
((TextView) v.findViewById(R.id.maintableLeft)).setText(b
.getDurationTimeInString());
((TextView) v.findViewById(R.id.maintableRight)).setText(b
.getLevel());
return v;
}
}
Modify your onCreate method to inflate the header, add it, and then bind your data to your ListView using the new CustomAdapter (I've created a fake BatteryInfo class for this example, you'll need to modify things slightly to get this working):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView lv = (ListView) this.findViewById(R.id.listView);
final Context context = this;
Button refresh = (Button) this.findViewById(R.id.button_Refresh);
refresh.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
List<BatteryInfo> x = new ArrayList<BatteryInfo>();
x.add(new BatteryInfo());
x.add(new BatteryInfo());
x.add(new BatteryInfo());
x.add(new BatteryInfo());
lv.setAdapter(new CustomAdapter(context, x));
}
});
}