I have created a list of 7 days and want the list to take the full screen. But the list is having a gap in the bottom. I want to remove the gap.
My code is as follows :
package com.example.collegehack;
import android.app.ListActivity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Days extends ListActivity {
String days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Days.this,
android.R.layout.simple_list_item_1,days));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String temporary = days[position];
try {
Class ourClass = Class.forName("com.example.collegehack"
+ temporary);
Intent ourIntent = new Intent(Days.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Please help me with this problem. Thanks in advance.
To do so you need to inflate your own view.
Follow these steps:
1. Create my_layout.xml
contained textView
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
2. In your adapter, you need to override the getView() method and set the rows height like this:
setListAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,days) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.my_layout, parent, false);
convertView.getLayoutParams().height = parent.getHeight() / getCount();
convertView.requestLayout();
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
tv.setText(getItem(position));
}
return convertView;
}
});
3. Disable the list scrolling because the dividers height doesn't count in this workaround and your listView will scroll by a millimeter:
getListView().setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true; // Indicates that this has been handled by you
// and will not be forwarded further.
}
return false;
}
});
That's it! Have fun :)