Hi all I have custom grid view with imageButton and textView as follows
Here i have used imageButton.. The problem is the grid is not clickable.. But if i use here imageView it works fine but UI is not appropriate.
Here is my layout
<?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"
android:gravity="center"
android:padding="5dp"
>
<ImageButton
android:id="#+id/profilePic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/newlocation"
android:background="#drawable/btnbackground"
/>
<TextView
android:id="#+id/SpeakerName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="15sp"
android:text="Some Speaker"
/>
Here is my adapter
package com.tt.cc;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class SavedConferencesGridAdapter extends ArrayAdapter<String>{
Context mContext;
List<String> confernceList;
public SavedConferencesGridAdapter(Context context, int textViewResourceId, List<String> confernceList)
{
super(context,textViewResourceId,confernceList);
this.confernceList = confernceList;
this.mContext = context;
Log.e("TAG", confernceList.size()+"");
}
int[] picIds = new int[] { R.drawable.newschedule,R.drawable.newexhibitors,
R.drawable.newsponsers, R.drawable.newlocation,
R.drawable.newfavourites, R.drawable.newreminder,R.drawable.info,R.drawable.info,R.drawable.info,R.drawable.info,R.drawable.info,R.drawable.info };
#Override
public int getCount() {
// TODO Auto-generated method stub
return confernceList.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if(convertView==null){
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.entity, null);
TextView tv = (TextView)v.findViewById(R.id.SpeakerName);
tv.setText("tt");
/*ImageView iv = (ImageView) v.findViewById(R.id.profilePic);
iv.setImageResource(picIds[position]);*/
}
else
{
v = convertView;
}
return v;
}
}
and here is my activity
public class SavedConferencesActivity extends Activity{
GridView confereneceGridView;
ArrayAdapter<String> conferneceAdapter;
Button btnUpdate;
List<String> conferenceList;
TextView txtHeading;
Boolean result = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected void onStart() {
super.onStart();
setContentView(R.layout.homegridlayout);
initialize();
confereneceGridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(SavedConferencesActivity.this, conferenceList.get(position)+"", Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
conferneceAdapter = new SavedConferencesGridAdapter(this, android.R.layout.simple_list_item_1, conferenceList);
confereneceGridView.setAdapter(conferneceAdapter);
}
private void initialize() {
confereneceGridView = (GridView) findViewById(R.id.gridOfConference);
btnUpdate = (Button) findViewById(R.id.btnUpdate);
txtHeading = (TextView) findViewById(R.id.txtTag);
conferenceList = new ArrayList<String>();
conferenceList.add("AA");
conferenceList.add("BB");
conferenceList.add("CC");
if (conferenceList.size() == 0)
txtHeading.setText("No conferences saved");
else
txtHeading.setText("Selected Conferences");
}
}
Please help me in solving this problem. Or if any alternative available..
Simple. Provide this attribute for the ImageButton ,
android:focusable="false"
This problem usually occurs when there is a focussable element in the Grid.(Eg:Buttons, ImageButton etc).
Reason of this ImageButton is by Default is clickable and focusable, so Focus and click event never goes to ItemView.
Set Properties
android:focusable-"false"
and
android:clickable="false"
Your item's Layout should be:
<LinearLayout ...
android:descendantFocusability="blocksDescendants">
<ImageButton ...
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
Don't forget setOnClickListener for ImageButton in Adapter.
in your adapter class,
before you return the view "v", add the "onClickListener" to "v"
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, position, Toast.LENGTH_SHORT).show();
}
});
Related
this is the image of layout tile.xml
i have created a list using custom adapter it has 2 textviews and 2 buttons inside it now what i want is to change the visiblity of one of textview on button click.
I am handling the button clicks outside the custom adapter.I want to toggle visiblity toggle for second textview with id tvstatus using on and off buttons.
this is code for customadapter
package slide.apptech.com.rpiconnect;
/**
* Created by MOHIT on 09-06-2016.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
//custom adapter class extends a arrayadapter
public class customadapter2 extends ArrayAdapter<String> {
private final Context context;
private final ArrayList values;
private String stv = "Ststus";
public customadapter2(Context context, ArrayList values) {
//for super constructor pass
// context files
//layout file required for list
//arraylist that has strings to be displayed
super(context, R.layout.tile, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
final ViewGroup par = parent;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.tile, parent, false);
final TextView textView = (TextView) rowView.findViewById(R.id.tvappname);
Button on = (Button) rowView.findViewById(R.id.bon);
Button off = (Button) rowView.findViewById(R.id.boff);
final TextView status = (TextView) rowView.findViewById(R.id.tvstatus);
on.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
status.setVisibility(View.VISIBLE);
notifyDataSetChanged();
((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
}
});
off.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) ;
((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
}
});
//get(position method is used to access the elements of arraylist)
String val = (String) values.get(position);
textView.setText(val);
// Change the icon for Windows and iPhone
String s = (String) values.get(position);
return rowView;
}
public void myClickHandler(View v)
{
}
}
this is code for my xml file that i am using as listelement
<?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">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_margin="2dp"
android:layout_height="100dp"
card_view:cardCornerRadius="4dp"
android:layout_alignParentStart="true"
android:background="#611818"
android:paddingBottom="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OFF"
android:id="#+id/boff"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginStart="37dp"
android:focusable="false"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bon"
android:text="ON"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="28dp"
android:focusable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ttile"
android:id="#+id/tvappname"
android:textSize="20sp"
android:layout_above="#+id/boff"
android:layout_centerHorizontal="true"
android:textColor="#color/abc_input_method_navigation_guard" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ON"
android:id="#+id/tvstatus"
android:layout_below="#+id/tvappname"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="#060505"
android:visibility="invisible"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
please comment if you want any other file
No, don't do that. Changing the value directly of any view inside listview is a bad idea. Just make changes in your arraylist and then call notifyDataSetChanged() to reflect changes.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final int pos = position;
final ViewGroup par = parent;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.tile, parent, false);
final TextView textView = (TextView) rowView.findViewById(R.id.tvappname);
Button on = (Button) rowView.findViewById(R.id.bon);
Button off = (Button) rowView.findViewById(R.id.boff);
final TextView status = (TextView) rowView.findViewById(R.id.tvstatus);
//get(position method is used to access the elements of arraylist)
String val = (String) values.get(position);
textView.setText(val);
if(val.equalsIgnoreCase("off")){
status.setVisibility(View.INVISIBLE);
}else{
status.setVisibility(View.VISIBLE);
}
on.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
values.set(position, "On");
notifyDataSetChanged();
((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
}
});
off.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
values.set(position, "Off");
notifyDataSetChanged();
((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
}
});
// Change the icon for Windows and iPhone
String s = (String) values.get(position);
return rowView;
}
Hope it will help :)
I have a Spinner that uses a custom adapter where the getDropDownView() is overridden. Each item in the custom drop-down view is made up of a TextView and a Button.
But, when I run my code, the spinner drop-down items display fine, but clicking them does nothing. The spinner drop-down remains open and spinner.onItemSelected() is not triggered.
drop_down_item.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/dropdown_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:singleLine="true" />
<Button
android:id="#+id/dropdown_button"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:text="Remove"/>
</RelativeLayout>
Custom adapter code
public View getDropDownView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.drop_down_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.dropdown_text);
textView.setText(mValues.get(position));
Button buttonView = (Button) rowView.findViewById(R.id.dropdown_button));
return rowView;
}
I create my spinner and adapter with this code:
spinner = (Spinner) findViewById(R.id.my_spinner);
MyAdapter adapter = new MyAdapter(getViewContext(), R.layout.spinner_item, values);
adapter.setDropDownViewResource(R.layout.drop_down_item);
spinner.setAdapter(adapter);
...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// Do something here - but this never runs
}
});
So I don't know why onItemSelected() is no longer called?
I was wondering if I need to put a click listener on the drop-down TextView which should in turn trigger onItemSelected() using maybe spinner.setSelection(pos)?
The Events is basically a interface that Activity implements to recieve the callBack by clicking on the LinearLayout of the DropDown View of Spinner.
public class MyArrayAdapter extends BaseAdapter {
String[] values;
int CustomResource;
Context context;
Events events;
public MyArrayAdapter(Context baseContext, int customspinnerview,
String[] stringArray, Events events) {
values = stringArray;
context = baseContext;
this.events = events;
CustomResource = customspinnerview;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return values.length;
}
#Override
public Object getItem(int position) {
if (position < values.length)
return values[position];
else {
return null;
}
}
#Override
public View getView(final int position, final View convertView,
ViewGroup parent) {
View rowView = convertView;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (rowView == null) {
rowView = inflater.inflate(CustomResource, parent, false);
}
TextView textView = (TextView) rowView.findViewById(R.id.dropdown_text);
textView.setText(values[position]);
Button button = (Button) rowView.findViewById(R.id.Button_text);
return rowView;
}
#Override
public View getDropDownView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = convertView;`enter code here`
if (rowView == null) {
rowView = inflater.inflate(CustomResource, parent, false);
}
final LinearLayout parentRelative = (LinearLayout) rowView
.findViewById(R.id.parent);
final TextView textView = (TextView) rowView
.findViewById(R.id.dropdown_text);
textView.setText(values[position]);
Button button = (Button) rowView.findViewById(R.id.Button_text);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
events.onItemSelectedLister(
(AdapterView<?>) parentRelative.getParent(),
parentRelative, position, (long) 0);
}
});
// Button buttonView = (Button)
// rowView.findViewById(R.id.dropdown_button);
return rowView;
}
Events Inteface Its a interface that the Activity implement in order to recieve the callbacks from the Adapter.
import android.view.View;
import android.widget.AdapterView;
public interface Events {
public void onItemSelectedLister(AdapterView<?> parent, View view,
int position, long id);
}
Activity Implementation.
onItemSelected Implementation is the place where you can do your task.....
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import com.example.adapter.MyArrayAdapter;
public class MainActivity extends Activity implements Events {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(new MyArrayAdapter(getBaseContext(),
R.layout.customspinnerview, getResources().getStringArray(
R.array.values), this));
}
#Override
public void onItemSelectedLister(AdapterView<?> parent, View view,
final int position, long id) {
//perform your Task.......
Method method;
try {
method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
method.setAccessible(true);
try {
method.invoke(spinner);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
spinner.post(new Runnable() {
#Override
public void run() {
spinner.setSelection(position);
spinner.setSelected(true);
((MyArrayAdapter) spinner.getAdapter()).notifyDataSetChanged();
}
});
}
}
Activity xml File for setContentView
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.customspinner.MainActivity" >
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</Spinner>
</RelativeLayout>
Spinner View which is passed to Adapter as layout file.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#99000000"
android:id="#+id/parent"
android:orientation="horizontal">
<TextView
android:id="#+id/dropdown_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/Button_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="remove"/>
</LinearLayout>
the Code works perfectly fine :) .I have the code perfectly running.
Solution is to set android:focusable="false" in the layout for both the TextView and the Button.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/dropdown_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:singleLine="true" />
<Button
android:id="#+id/dropdown_button"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:text="Remove"/>
</RelativeLayout>
Alternatively can also do this in the code:
textView.setFocusable(false);
buttonView.setFocusable(false);
Found the answer here. This works because the Spinner implementation only allows one focusable item in the view. That's why I wasn't able to select the item.
Android Code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView;
rootView = inflater.inflate(R.layout.view_contact_list, container, false);
listview = (ListView) rootView.findViewById(R.id.listView1);
Log.d("Contact List", "Before read method");
ReadContacts();
Log.d("Contact List", "After Read Method");
Log.d("Before ", "Before Adapter Initialization");
adapter = new ContactListAdapter(getActivity(), contact_list);
Log.d("After",
"After Adapter Initializaiton and before listview set adapter");
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
TextView name = (TextView) arg1
.findViewById(R.id.tvContactName);
TextView phone = (TextView) arg1
.findViewById(R.id.tvContactPhone);
Toast.makeText(
getActivity(),
"Name : " + name.getText().toString() + "Phone : "
+ phone.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
The adapter is working properly and i can see the data in a listview but i cannot click on the list items. There is no response
i have set the android:clickable = "true"
The fragment is a part of a view Pager
I cannot use list fragments and want to use an adapter .. what is the error ?
I have also pasted this setonitemclick method in onActivityCreated() .. it didnt work
XML of List View :
<?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"
android:clickable="true"
android:focusable="false"
>
</ListView>
</LinearLayout>
The Adapter I am using:
package com.example.eh;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class ContactListAdapter extends BaseAdapter
{
Context context;
List<Tuple> contact_list=new ArrayList<Tuple>();
public ContactListAdapter(Context ctx, List<Tuple> data)
{
context = ctx;
contact_list = data;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
//return contacts.size();
return contact_list.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return contact_list.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public String getName(int arg0)
{
return contact_list.get(arg0).getContactName();
}
public String getPhone(int arg0)
{
return contact_list.get(arg0).getContactPhone();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
if (convertView == null) {
view = ((Activity) context).getLayoutInflater().inflate(R.layout.row_full_contact_list, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.name.setText(getName(position));
holder.phone.setText(getPhone(position));
return view;
}
private class ViewHolder {
public TextView name;
public TextView phone;
public Button invite;
public ViewHolder(View v)
{
name = (TextView) v.findViewById(R.id.tvContactName);
phone = (TextView) v.findViewById(R.id.tvContactPhone);
invite = (Button) v.findViewById(R.id.bInviteFriends);
}
}
}
The XML of the row item:
<?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="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/LeftSide"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvContactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="30dp"
android:maxLines="1"
/>
<TextView
android:id="#+id/tvContactPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvContactName"
android:text="TextView" />
</RelativeLayout>
<Button
android:id="#+id/bInviteFriends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Invite" />
</RelativeLayout>
Have you put a breakpoint inside the onItemClick method to make sure its not fired?
Try to add this line of code on your listview's XML :
android:focusable="false"
try this
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getActivity(),position.toString+" is the position you clicked!!",Toast.LENGTH_SHORT).show();
TextView name = (TextView) arg1
.findViewById(R.id.tvContactName);
TextView phone = (TextView) arg1
.findViewById(R.id.tvContactPhone);
Toast.makeText(
getActivity(),
"Name : " + name.getText().toString() + "Phone : "
+ phone.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
I've created a ListView that has an EditText in each row. The ListView also allows the user to delete an item in the list by swiping horizontally using SwipeDismissListViewTouchListener.java from https://github.com/romannurik/Android-SwipeToDismiss.
Both functionality work well, except when the user tries to do the swiping gesture with the initial ACTION_DOWN event occurring over the EditText. It looks like the EditText is consuming the ACTION_DOWN event before it can be passed to the SwipeDismissListViewTouchListener, which needs the ACTION_DOWN event to initialize the gesture.
Question:
How can I get the SwipeDismissListViewTouchListener to work when the swipe gesture is initiated on the EditText?
SampleListFragment.java:
import android.app.Activity;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class SampleListFragment extends ListFragment {
protected AddGroupAdapter adapter;
protected ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new AddGroupAdapter(getActivity());
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sample, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
lv = getListView();
SwipeDismissListViewTouchListener listener =
new SwipeDismissListViewTouchListener(
lv,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
#Override
public boolean canDismiss(int position) {
return true;
}
#Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
adapter.removeRecord(position);
}
adapter.notifyDataSetChanged();
}
});
lv.setOnTouchListener(listener);
lv.setOnScrollListener(listener.makeScrollListener());
super.onActivityCreated(savedInstanceState);
}
public class AddGroupAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<String> data;
public AddGroupAdapter(Activity activity) {
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
data = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
data.add("Sample Text " + String.valueOf(i));
}
notifyDataSetChanged();
}
public void removeRecord(int position) {
data.remove(position);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.row, null);
holder.edit = (EditText) convertView.findViewById(R.id.edit_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.edit.setText(data.get(position));
holder.edit.setId(position);
holder.edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
final int position = view.getId();
final EditText edit = (EditText) view;
if (edit.getText() != null && !edit.getText().toString().isEmpty())
if (position < getCount())
data.set(position, edit.getText().toString());
}
}
});
return convertView;
}
}
static class ViewHolder {
EditText edit;
}
}
row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/edit_text"
android:hint="Enter Text"
android:gravity="center_horizontal"
android:singleLine="true" />
</LinearLayout>
*fragment_sample.xml:*
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#android:id/list"
android:name="com.example.app.SampleListFragment"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:descendantFocusability="beforeDescendants"
tools:context=".SampleActivity"
tools:layout="#android:layout/list_content"/>
</LinearLayout>
Trying adding this to your LinearLayout in the row.xml:
android:descendantFocusability="blocksDescendants"
Edit:
So apparently a EditText is trickier than buttons to have inside a listview. Maybe this answer can put you in the right direction if you must have them in yours: https://stackoverflow.com/a/2680077/362298
I need to make a listview keep the selected view highlighted until another item is chosen, in that case the previous view removes the highlight and the latest clicked gets the highlight.
I tried not to do this and with a radio button but it isnt working.
It's actually pretty easy:
In the custom layout for the individual listview item, make sure it has the attribute:
android:background="?android:attr/activatedBackgroundIndicator" >
By way of example, here's a listview item layout from the HoneycombGallery sample that ships with the SDK (located in the title_list_item.xml file:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:gravity="bottom"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="?android:attr/activatedBackgroundIndicator" >
</TextView>
That attribute will cause the background color to change based on whether the individual list item is selected or not.
i m novice to android so i found this question interesting and finally i succeeded after 1 and half hour ;-)
here is the code:
main.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" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
listinflater.xml
android:id="#+id/tvItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
PreservelistitemActivity.java
package com.mehuljoisar;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class PreservelistitemActivity extends Activity {
//declaring variable
ListView listView1;
MyAdapter adapter1;
Integer length,i;
String[] data = {"Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//binding to the view
listView1 = (ListView)findViewById(R.id.listView1);
//set adapter
adapter1=new MyAdapter(PreservelistitemActivity.this,data);
listView1.setAdapter(adapter1);
}
public class MyAdapter extends BaseAdapter{
private final Activity context;
private final String[] data;
public MyAdapter(Activity context,
String[] data) {
// TODO Auto-generated constructor stub
super();
this.context=context;
this.data=data;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.listinflater, null);
final TextView tvItem = (TextView)convertView.findViewById(R.id.tvItem);
tvItem.setText(data[position]);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
/* length=parent.getAdapter().getCount();
for(i=0;i<length;i++)
{
//clear
if(i==position)
{
//set
}
}
*/
v.setSelected(true);
}
});
return convertView;
}
}
}