Button under a couple of Textviews - android

I'm doing something quite similar to the tutorial http://www.vogella.com/articles/AndroidListView/article.html#listadvanced_interactive.
I now want to put a button at the bottom. Then click the button and do something with the selected Values.
I understand, that I need some new layout, because adding the button to the existing one would just add a button to every textview. But I cant figure out how to do it.
Some help would be very nice. Thanks in advance. :)
Edit: That's my layout at the moment.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="30px" >
</TextView>
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px" >
</CheckBox>
</RelativeLayout>
This creates something that looks like this:
http://i.stack.imgur.com/cN8Dl.png
My adapter looks like this:
public class AttributeAdapter extends ArrayAdapter<Attribute> {
private final List<Attribute> list;
private final Activity context;
public AttributeAdapter(Activity context, List<Attribute> list) {
super(context, R.layout.activity_select_attributes, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.activity_select_attributes, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Attribute element = (Attribute) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName() + " " + list.get(position).getType());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
Now i want a button at the bottom to continue with the selected values.

Create a new layout or xml file named activity_base and add following:
<RelativeLayout 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"
tools:context=".BaseActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
>
</ListView>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
in your ListActivity in the Oncreate add setContentView(R.layout.activity_base);
This should do the job.

Related

Text disappears in listview in android 2.3.6

I'm using an inflated ListView in my project. The layout file is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background2" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:smoothScrollbar="true" >
</ListView>
</RelativeLayout>
And the inflated layout file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="70dp"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/contact" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
The above code works fine when I'm using API level 11 or above, but below API level 11, it behaves in an awkward fashion, like the textView1 becomes totally invisible while textView2 is still visible. Also, while scrolling, all the items are VISIBLE but when the scrolling is finished, it again turns invisible. I'm using Actionbarsherlock. And yeah, one last thing, I'm using SearchView in actionbar, the bottom part of which strangely appears like a blue highlighted line in API 8 while everything works perfect in higher android levels
EDIT
This is my code snippet of ListView adapter
private class ContactViewHolder {
CheckBox checkBox;
TextView textView;
TextView textView2;
ImageView iv;
}
private class ContactArrayAdapter extends ArrayAdapter<Contact> {
//private LayoutInflater inflater;
List<Contact> myList;
Context mContext;
//Cursor cursor;
public ContactArrayAdapter(Context context, List<Contact> planetList) {
super(context, R.layout.simplerow, R.id.textView1, planetList);
// Cache the LayoutInflate to avoid asking for a new one each time.
//inflater = LayoutInflater.from(context);
mContext = context;
myList = planetList;
}
#Override
public int getCount() {
if(myList != null)
return myList.size();
return 0;
}
#Override
public Contact getItem(int position) {
if(myList != null)
return myList.get(position);
return null;
}
#Override
public long getItemId(int position) {
if(myList != null)
return myList.get(position).hashCode();
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Contact to display
ContactViewHolder holder;
//If the listview does not have an xml layout ready set the layout
if (convertView == null){
//we need a new holder to hold the structure of the cell
holder = new ContactViewHolder();
//get the XML inflation service
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate our xml cell to the convertView
convertView = inflater.inflate(R.layout.simplerow, null);
//Get xml components into our holder class
holder.textView = (TextView) convertView.findViewById(R.id.textView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Contact contact = (Contact) cb.getTag();
contact.setChecked(cb.isChecked());
}
});
//Attach our holder class to this particular cell
convertView.setTag(holder);
}else{
//The listview cell is not empty and contains already components loaded, get the tagged holder
holder = (ContactViewHolder) convertView.getTag();
}
//Fill our cell with data
//get our person object from the list we passed to the adapter
Contact contact = getItem(position);
//Fill our view components with data
holder.textView.setText(name1.get(position));
holder.textView2.setText(num1.get(position));
holder.checkBox.setChecked(contact.checked);
return convertView;
}
}
The Contact class is
private static class Contact {
private String name = "";
private boolean checked = false;
public Contact() {
}
public Contact(String name) {
this.name = name;
}
public Contact(String name, boolean checked) {
this.name = name;
this.checked = checked;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String toString() {
return name;
}
public void toggleChecked() {
checked = !checked;
}
}
Here is how to write a good adapater for your listview
public class MyAdapter extends ArrayAdapter<Person> {
Context context;
List<Person>myList;
public MyAdapter(Context context, int resource, List<Person> objects) {
super(context, resource, objects);
this.context = context;
this.myList = objects;
}
#Override
public int getCount() {
if(myList != null)
return myList.size();
return 0;
}
#Override
public Person getItem(int position) {
if(myList != null)
return myList.get(position);
return null;
}
#Override
public long getItemId(int position) {
if(myList != null)
return myList.get(position).hashCode();
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
//If the listview does not have an xml layout ready set the layout
if (convertView == null){
//we need a new holder to hold the structure of the cell
holder = new Holder();
//get the XML inflation service
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate our xml cell to the convertView
convertView = inflater.inflate(R.layout.person_cell, null);
//Get xml components into our holder class
holder.txtName = (TextView)convertView.findViewById(R.id.person_cell_txtName);
holder.txtSurname = (TextView)convertView.findViewById(R.id.person_cell_txtSurname);
holder.imageView = (ImageView)convertView.findViewById(R.id.person_cell_imageview);
//Attach our holder class to this particular cell
convertView.setTag(holder);
}else{
//The listview cell is not empty and contains already components loaded, get the tagged holder
holder = (Holder)convertView.getTag();
}
//Fill our cell with data
//get our person object from the list we passed to the adapter
Person person = getItem(position);
//Fill our view components with data
holder.txtName.setText(person.getName());
holder.txtSurname.setText(person.getSurname());
Picasso.with(context).load(person.getImageUrl()).fit().into(holder.imageView);
return convertView;
}
/**
* This holder must replicate the components in the person_cell.xml
* We have a textview for the name and the surname and an imageview for the picture
*/
private class Holder{
TextView txtName;
TextView txtSurname;
ImageView imageView;
}
}
Notice that for Holder class you only set the properties reflecting the inflated xml. You dont need any constructor or getter/setters
In the get view you have to attach the holder class to the convertView tag property (convertView.setTag(holder))
retrieve the tagged holder if the convertView is not null
try to follow this pattern for the adapter
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/contact" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="fill_vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" />
</LinearLayout>
</LinearLayout>

Check box selection and list item click on the same listview in android

I have implemented a custom listview with checkboxes in the single choice mode of the android listview.I want a functionality such that when I click on the checkbox the row corresponding to that check box must get added(or rather say the "Person" object shown in that row should get added to another list and be removed from that list) and on itemclick on that listview should take me to another screen in android.I tried the other ways but they specify that the checkbox needs to be focasable:false.Also I want the click listener to work on the check box only.Please any suggestions or help on these.Thanks in advance
This id the xml code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="#+id/projects_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:focusable="false" />
<TextView
android:id="#+id/project_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/projects_check"
android:layout_alignBottom="#+id/projects_check"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/projects_check"
android:layout_toRightOf="#+id/projects_check"
android:text="TextView" />
</RelativeLayout>
And this is my onitem click listener code..
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
Projects info = (Projects) parent.getItemAtPosition(position);
info.toggleChecked();
ProjectsHolder viewHolder = (ProjectsHolder) view.getTag();
viewHolder.getmChoiceSelect().setChecked(info.isChecked());
if (viewHolder.getmChoiceSelect().isChecked()) {
completed_projects.add(info);
mCompProjAdapter.notifyDataSetChanged();
projects_list.remove(info);
mProjAdapter.notifyDataSetChanged();
}
}
To manage click onto the row itself, you can just write listener for listView itself.
This is how i manage checkbox click.
class CustomAdapter extends ArrayAdapter<BeanClass> {
private ArrayList<BeanClass> items;
public FieldPlannedAdapter(Context context,
ArrayList<BeanClass> items) {
super(context, R.layout.custom_list_row, items);
this.items = items;
}
public class ViewHolder {
protected CheckBox checkbox;
protected TextView name;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_list_row,
null);
viewHolder = new ViewHolder();
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.checkIsMissed);
viewHolder.name = (TextView) convertView
.findViewById(R.id.txtViewMTPname);
convertView.setTag(viewHolder);
viewHolder.checkbox
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CheckBox cb = (CheckBox) v;
final BeanClass item = (BeanClass) cb
.getTag();
item.setSelected(cb.isChecked());
if (cb.isChecked()) {
// DO SOMETHING
} else {
}
}
});
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
BeanClass item = items.get(position);
viewHolder.name.setTag(item);
viewHolder.checkbox.setTag(item);
viewHolder.name.setText(item.getName());
viewHolder.checkbox.setChecked(item.isSelected());
if (viewHolder.checkbox.isChecked()) {
// DO SOMETHING
} else {
// DO SOMETHING
}
return convertView;
}
}
Custom row example :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<LinearLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/card_ui"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="#+id/txtViewMTPname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Customer Name"
android:textColor="#drawable/text_selector"
android:textSize="14sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtViewMtpDarCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="dgdgdgdg"
android:textColor="#drawable/text_selector"
android:textSize="16sp"
android:textStyle="italic" />
<CheckBox
android:id="#+id/checkIsMissed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="Missed" />
</LinearLayout>
</LinearLayout>
</FrameLayout>

