I have a ListView that have in each row 2 TextView and a CheckBox.
I have a drawable (a line), that I want once the user check the CheckBox that the drawable will be scale as an animation at the background of the checked CheckBox row, I was able in my getView() method on my Custom Adapter, to animate the drawable as a background but it also scale the 2 TextView and the CheckBox, and I want that only the drawable background will be scale as an animation, how do i do that?
here is my getView() method on the custom adapter:
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.listview_item_row, parent, false);
// cache view fields into the holder
holder = new ViewHolder();
holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
holder.itemQuantity = (TextView) convertView.findViewById(R.id.itemQuantity);
holder.itemCheck = (CheckBox) convertView.findViewById(R.id.itemCheck);
convertView.setTag(holder);
final Cursor cursor = getCursor();
final View rowView = convertView;
cursor.moveToPosition(position);
holder.itemTitle.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_NAME))));
holder.itemQuantity.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY))));
holder.itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED)) == 1);
holder.itemCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(1, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), true);
Animation drawLine = AnimationUtils.loadAnimation(context, R.anim.item_done);
rowView.setBackgroundResource(R.drawable.line);
rowView.startAnimation(drawLine);
ShowListActivity.PRIORITY++;
}
else {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(0, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), false);
rowView.setBackgroundResource(0);
ShowListActivity.PRIORITY--;
}
}
});
if (holder.itemCheck.isChecked()) rowView.setBackgroundResource(R.drawable.line);
else rowView.setBackgroundResource(0);
return rowView;
}
private static class ViewHolder {
TextView itemTitle;
TextView itemQuantity;
CheckBox itemCheck;
}
update
I managed to solve the problem:
first I changed the custom row layout file like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameRowLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="10">
<LinearLayout android:id="#+id/rowLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<CheckBox android:id="#+id/itemCheck"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="2" android:gravity="center"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="false" />
<TextView android:id="#+id/itemTitle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="6"
android:gravity="center"
android:text="some text1"
android:textColor="#2B89A8"
android:textSize="22dp"
android:textStyle="bold" />
<TextView android:id="#+id/itemQuantity"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="2"
android:gravity="center"
android:text="some text2"
android:textColor="#2B89A8"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<View
android:id="#+id/backgroundView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
the whole idea of Framelayout is that views can overlap each other, there for the View after the LinearLayout is overlapping the LinearLayout.
then I changed my getView() method in my custom adapter as Following:
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.listview_item_row, parent, false);
// cache view fields into the holder
holder = new ViewHolder();
holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
holder.itemQuantity = (TextView) convertView.findViewById(R.id.itemQuantity);
holder.itemCheck = (CheckBox) convertView.findViewById(R.id.itemCheck);
holder.rowView = (View) convertView.findViewById(R.id.backgroundView);
convertView.setTag(holder);
final Cursor cursor = getCursor();
cursor.moveToPosition(position);
holder.itemTitle.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_NAME))));
holder.itemQuantity.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY))));
holder.itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED)) == 1);
holder.itemCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(1, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), true);
Animation drawLine = AnimationUtils.loadAnimation(context, R.anim.item_done);
holder.rowView.setBackgroundResource(R.drawable.line);
holder.rowView.startAnimation(drawLine);
ShowListActivity.PRIORITY++;
}
else {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(0, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), false);
holder.rowView.setBackgroundResource(0);
ShowListActivity.PRIORITY--;
}
}
});
if (holder.itemCheck.isChecked()) holder.rowView.setBackgroundResource(R.drawable.line);
else holder.rowView.setBackgroundResource(0);
return convertView;
}
private static class ViewHolder {
TextView itemTitle;
TextView itemQuantity;
CheckBox itemCheck;
View rowView;
}
hope you can understand what I did.
Related
I created a listview with a textview and an edittext inside of it.
I’ve also done an adapter with an arraylist of string as parameter.
I want to edit the elements of my arraylist with the edittext but it works only with numbers whereas I want to edit text.
When the inputtype is “text”, the “onFocusChange” doesn’t work and I cannot enter text but when the inputtype is number, it works.
Here is an extract of my code :
Adapter :
public View getView(int position, View view, ViewGroup parent) {
ViewHolder viewHolder;
if (view == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.lv_editetext_reponse, null, true);
viewHolder.textView1 = (TextView) view.findViewById(R.id.Item_name);
viewHolder.editText1 = (EditText) view.findViewById(R.id.Item_price);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.textView1.setText(reponses.get(position));
viewHolder.editText1.setId(position);
viewHolder.ref = position;
// Add listener for edit text
viewHolder.editText1
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
int itemIndex = v.getId();
String enteredPrice = ((EditText) v).getText()
.toString();
reponses.set(itemIndex, enteredPrice);
}
}
});
return view;
}
private class ViewHolder {
TextView textView1;
EditText editText1;
int ref;
}
XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/Item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="5dp"/>
<EditText
android:id="#+id/Item_price"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentRight = "true"
android:layout_gravity="right"
android:hint="Price"
android:focusable="true"
android:focusableInTouchMode="true"
android:editable="true"
android:lines="1" />
</RelativeLayout>
I have a list (ExpandableListView), and every item of the list has a ToggleButton that shows/hides the child items when toggled (the child is always 1 and it's a sort of toolbar with two buttons). Thanks to this tutorial I could set a custom button instead of the expandablelistview's indicator and I made it so that I can do something else than showing the toolbar when clicking on a list item. Also, I used the answer to this question to automatically close an open toolbar when opening another one.
So, I need to collapse the currently expanded toolbar when touching anything on the screen that is not its ToggleButton (I would need to collapse it even when clicking on one of the toolbar's buttons, as long as its own "onClick" is sent anyway).
here's an image of the app:
Here's the ExpandableListView:
<ExpandableListView
android:id="#+id/normalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="#drawable/toggle_button_selector" >
</ExpandableListView>
Here's the xml of the group item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/filesListDrawerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp" >
<ImageView
android:id="#+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:contentDescription="#string/icondescription" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="29dp"
android:layout_alignTop="#+id/img"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_alignBottom="#+id/img"
android:layout_below="#+id/text"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ToggleButton
android:id="#+id/toggle"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/text"
android:layout_marginRight="15dp"
android:background="#drawable/toggle_button_selector"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColorLink="#android:color/transparent"
android:textOff=""
android:textOn="" />
The child item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/details"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_about"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/details" />
<TextView
android:id="#+id/send"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_new_email"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/send" />
</LinearLayout>
The relevant parts of the adapter:
public class CustomList extends BaseExpandableListAdapter {
private final Activity context;
public final List<Item> names; //Item is a custom Object
private final Integer[] imageId;
private int lastExpandedGroupPosition;
public CustomList(Activity context, List<Item> names, Integer[] imageId) {
this.context = context;
this.names = names;
this.imageId = imageId;
}
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_single, null);
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.text);
TextView txtData = (TextView) convertView.findViewById(R.id.counter);
ImageView imageView = (ImageView) convertView.findViewById(R.id.img);
ToggleButton toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
final ExpandableListView list = (ExpandableListView) parent.findViewById(R.id.normalList);
txtTitle.setText(names.get(position).getName());
txtData.setText(names.get(position).getData());
if(/*some conditions*/) {
imageView.setImageResource(imageId[0]);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
list.expandGroup(position, true);
} else {
list.collapseGroup(position);
}
}
});
} else if(/*other conditions*/) {
imageView.setImageResource(imageId[2]);
toggle.setVisibility(View.GONE);
} else {
imageView.setImageResource(imageId[1]);
toggle.setVisibility(View.GONE);
}
return convertView;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child, null);
}
TextView details = (TextView) convertView.findViewById(R.id.details);
TextView send = (TextView) convertView.findViewById(R.id.send);
details.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something
}
});
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something else
}
});
return convertView;
}
#Override
public void onGroupExpanded(int groupPosition) {
ToggleButton button = (ToggleButton) ((ExpandableListView) context
.findViewById(R.id.normalList)).getChildAt(lastExpandedGroupPosition)
.findViewById(R.id.toggle);
if(groupPosition != lastExpandedGroupPosition && button.isChecked()) {
button.performClick();
}
super.onGroupExpanded(groupPosition);
lastExpandedGroupPosition = groupPosition;
}
}
And finally the important parts in the activity:
lv = (ExpandableListView)findViewById(R.id.normalList);
lv.setGroupIndicator(null);
lv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
//do some things
return true; //this tells the list that it mustn't show the child items
}
});
adapter = new CustomList(myActivity.this, list, icons);
lv.setAdapter(adapter);
P.S.: The code to hide the open toolbars doesn't work properly, but I really can't understand why. But this is another story...
--- EDIT ---
About the P.S. above: now I know why that happens.
Your list looks like ExpandableListView. Did you try to use it? It has build it second level rows, expanding, hiding, etc. Simply add your pdf rows as group headers and then add tool rows as child rows. Then you can handle row clicks to show and hide rows you would like to show/hide. See: http://developer.android.com/reference/android/widget/ExpandableListView.html
And, btw., using additional rows with hiding is a bit wrong UX design. Maybe it would be better to use drop down lists or custom dialog windows? Both of them have built in functionality do disappear when touched outside.
You can simply put all your ToggleButton whenfilling the list in an ArrayList. This way you just have to force the click on each button of list toggled when one of them is clicked.
Ok I solved the problem.
Instead of toggling the buttons with "performClick()" or "setChecked" in some methods and managing the expand/collapse action in an OnCheckedChangedListener, I decided to store the status of the togglebuttons in an array, and to expand/collapse the child views in the "getGroupView" method depending on the state of the groupview's button, whose position in the array corresponds to the groupview's position in the list. To update the list, I call notifyDataSetChanged() on the adapter.
I added a FrameLayout around the ToggleButton to increase its clickable area.
Here's the code for clarity:
The relevant parts of the MainActivity:
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
.
.
.
ExpandableListView lv = (ExpandableListView) findViewById(R.id.normalList);
CustomList adapter = new CustomList(MainActivity.this, list /*the list of Items, declared elsewhere*/, lv);
lv.setAdapter(adapter);
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.filesListDrawerLayout);
lv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
uncheckButtons(mDrawerLayout);
//do some unrelated stuff
return true;
}
});
}
public void uncheckButtons(ViewGroup vg) {
for(int i = 0; i < adapter.buttons.length; ++i)
adapter.buttons[i] = false;
adapter.lastChecked = -1;
adapter.notifyList();
}
The relevant parts of the adapter:
public class CustomList extends BaseExpandableListAdapter {
private final Activity context;
public final List<Item> names; //Item is a custom Object
public boolean[] buttons;
public int lastChecked;
private final Integer[] imageId = new Integer[] { R.drawable.image1, R.drawable.image1, R.drawable.image3 };
private ExpandableListView list;
public CustomList(Activity context, List<Item> names, boolean forUpload, ExpandableListView list) {
this.context = context;
this.names = names;
this.list = list;
this.buttons = new boolean[names.size()];
this.lastChecked = -1;
}
private static class GroupHolder {
TextView txtTitle;
TextView txtData;
ImageView imageView;
ToggleButton toggle;
FrameLayout frame;
}
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_single, null);
holder.txtTitle = (TextView) convertView.findViewById(R.id.text);
holder.txtData = (TextView) convertView.findViewById(R.id.counter);
holder.imageView = (ImageView) convertView.findViewById(R.id.img);
holder.toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
holder.frame = (FrameLayout) convertView.findViewById(R.id.frame);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
txtTitle.setText(names.get(position).getName());
txtData.setText(names.get(position).getData());
if(/*some conditions*/) {
imageView.setImageResource(imageId[0]);
holder.frame.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(lastChecked != -1) {//if there's a checked button
buttons[lastChecked] = !buttons[lastChecked];//uncheck it
if(position == lastChecked)//if I clicked on the last checked button
lastChecked = -1;//there's no checked button
else {
buttons[position] = !buttons[position];//check the button
lastChecked = position;//and set it as the last checked one
}
notifyList();
}
return false;
}
});
holder.toggle.setChecked(buttons[position]);
if(holder.toggle.isChecked()) {
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2)
list.expandGroup(position);
else
list.expandGroup(position, true);
} else {
list.collapseGroup(position);
}
} else if(/*other conditions*/) {
//do other stuff with the views that don't interest us
}
return convertView;
}
private static class ChildHolder {
TextView details;
TextView send;
TextView other;
}
public View getChildView(final int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child, null);
holder = new ChildHolder();
holder.details = (TextView) convertView.findViewById(R.id.details);
holder.send = (TextView) convertView.findViewById(R.id.send);
holder.other = (TextView) convertView.findViewById(R.id.other);
} else {
holder = (ChildHolder) convertView.getTag();
}
//maybe I could manage the listeners more nicely and without redundance...
holder.details.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
holder.send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
holder.other.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
return convertView;
}
public void notifyList() {
this.notifyDataSetChanged();
}
//I don't need OnGroupExpanded anymore
The ExpandableListView:
<ExpandableListView
android:id="#+id/normalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:groupIndicator="#drawable/toggle_button_selector" >
</ExpandableListView>
The xml of the group item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/filesListDrawerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp" >
<ImageView
android:id="#+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:contentDescription="#string/icondescription" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="29dp"
android:layout_alignTop="#+id/img"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black" />
<TextView
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_alignBottom="#+id/img"
android:layout_below="#+id/text"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/text"
android:layout_marginTop="-15dp"
android:clickable="true"
android:paddingTop="10dp"
android:paddingRight="15dp"
android:paddingLeft="10dp" >
<ToggleButton
android:id="#+id/toggle"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="#drawable/toggle_button_selector"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColorLink="#android:color/transparent"
android:textOff=""
android:textOn="" />
</FrameLayout>
</RelativeLayout>
The xml of the child item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/details"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_about"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/details" />
<TextView
android:id="#+id/send"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_new_email"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/send" />
<!-- this one is new -->
<TextView
android:id="#+id/other"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:background="#drawable/toolbar_selector"
android:clickable="true"
android:drawablePadding="-12dp"
android:drawableTop="#drawable/ic_action_other"
android:gravity="center"
android:text="#string/other" />
</LinearLayout>
There. I'm happy now.
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.
I have implemented a ListView, each element of which contains a checkbox and a button. I have written an adapter for the view as follows:
public class myadapter extends SimpleAdapter
{
LayoutInflater mLayoutInflater;
public myadapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
{
super(context, data, resource, from, to);
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = null;
Log.i("xx","getView called: position="+position);
if (convertView != null)
{
view = convertView;
}
else
{
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
}
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox);
buttonEdit.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Log.i("xx","Button pressed! position ="+position);
}
});
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
Log.i("xx","Checkbox changed! position ="+position);
}
});
return super.getView(position, convertView, parent);
}
}
At runtime, if I click on a button in the top row of my list I always see "Button pressed! position = 0" in the log output. Similarly, if I click on the checkbox I see "Checkbox changed! position=0". The problem comes if I click on a button on any other element in the list. Sometimes I see the correct log message, but other times I see nothing at all. Any ideas?
EDIT: here's my custom_row_view.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="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="142dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#FFFF00"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/text2"
android:textSize="12sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/text3"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />
<Button
android:id="#+id/editbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Edit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
do like this
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = convertView;
Log.i("xx","getView called: position="+position);
if (view == null)
{
view = convertView;
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
}
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox);
return view;
}
you are returning the superclass value, with convertview as a parameter.
return super.getView(position, convertView, parent);
but you are doing this:
if (convertView != null)
{
view = convertView;
}
else
{
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
}
If convertView is null, you are inflating a new view, but you are not returning it.
my suggestion is
return view;
instead of
return super.getView(position, convertView, parent);
That should be enough
do this:
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox);
buttonEdit.setTag(String.valueOf(positin));
cb.setTag(String.valueOf(positin));
buttonEdit.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Log.i("xx","Button pressed! position ="+arg0.getTag());
}
});
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
Log.i("xx","Checkbox changed! position ="+arg0.getTag(););
}
});
you are using OnClickListener() inner as a inner class. for inner class the value of position remains zero. until you setTag() on button and then getTag from button.
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.