As the title says, I want to bring up the keyboard when I click/tap on the EditText. I've looked at other questions asking for the same thing but the answers just aren't working for me. I don't think the other questions are trying to do this in the onCreate() method and I don't think that they're using this answer for not focusing on the EditText when the app begins. I've added a list of code segments of what I tried and didn't work to avoid redundant answers.
MainActivity.java:
public class MainActivity extends ActionBarActivity {
private boolean mIsPlaying;
private int primesLE;
private GridView mGridView;
private ImageAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIsPlaying = false;
ViewGroup.LayoutParams layoutParams;
primesLE = 0;
int numOfColumns = (int)Math.round(Math.sqrt((double) primesLE));
int numOfRows = (int)Math.ceil((double)primesLE/(double)numOfColumns);
View relativeLayout = findViewById(R.id.relativeLayout);
View title_horizontalScrollView = relativeLayout.findViewById(R.id.title_horizontalScrollView);
View dataLayout = title_horizontalScrollView.findViewById(R.id.dataLayout);
mGridView = (GridView) dataLayout.findViewById(R.id.mGridView);
layoutParams = mGridView.getLayoutParams();
layoutParams.width = 150*numOfColumns; //this is in pixels
mGridView.setLayoutParams(layoutParams);
mGridView.setNumColumns(numOfColumns);
mAdapter = new ImageAdapter(this, android.R.layout.simple_list_item_1, primesLE);
mGridView.setAdapter(mAdapter);
View inputLayout = relativeLayout.findViewById(R.id.inputLayout);
EditText inputEditText = (EditText)inputLayout.findViewById(R.id.inputEditText);
//list of what doesn't work
/*
inputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
*/
/*
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(inputEditText, InputMethodManager.SHOW_IMPLICIT);
*/
/*
inputEditText.setFocusableInTouchMode(true);
inputEditText.setFocusable(true);
InputMethodManager imm = (InputMethodManager) MainActivity.this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputEditText.getWindowToken(), 0);
inputEditText.requestFocus();
imm.showSoftInput(inputEditText, 0);
*/
/*
inputEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
inputEditText.getWindowToken(), 0);
return true;
}
});
*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private void playSoE(){
for(int i = 0; i < primesLE; i++){
mAdapter.setPositionColor(i, 0xffff0000 + 0x100 * i);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Log.d("Menu","Button Pressed");
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
else if (id == R.id.action_status){
if(mIsPlaying) {
mIsPlaying = false;
item.setIcon(R.drawable.ic_action_play);
item.setTitle("Play");
playSoE();
}
else {
mIsPlaying = true;
item.setIcon(R.drawable.ic_action_pause);
item.setTitle("Pause");
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/relativeLayout"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/inputLayout"
android:layout_alignParentTop="true"
android:background="#a9a9a9"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:id="#+id/textView"
android:text="All primes lower this number:"
/>
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true" android:focusableInTouchMode="true"
android:layout_width="0px" android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="#+id/autotext"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:nextFocusUp="#id/autotext" android:nextFocusLeft="#id/autotext"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:inputType="numberSigned"
android:ems="10"
android:id="#+id/inputEditText"
android:layout_weight="1" />
</LinearLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/inputLayout"
android:id="#+id/title_horizontalScrollView"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/dataLayout"
>
<GridView
android:id="#+id/mGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</HorizontalScrollView>
</RelativeLayout>
ImageAdapter.java:
public class ImageAdapter extends ArrayAdapter {
private Context mContext;
private int mCount;
private boolean setBlueBackground = false;
private int[] gridColors;
public ImageAdapter(Context c, int resource, int count) {
super(c, resource);
mContext = c;
mCount = count;
gridColors = new int[mCount];
Arrays.fill(gridColors,Color.LTGRAY);
}
public int getCount() {
return mCount;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
textView = new TextView(mContext);
textView.setGravity(Gravity.CENTER);
textView.setLayoutParams(new GridView.LayoutParams(100, 100));
} else {
textView = (TextView) convertView;
}
textView.setBackgroundColor(gridColors[position]);
textView.setText("" + position);
return textView;
}
public void setPositionColor(int position, int color) {
gridColors[position] = color;
notifyDataSetChanged();
}
}
menu_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_status"
android:icon="#drawable/ic_action_play"
android:title="Play"
app:showAsAction="always"/>
<item android:id="#+id/action_stop"
android:icon="#drawable/ic_action_stop"
android:title="Stop"
app:showAsAction="always"/>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
P.S. I'm trying to bring up the keyboard with numbers only, and figure out how how to close it and execute it too.
You are not seeing the inputEditText because the AutoCompleteTextView control is
overlapping it. Try putting the inputEditText before the autotext and you will see the keyboard with numbers only. I also added android:orientation="vertical" to your XML.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/inputLayout"
android:layout_alignParentTop="true"
android:background="#a9a9a9"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:id="#+id/textView"
android:text="All primes lower this number:"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:ems="10"
android:id="#+id/inputEditText"/>
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true" android:focusableInTouchMode="true"
android:layout_width="0px" android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="#+id/autotext"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:nextFocusUp="#id/autotext" android:nextFocusLeft="#id/autotext"/>
</LinearLayout>
Related
I've been struggling to figure out this weird bug for a day and a half. Whenever I click on an EditText that is a child of an ExpandableListView, the screen gets obscured such that the user is unable to see what is being typed, as shown in the gif below: \n
https://giant.gfycat.com/GenuineUnluckyHyrax.mp4
I started digging into the Hierarchy View to see what could be doing this, here is a screenshot of it:
https://i.imgur.com/B4kkHv7.png
To me it looks like the Toolbar up top is extending downwards to cover the screen, which is odd because it doesnt do that on other activities that contain EditText widgets. I've checked that parent views are using "wrap_content" for height instead of "match_parent" as well, except for my DrawerLayout which must match_parent. Below is the layout code for the activity:
<!-- This DrawerLayout has two children at the root -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rlcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<!--SideBar views -->
<RelativeLayout
android:layout_width="350dp"
android:layout_height="match_parent"
android:background="#color/sidebarColor">
<ExpandableListView
android:id="#+id/qwedit_sidebar_elv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indicatorLeft="?
android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="#color/colorPrimary"
android:dividerHeight="0.5dp">
</ExpandableListView>
</RelativeLayout>
<!--MainView views -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ExpandableListView
android:id="#+id/qwedit_main_elv"
android:layout_width="930dp"
android:layout_height="wrap_content"
android:indicatorLeft="?
android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="#color/colorPrimary"
android:dividerHeight="0.5dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="25dp">
</ExpandableListView>
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:layout_marginTop="25dp"
app:menu="#menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
Here is the EVL's group layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/qwedit_sidebar_elv_parent_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:padding="3dp"
android:layout_marginLeft="40dp"
android:gravity="center"
android:textSize="20sp"
android:text="Category"/>
<ImageView
android:id="#+id/qwedit_elv_parent_status"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="35dp"
android:src="#drawable/ic_warning"/>
<TextView
android:id="#+id/qwedit_elv_parent_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:layout_gravity="left"
android:gravity="right"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:text="Score"/>
</LinearLayout>
and the item layout
<?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">
<LinearLayout
android:id="#+id/qwedit_elv_item_score_container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/qwedit_main_badView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/badScore">
<TextView
android:id="#+id/qwedit_main_badTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Bad"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_mediocreView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/mediocreScore">
<TextView
android:id="#+id/qwedit_main_mediumTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Mediocre"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_goodView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/goodScore">
<TextView
android:id="#+id/qwedit_main_goodTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Good"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_wcView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/worldclassScore">
<TextView
android:id="#+id/qwedit_main_worldclassTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="World Class"/>
</LinearLayout>
</LinearLayout>
<EditText
android:layout_width="930dp"
android:layout_height="wrap_content"
android:layout_below="#+id/qwedit_elv_item_score_container"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Comments"/>
</RelativeLayout>
I'm assuming that this is all due to the layouts, but here is the CustomExpandableListAdapter that I wrote for it just in case the answer lies in how the views are being inflated:
/**
* Created by Tadhg on 9/8/2017.
*/
public class QwEditMainCustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
//Values inits
private int categoryScore;
private int subCategoryScore;
private boolean completedCategory;
private boolean completedSubCategory;
// ChildView views
private View badScoreView;
private View mediocreScoreView;
private View goodScoreView;
private View worldclassScoreView;
TextView badScoreTextView;
TextView mediocreScoreTextView;
TextView goodScoreTextView;
TextView worldClassScoreTextView;
EditText questionCommentEditText;
public QwEditMainCustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail
.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.qwedit_main_elv_item, null);
}
// ChildView views
badScoreView = convertView.findViewById(R.id.qwedit_main_badView);
mediocreScoreView = convertView.findViewById(R.id.qwedit_main_mediocreView);
goodScoreView = convertView.findViewById(R.id.qwedit_main_goodView);
worldclassScoreView = convertView.findViewById(R.id.qwedit_main_wcView);
badScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_badTextView);
mediocreScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_mediumTextView);
goodScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_goodTextView);
worldClassScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_worldclassTextView);
// TODO: TextView score subcategory setters
badScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 1;
badScoreView.setBackgroundColor(Color.parseColor("#eca9a7"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
mediocreScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 2;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f7d6a6"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
goodScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 3;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#addbad"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
}
});
worldclassScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 4;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#80baeb"));
}
});
/**
* Use variables categoryScore and subCategoryScore to update scores
*/
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)).size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.qwedit_main_elv_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.qwedit_main_elv_parent_title);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() { return false; }
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) { return true; }
}
Any points or tips would be great. I think that this may be due to the number of views in my hierarchy (or at least a subissue of it), but I'm not sure and am relatively new to Android Development.
Thanks!
I solved the issue, simply adding android:windowSoftInputMode="adjustNothing|stateHidden" caused the ActionBar to not expand to 996px when the EditText gained focus
each of my listview's rows holds a number of textviews.
Each textview has singleline set to true and is truncated at the end.
If a textview is truncated, it should be longclickable by the user to open a popup or toast and print the whole text.
So here is my problem:
As soon as I add the OnLongClickListener to a textview, that is part of a row of a listView, the onItemClick method of that listView is not called anymore when the textview is clicked (not longclicked).
XML:
List row item:
<TextView
android:id="#+id/leadingText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:gravity="center"
android:textSize="35sp"
android:singleLine="true"
android:text="20x"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<ExpandableTextView
android:id="#+id/firstLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center_vertical"
android:clickable="false"
android:focusable="false"
/>
<TextView
android:id="#+id/secondLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:singleLine="true"
/>
</LinearLayout>
ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:divider="#android:color/transparent"
android:dividerHeight="5.0dp"
android:layout_width="match_parent"
android:focusable="true"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
Code:
Adding the onItemClickListener to my listView (this happens in a fragment, that holds my listview):
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
UIListEntry item = (UIListEntry) parent.getItemAtPosition(position);
if(selectedKey != item.getId()) {
selectedKey = item.getId();
view.setSelected(true);
} else {
selectedKey = null;
view.setSelected(false);
view.setActivated(false);
}
}
});
Setting the onLongClickListener in my custom TextView ExpandableTextView
private void init() {
this.post(new Runnable() {
#Override
public void run() {
final ViewTreeObserver vto = getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Layout l = getLayout();
if (l != null) {
int lines = l.getLineCount();
if (lines > 0)
if (l.getEllipsisCount(lines - 1) > 0)
addLongClickListener();
}
vto.removeOnGlobalLayoutListener(this);
}
});
}
});
}
private void addLongClickListener() {
this.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast t = Toast.makeText(SmlApp.getInstance().getApplicationContext(), getText(), Toast.LENGTH_SHORT);
t.show();
return false;
}
});
}
Is there a way to implement an individual onLongClickListener for each textview but keep the onClickListener for the whole row, so the textview does not intercept it and the row is still selectable? Setting the onItemLongClickListener on the listView won't help me, because i don't want the whole row to be longclickable but the individual ExpandableTextView
I have a GridView of TextViews, I want to iterate though each TextView, change it's background, wait for 1 second, and go to the next TextView and do the same thing.
First start off with none of the TextViews being colored
Then I want to color the first TextView
Now I want to wait for one second...
and afterwards.
And repeat with the remaining TextViews.
The problem is, MainActivity.playSoE(), the method that does updating of the grid, uses notifyDataSetChanged() to update the GridView. Which makes threading more of an issue. Everything I've tried either causes an Exception or freezes everything and skips the intermediate steps until the whole Grid is colored. I've tried using Activity.runOnUiThread(), wait(), Thread.sleep() and tried making a new Thread but none of these has worked for me. I
And to clarify, I want to be able to communicate with the rest of the app while the Grid is being colored, I don't want the entire app freezing on me.
MainActivity.java:
public class MainActivity extends ActionBarActivity {
private boolean mIsPlaying;
private int primesLE;
private GridView mGridView;
private ImageAdapter mAdapter;
private Thread mThread;
private void dataVisualization(){
int numOfColumns = (int)Math.round(Math.sqrt((double) primesLE));
//int numOfRows = (int)Math.ceil((double)primesLE/(double)numOfColumns);
ViewGroup.LayoutParams layoutParams;
layoutParams = mGridView.getLayoutParams();
layoutParams.width = 150*numOfColumns; //this is in pixels
mGridView.setLayoutParams(layoutParams);
mGridView.setNumColumns(numOfColumns);
mAdapter = new ImageAdapter(this, android.R.layout.simple_list_item_1, primesLE);
mGridView.setAdapter(mAdapter);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIsPlaying = false;
primesLE = 0;
View relativeLayout = findViewById(R.id.relativeLayout);
View title_horizontalScrollView = relativeLayout.findViewById(R.id.title_horizontalScrollView);
View dataLayout = title_horizontalScrollView.findViewById(R.id.dataLayout);
mGridView = (GridView) dataLayout.findViewById(R.id.mGridView);
dataVisualization();
View inputLayout = relativeLayout.findViewById(R.id.inputLayout);
final EditText inputEditText = (EditText)inputLayout.findViewById(R.id.inputEditText);
inputEditText.setOnKeyListener(new EditText.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
InputMethodManager inputManager = (InputMethodManager) MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(inputEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
try {
primesLE = Integer.parseInt(inputEditText.getText().toString());
} catch(NumberFormatException nfe) {
inputEditText.setText("Try again");
primesLE = 0;
}
if(primesLE < 0)
primesLE = 0;
dataVisualization();
Toast.makeText(MainActivity.this, inputEditText.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
inputEditText.setOnClickListener(new EditText.OnClickListener() {
#Override
public void onClick(View v) {
inputEditText.getText().clear();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private void playSoE(){
for(int i = 0; i < primesLE; i++) {
mAdapter.setPositionColor(i, 0xffff0000 + 0x100 * i); //calls notifyDataSetChanged()
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
}
}, 1000);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Log.d("Menu","Button Pressed");
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
else if (id == R.id.action_status){
if(mIsPlaying) {
mIsPlaying = false;
item.setIcon(R.drawable.ic_action_play);
item.setTitle("Play");
playSoE();
}
else {
mIsPlaying = true;
item.setIcon(R.drawable.ic_action_pause);
item.setTitle("Pause");
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
ImageAdapter.java:
public class ImageAdapter extends ArrayAdapter {
private Context mContext;
private int mCount;
private boolean setBlueBackground = false;
private int[] gridColors;
public ImageAdapter(Context c, int resource, int count) {
super(c, resource);
mContext = c;
mCount = count;
gridColors = new int[mCount];
Arrays.fill(gridColors,Color.LTGRAY);
}
public int getCount() {
return mCount;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
textView = new TextView(mContext);
textView.setGravity(Gravity.CENTER);
textView.setLayoutParams(new GridView.LayoutParams(100, 100));
} else {
textView = (TextView) convertView;
}
textView.setBackgroundColor(gridColors[position]);
textView.setText("" + position);
return textView;
}
public void setPositionColor(int position, int color) {
gridColors[position] = color;
notifyDataSetChanged();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/relativeLayout"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/inputLayout"
android:layout_alignParentTop="true"
android:background="#a9a9a9"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:id="#+id/textView"
android:text="All primes less than\n or equal to this number:"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true" android:focusableInTouchMode="true"
android:layout_width="0px" android:layout_height="0px"
android:inputType="numberSigned"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="#+id/autotext"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:nextFocusUp="#id/autotext" android:nextFocusLeft="#id/autotext"
android:inputType="numberSigned" android:imeOptions="actionDone"
android:layout_gravity="center_vertical" android:gravity="center_vertical"
android:layout_centerVertical="true" android:layout_toEndOf="#+id/textView"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:inputType="numberSigned"
android:ems="10"
android:id="#+id/inputEditText"
android:layout_centerVertical="true"
android:layout_alignEnd="#+id/autotext"
android:layout_toEndOf="#+id/textView" />
</RelativeLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/inputLayout"
android:id="#+id/title_horizontalScrollView"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/dataLayout"
>
<GridView
android:id="#+id/mGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</HorizontalScrollView>
</RelativeLayout>
menu_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_status"
android:icon="#drawable/ic_action_play"
android:title="Play"
app:showAsAction="always"/>
<item android:id="#+id/action_stop"
android:icon="#drawable/ic_action_stop"
android:title="Stop"
app:showAsAction="always"/>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
Ultimately you should switch to using a RecyclerView with a GridLayoutManager. The RecyclerView offers very flexible methods for updating the UI. In your case, notifyItemChanged(int position) would efficiently update one single TextView. Find out more here.
You can use Handler to delay and perform your action
Edited
for(int i = 0; i < primesLE; i++) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mAdapter.setPositionColor(i, 0xffff0000 + 0x100 * i);
notifyDataSetChanged();
}
}, 1000);
}
This will execute for 1 second and will update the data
I am trying to create a demo app where the users enter a search and destination, the data will be queried from parse.com and then the matching result will be displayed in the next screen's listview. I have followed a few tutorials but for sure haven't been able to grasp the concept concretely enough. When I enter a search query, the progress bar shows up and then the application goes back into the previous activity. I can't understand where exactly things are going wrong.
This is my mainactivity class.
public class CarpoolingActivitySearch extends ActionBarActivity {
ListView listView;
ArrayList<Travellers> travellers;
CarpoolingAdapter adapter;
protected ProgressDialog proDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carpooling_activity_search);
final EditText mSrc = (EditText)findViewById(R.id.carpooling_source);
final EditText mDst = (EditText)findViewById(R.id.destination);
Button mSubmitButton = (Button)findViewById(R.id.carpooling_submit);
mSubmitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String source = mSrc.getEditableText().toString();
String destination = mDst.getEditableText().toString();
listView = (ListView) findViewById(R.id.list);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Carpooling");
query.whereEqualTo("Source", source); //assume you have a DonAcc column in your Country table
query.whereEqualTo("Destination", destination); //assume you have a DonAcc column in your Country table
startLoading();
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if(e==null)
{
for(int i=0;i<parseObjects.size();i++)
{
Travellers travellers1 = new Travellers();
travellers1.setSource("Source");
travellers1.setDestination("Destination");
travellers.add(travellers1);
if(travellers.size() > 0)
{
adapter = new CarpoolingAdapter(getApplicationContext(),R.layout.activity_carpooling_activity_search,travellers);
listView.setAdapter(adapter);
} else {
AlertDialog.Builder popup = new AlertDialog.Builder(CarpoolingActivitySearch.this);
popup.setMessage("Seems our servers are busy. Try again in some time.");
popup.setPositiveButton("Back",null);
AlertDialog dialog = popup.create();
dialog.show();
}
}
stopLoading();
finish();
}
else {
stopLoading();
AlertDialog.Builder popup = new AlertDialog.Builder(CarpoolingActivitySearch.this);
popup.setMessage(e.getMessage());
popup.setPositiveButton("Back",null);
AlertDialog dialog = popup.create();
dialog.show();
}
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_carpooling_activity_search, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected void startLoading() {
proDialog = new ProgressDialog(this);
proDialog.setMessage("loading...");
proDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
proDialog.setCancelable(false);
proDialog.show();
}
protected void stopLoading() {
proDialog.dismiss();
proDialog = null;
}
}
I have created a custom adapter for this purpose.
public class CarpoolingAdapter extends ArrayAdapter<Travellers> {
ArrayList<Travellers> travellersArrayList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
Context context;
public CarpoolingAdapter(Context context, int resource, ArrayList<Travellers> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
travellersArrayList = objects;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView== null) {
convertView = vi.inflate(Resource,null);
holder = new ViewHolder();
holder.mSource = (TextView)convertView.findViewById(R.id.textView);
holder.mDestination = (TextView)convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.mSource.setText(travellersArrayList.get(position).getSource());
holder.mDestination.setText(travellersArrayList.get(position).getDestination());
return convertView;
}
static class ViewHolder {
public TextView mSource;
public TextView mDestination;
}
}
And the travellers class with a default constructor and getter/setter methods.
public class Travellers {
private String Source;
private String Destination;
public Travellers() {
}
public Travellers(String source, String destination) {
super();
Source = source;
Destination = destination;
}
public String getSource() {
return Source;
}
public void setSource(String source) {
Source = source;
}
public String getDestination() {
return Destination;
}
public void setDestination(String destination) {
Destination = destination;
}
}
The main layout file named activity_carpooling_activity_search.xml.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.android.myapplication.Carpooling.Carpooling">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/carpooling_source"
android:hint="Source"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/destination"
android:hint="Destination"
android:layout_below="#+id/carpooling_source"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="86dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/carpooling_submit"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_toEndOf="#+id/carpooling_submit"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_below="#+id/carpooling_submit">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list" />
</ScrollView>
</RelativeLayout>
And finally my list_item_deatil.xml file.
<?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"
android:weightSum="1">
<TextView
android:layout_width="174dp"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView"
android:layout_weight="0.13" />
<TextView
android:layout_width="245dp"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView2"
android:layout_weight="0.13" />
</LinearLayout>
What should I modify to make it work appropriately? I am not getting any errors as such.
First of all, you have posted a very long code blocks where indentation is not good enough to read you code.
So what I understood, as your ParseException e is null that means there is no exception. Inside that if block, after the for loop you are calling finish() which is causing your current activity instance to be destroyed thus going to previous activity.
I hope this helps you.
How do I use the swipe gesture to remove cards from my recycle-view in the same way that it is done in Google-now etc. So far I've created the cardview application but it's removing cards via a swipe gesture which I'm having problems with. I haven't found a single tutorial or question answered on this website which could help.
Any help would be very much appreciated. My code is below.
MyActivity
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_my);
setContentView(R.layout.activity_my);
RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
ContactAdapter ca = new ContactAdapter(createList(30));
recList.setAdapter(ca);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private List<ContactInfo> createList(int size) {
List<ContactInfo> result = new ArrayList<ContactInfo>();
for (int i=1; i <= size; i++) {
ContactInfo ci = new ContactInfo();
ci.name = ContactInfo.NAME_PREFIX + i;
ci.surname = ContactInfo.SURNAME_PREFIX + i;
ci.email = ContactInfo.EMAIL_PREFIX + i + "#test.com";
result.add(ci);
}
return result;
}
}
ContactInfo.java
public class ContactInfo {
protected String name;
protected String surname;
protected String email;
protected static final String NAME_PREFIX = "Name_";
protected static final String SURNAME_PREFIX = "Surname_";
protected static final String EMAIL_PREFIX = "email_";
}
ContactAdapter.java
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
private List<ContactInfo> contactList;
public ContactAdapter(List<ContactInfo> contactList) {
this.contactList = contactList;
}
#Override
public int getItemCount() {
return contactList.size();
}
#Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
ContactInfo ci = contactList.get(i);
contactViewHolder.vName.setText(ci.name);
contactViewHolder.vSurname.setText(ci.surname);
contactViewHolder.vEmail.setText(ci.email);
contactViewHolder.vTitle.setText(ci.name + " " + ci.surname);
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.card_layout, viewGroup, false);
return new ContactViewHolder(itemView);
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
protected TextView vName;
protected TextView vSurname;
protected TextView vEmail;
protected TextView vTitle;
public ContactViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.txtName);
vSurname = (TextView) v.findViewById(R.id.txtSurname);
vEmail = (TextView) v.findViewById(R.id.txtEmail);
vTitle = (TextView) v.findViewById(R.id.title);
}
}
}
activity_my.xml
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
card_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#color/bkg_card"
android:text="contact det"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:textSize="14dp"/>
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#id/title"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="#+id/txtSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#id/txtName"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="#+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="150dp"
android:layout_alignBaseline="#id/txtName"/>
<TextView
android:id="#+id/txtAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textSize="10dp"
android:layout_alignStart="#id/txtEmail"
android:layout_alignBaseline="#id/txtSurname"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
You can use below code (ItemTouchHelper.SimpleCallback from Android support V7) to remove the cards from RecyclerView using swipe gesture
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
contacts.remove(viewHolder.getAdapterPosition());
ca.notifyItemRemoved(viewHolder.getAdapterPosition());
}
#Override
public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(recList);