listview onItemClick not getting called while BaseAdapter

its been a hour im trying to figure out the issue. Im implementing my own Adapter,The onClick event of the listview is not getting called
THe custom layout xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px" >
</CheckBox>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/imageView1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="CodeLearn Chapter 1"
android:textSize="16sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/textView1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Description" />
</RelativeLayout>
The main layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tvUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
/>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Custom Adapter code
public class AssignmentAdapter extends BaseAdapter
{
public View getView(int rowNumber, View convertView, ViewGroup parent)
{
View view = null;
ViewHolder viewHolder;
final int row = rowNumber;
if (convertView == null)
{
LayoutInflater minflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// convertView = minflater.inflate(R.layout.listitem, parent,
// false);
view = minflater.inflate(R.layout.listitem, null);
viewHolder = new ViewHolder();
viewHolder.courseTitle = (TextView) view.findViewById(R.id.textView1);
viewHolder.assignmentTitle = (TextView) view.findViewById(R.id.textView2);
viewHolder.chkBox = (CheckBox) view.findViewById(R.id.check);
// chapterDesc.setText(myAssign.assignmentTitle);
viewHolder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// Model element = (Model) viewHolder.checkbox
// .getTag();
// element.setSelected(buttonView.isChecked());
if (isChecked)
{
Log.d(tag, "i got checked"+ row);
}
}
});
view.setTag(viewHolder);
} else
{
view = convertView;
viewHolder = (ViewHolder) convertView.getTag();
}
ViewHolder holder = (ViewHolder) view.getTag();
Assignment myAssign = listAssignment.get(rowNumber);
holder.courseTitle.setText(myAssign.courseTitle);
holder.assignmentTitle.setText(myAssign.assignmentTitle);
return view;
}
The MainActivity
public class MainActivity extends Activity
{
AssignmentAdapter assignmentAdaper;
Context context = MainActivity.this;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page);
assignmentAdaper = new AssignmentAdapter(this);
ListView assignmentLists = (ListView) findViewById(R.id.listView1);
assignmentLists.setAdapter(assignmentAdaper);
Assignment myAssignment = new Assignment();
assignmentLists.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Log.d("MainActivity", "wewew");
}
});
Since, I have checkbox in the listview layout. I need to disable the focus attribute of the checkbox
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>

android grid view display caption below each grid

I want to display some text below each grid image.
Please see my code below.
Please help me in finding why its not working
I am getting the images properly displayed but no text
if i try to display text within image its coming
so it should be something with the layout defined. but i am not able to debug
adaptor code :
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.griditems, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.grid_item_text);
Log.i(TAG, holder.text1.toString());
holder.image = (ImageView) convertView
.findViewById(R.id.grid_item_image);
// if it's not recycled, initialize some attributes
holder.image.setImageResource(gridItemIds[position]);
holder.text1.setText(gridTitles[position]);
//holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
// holder.image.setPadding(20, 20, 20, 20);
convertView.setLayoutParams(new GridView.LayoutParams(Utils
.getLayoutParameter(), Utils.getLayoutParameter()));
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView text1;
ImageView image;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<ImageView android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#000000"
>
</TextView>
</LinearLayout>
gridview :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/text1view" android:textStyle="bold"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center_horizontal" android:background="#drawable/title_bar"
/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:verticalSpacing="50dip"
android:horizontalSpacing="10dp" android:numColumns="3"
android:stretchMode="columnWidth"
android:layout_below="#+id/text1view"
android:gravity="center"
android:layout_centerHorizontal="true"
android:background="#drawable/home_bg"
/>
</LinearLayout>
I used your xml but with a simple array adapter that I wrote and everything work perfectly, try to replace your adapter with mine and let me know, thanks.
class ItemsAdapter extends ArrayAdapter<String> {
public ItemsAdapter(Context context, List<String> list) {
super(context, R.layout.griditems, list);
}
#Override
public View getView(final int position, View row, final ViewGroup parent) {
final String item = getItem(position);
ItemWrapper wrapper = null;
if (row == null) {
row = getLayoutInflater().inflate(R.layout.griditems, parent, false);
wrapper = new ItemWrapper(row);
row.setTag(wrapper);
} else {
wrapper = (ItemWrapper) row.getTag();
}
wrapper.refreshData(item);
return row;
}
}
static class ItemWrapper {
TextView text;
ImageView img;
public ItemWrapper(View row) {
text = (TextView) row.findViewById(R.id.grid_item_text);
img = (ImageView) row.findViewById(R.id.grid_item_image);
}
public void refreshData(String item) {
img.setImageResource(R.drawable.image_1);
text.setText(item);
}
}
Also try using RelativeLayout too, placing the TextView underneath the ImageView by using
android:layout_below="#id/grid_item_image"
It'll place it underneath the image, however, if you have the option to use the Graphical Layout, dragging it where you want it in conjunction with the image will set all the parameters for you instead of typing them out.

RelativeLayout Buttons on top and custom listview below buttons

I've searched but not found any examples for what I want to do. I'm sure its trivial but I'm stumped. I have a custom listview adapter and a defined relative layout. The entire layout is being called for each item, I want my buttons to only appear once on top. Any advice would be greatly appreciated.
This is how it looks now:
http://img440.imageshack.us/img440/2803/device20120104173817.png
This is how I want it to look:
http://img215.imageshack.us/img215/805/devicewant.png
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/relativeLayout2">
<TextView
android:id="#+id/postinfo"
android:layout_below="#+id/FirstPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<TextView
android:id="#+id/dateinfo"
android:layout_alignRight="#+id/postinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<TextView
android:id="#+id/post"
android:layout_below="#+id/postinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<ImageView
android:id="#+id/icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/post"
android:contentDescription="OCForums thread icon"
android:focusable="false"
/>
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top" >
<Button
android:id="#+id/LastPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/last" />
<Button
android:id="#+id/NextPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/LastPage"
android:text="#string/next" />
<Button
android:id="#+id/FirstPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/first" />
<Button
android:id="#+id/PreviousPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/FirstPage"
android:text="#string/previous" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
static class MyCustomAdapter2 extends BaseAdapter {
private LayoutInflater mInflater;
public MyCustomAdapter2(Context context, int textViewResourceId,List<List<String>> objects) {
super();
mInflater = LayoutInflater.from(context);
rid = textViewResourceId;
fcontext = context;
usernames = objects.get(0);
usernamecolors = objects.get(1);
dates = objects.get(2);
messages = objects.get(3);
Log.i("is it empy?(messages)",Integer.toString(messages.size()));
Log.i("is it empy?(dates)",Integer.toString(dates.size()));
Log.i("is it empy?(usernamecolors)",Integer.toString(usernamecolors.size()));
Log.i("is it empy?(usernames)",Integer.toString(usernames.size()));
}
public int getCount() {
return lists.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(rid, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.postinfo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(usernames.get(position));
holder.text.setTextColor(Color.parseColor(usernamecolors.get(position)));
ViewHolder holderd;
if (convertView == null) {
convertView = mInflater.inflate(rid, null);
holderd = new ViewHolder();
holderd.text = (TextView) convertView.findViewById(R.id.dateinfo);
convertView.setTag(holderd);
} else {
holderd = (ViewHolder) convertView.getTag();
}
holderd.text.setText(dates.get(position));
ViewHolder holderm;
if (convertView == null) {
convertView = mInflater.inflate(rid, null);
holderm = new ViewHolder();
holderm.text = (TextView) convertView.findViewById(R.id.post);
convertView.setTag(holderm);
} else {
holderm = (ViewHolder) convertView.getTag();
}
holderm.text.setText(messages.get(position));
ImageView icon=(ImageView)convertView.findViewById(R.id.icon2);
if (newlist.size() > 0 && lists.get(position).matches(newlist.get(position))){
icon.setImageResource(R.drawable.forum_new);
}
else{
icon.setImageResource(R.drawable.forum_old);
}
return convertView;
}
static class ViewHolder {
TextView text;
}
}
This is where I am calling it all.
ListView listview = (ListView) findViewById(R.id.listView3);
listview.setAdapter(new CustomListView.MyCustomAdapter2(ThreadActivity.this, R.layout.row2, output));
You need to define the header as a separate layout (separate xml file) then instantiate it and add it to the listview as a header -- then this layout will show as the first item in the list.
See this question: Android ListView with complex header

Categories

Resources