I am trying to have it so that when I hit enter the recyclerview is cleared out of the old data and new data is grabbed and put in and displayed. I have it so that anytime that enter is hit it grabs what is in the edittext and put that into a variable that is then searched for. The variable is being updated but the array that holds the searched for items isn't updated. Do I need to clear that array out? I think to do that I would need to use an ArrayList instead of a normal array. Would that be a better idea?
public View.OnKeyListener search = new View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
searchItem = "";
if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER)
{
if(searchItem != searchBar.getText().toString())
{
searchItem = searchBar.getText().toString();
Toast.makeText(imdb_activity.this, "The search is: " + searchItem, Toast.LENGTH_SHORT).show();
Log.d("Android view component", "Enter button was pressed and the value is: " + searchItem);
}
//clear recyclerview?
goGetSearch();
return true;
}
return false;
}
};
you need to pass the updated list to the recyclerview, I assume that goGetSearch is the function where you pass the parameters to recyclerview and the searchItem is your list, so I hope you got the concept and if my assumption is wrong, you can edit it to fit in your case.
public View.OnKeyListener search = new View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
searchItem = "";
if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER)
{
if(searchItem != searchBar.getText().toString())
{
searchItem = searchBar.getText().toString();
Toast.makeText(imdb_activity.this, "The search is: " + searchItem,
Toast.LENGTH_SHORT).show();
Log.d("Android view component", "Enter button was pressed and the value
is: " + searchItem);
}
//clear recyclerview?
goGetSearch(searchItem);
return true;
}
return false;
}
};
Related
I have a multiline editText in my activity. And what I want is ecerytime the user inputs new line / next line / enter , a bullet should be generated automatically. What should I do? Below is an example of the expected output:
• Item 1 (user presses enter / new line)
•
What I'm not sure about is if you also want the first line to get a bullet automatically. Try this for getting a bullet in the new line:
final EditText et = findViewById(R.id.edittext);
et.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent event) {
// onKey is fired twice, we filter one out
if (event.getAction()!=KeyEvent.ACTION_UP) {
return false;
}
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
String oldString = et.getText().toString();
String newString = oldString + "\u25CF";
et.setText(newString);
et.setSelection(et.getText().length());
}
return false;
}
});
You can add a character to do this.
• = \u2022
● = \u25CF
You should be able to add one to the editbox.
yourBox.setOnKeyListener(new View.OnKeyListener() {
#Override public boolean onKey (View v,int keyCode, KeyEvent event){
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
String myString = yourBox.getText().toString();
yourBox.setText("\u25CF " + myString);
}
return false;
}
});
You have propaply seen those pretty cheap android tv boxes available on the market. They are usually followed with a remote control that has some functionalities like clicking, swipe or slide to left and right up and down.
Recently i made an app and tried to navigate it using the remote control. I have some gesture methods in the project. I tried to swipe to left and right but the app didnt do anything while when i try it on my phone that has a screen gets the gesture and do what it should. Like opening the navigation drawer etc.
Now to my question: does one need to use speciel methods? Is there some rules that one should be ware of?
Edit
This is what Ive done so far. I made a class that defines the action: It's from google.developer
public class Dpad {
public final static int UP = 0;
public final static int LEFT = 1;
public final static int RIGHT = 2;
public final static int DOWN = 3;
public final static int CENTER = 4;
int directionPressed = -1; // initialized to -1
public int getDirectionPressed(InputEvent event) {
if (!isDpadDevice(event)) {
return -1;
}
// If the input event is a MotionEvent, check its hat axis values.
if (event instanceof MotionEvent) {
// Use the hat axis value to find the D-pad direction
MotionEvent motionEvent = (MotionEvent) event;
float xaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_X);
float yaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_Y);
// Check if the AXIS_HAT_X value is -1 or 1, and set the D-pad
// LEFT and RIGHT direction accordingly.
if (Float.compare(xaxis, -1.0f) == 0) {
directionPressed = Dpad.LEFT;
} else if (Float.compare(xaxis, 1.0f) == 0) {
directionPressed = Dpad.RIGHT;
}
// Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad
// UP and DOWN direction accordingly.
else if (Float.compare(yaxis, -1.0f) == 0) {
directionPressed = Dpad.UP;
} else if (Float.compare(yaxis, 1.0f) == 0) {
directionPressed = Dpad.DOWN;
}
}
// If the input event is a KeyEvent, check its key code.
else if (event instanceof KeyEvent) {
// Use the key code to find the D-pad direction.
KeyEvent keyEvent = (KeyEvent) event;
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
directionPressed = Dpad.LEFT;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
directionPressed = Dpad.RIGHT;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
directionPressed = Dpad.UP;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
directionPressed = Dpad.DOWN;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
directionPressed = Dpad.CENTER;
}
}
return directionPressed;
}
public static boolean isDpadDevice(InputEvent event) {
// Check that input comes from a device with directional pads.
if ((event.getSource() & InputDevice.SOURCE_DPAD)
!= InputDevice.SOURCE_DPAD) {
return true;
} else {
return false;
}
}
}
In my MainActivity I have a navigationdrawer. which I want to open and close when remotecontrol D-pad wants it
mDrawerLayout.setOnGenericMotionListener(new View.OnGenericMotionListener() {
#Override
public boolean onGenericMotion(View view, MotionEvent motionEvent) {
if (Dpad.isDpadDevice(motionEvent)) {
int press = mDpad.getDirectionPressed(motionEvent);
switch (press) {
case Dpad.RIGHT:
// Do something for UP direction press Open the drawer
mDrawerLayout.openDrawer(Gravity.START);
return true;
case Dpad.LEFT:
mDrawerLayout.closeDrawer(Gravity.START);
return true;
}
}
return false;
}
});
In one of the fragments I have a media player and using D-pad up and D-pad down I change the video.
v.setOnGenericMotionListener(new View.OnGenericMotionListener() {
#Override
public boolean onGenericMotion(View view, MotionEvent motionEvent) {
if (Dpad.isDpadDevice(motionEvent)) {
int press = mDpad.getDirectionPressed(motionEvent);
switch (press) {
case Dpad.UP:
// Do something for UP direction press
UP(); // Change the video to next
return true;
case Dpad.DOWN:
DOWN(); // Change the video the earlier on
return true;
}
}
return false;
}
});
EDIT
It' now become a problem since it doesn't response to any motion. I tried on a emulator with physical keyboard and not a single action happens. I would appritiate if someone gives me a hint.
i mean this kind of remote control in the picture below
Thanks in advance
I think the code on Android Developer is wrong:
public static boolean isDpadDevice(InputEvent event) {
// Check that input comes from a device with directional pads.
if ((event.getSource() & InputDevice.SOURCE_DPAD)
!= InputDevice.SOURCE_DPAD) {
return true;
} else {
return false;
}
}
The condition should be "==" instead of "!=", so this is correct:
public static boolean isDpadDevice(InputEvent event) {
// Check that input comes from a device with directional pads.
if ((event.getSource() & InputDevice.SOURCE_DPAD)
== InputDevice.SOURCE_DPAD) {
return true;
} else {
return false;
}
}
Problem Description
In my application I have GridView with 12 items in it. Every item in the GridView has its own icon like it is shown in the image below. For every icon I have two images one pressed and one normal.
Question
I try to find some standard mechanisms to change images from Normal state to Presses state when user press on the item and then release the button, but can't find. Instead of that I use public boolean onTouch(View v, MotionEvent event) method but it brings to some side effects for example when user scroll down or slide screen to change tab, items are clicked.
Source Code
#Override
public boolean onTouch(View v, MotionEvent event) {
/* Get Action on Touch. */
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
float currentXPosition = event.getX();
float currentYPosition = event.getY();
lastPressedPosition = gvCategories.pointToPosition((int) currentXPosition, (int) currentYPosition);
if(lastPressedPosition < 0 || lastPressedPosition > sDefaultArray.length)
return false;
/* Set Pressed Image. */
Drawable drawable = getActivity().getResources().getDrawable(sPressedArray[lastPressedPosition]);
Category category = (Category) gvCategories.getItemAtPosition(lastPressedPosition);
category.setImage(((BitmapDrawable) drawable).getBitmap());
CategoriesGridAdapter adapter = (CategoriesGridAdapter)gvCategories.getAdapter();
adapter.notifyDataSetChanged();
}
else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
/* Get current possition to compare with the last one. */
float currentXPosition = event.getX();
float currentYPosition = event.getY();
int currentPosition = gvCategories.pointToPosition((int) currentXPosition, (int) currentYPosition);
if(currentPosition == -1 && lastPressedPosition == -1)
return false;
if (currentPosition == lastPressedPosition && action == MotionEvent.ACTION_UP) {
Category category = (Category) gvCategories.getItemAtPosition(currentPosition);
Log.i(TAG, String.format("Category Title is: %s", category.getTitle()));
if (category == Category.More) {
//tracker.trackEvent("Category Clicked", "More", "", 0L);
/* Get current selected language. */
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
final String lang = preferences.getString(Preferences.LANGUAGE, "en");
/* Load All categories available in the application. */
updateCategoriesAdapter(lang, true);
}
else {
//tracker.trackEvent("Category Clicked", category.getTitle(), "", 0L);
CategoryDetailsActivity.sFragmentManager = getFragmentManager();
/* Start categories detail activity. */
Intent intent = new Intent(getActivity().getApplicationContext(), CategoryDetailsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(CategoryDetailsActivity.CATEGORIES_IDS, category.getIDs());
intent.putExtra(CategoryDetailsActivity.CATEGORY_NAME, category.getTitle());
getActivity().getApplicationContext().startActivity(intent);
}
}
if(lastPressedPosition == -1)
return false;
/* Set Pressed Image. */
Drawable drawable = getActivity().getResources().getDrawable(sDefaultArray[lastPressedPosition]);
Category category = (Category) gvCategories.getItemAtPosition(lastPressedPosition);
category.setImage(((BitmapDrawable) drawable).getBitmap());
CategoriesGridAdapter adapter = (CategoriesGridAdapter)gvCategories.getAdapter();
adapter.notifyDataSetChanged();
}
return false;
}
If you have the two images already made, use a Button element in your GridView and set the Background element of your button to a state selector drawable. refer to this link for XML declarations on making your selector drawable example here .
For your scenario you will have to create different selector XML files for each button.
I'm trying to use a SearchView and I got everything to work, except when I want to search an empty string.
The onQueryTextChange does react when I remove the last character, but I want the user to be able to press the search button when the searchfield is empty.
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
// Do something
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
I've also tried using a OnKeyListner. but it does not seem to work either.
searchView.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
//Do something
return true;
}
});
This seems such a simple thing to do, but I can't get it to work. Any suggestions?
Edit
I have looked for a solution for a while now and just some minutes after posting this, I found a solution.
On this thread I found out this was not a bug, but it actually was deliberate.
Android SearchView.OnQueryTextListener OnQueryTextSubmit not fired on empty query string
So I just downloaded ActionBarSherlock and made some modification to the method onSubmitQuery()
From
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if (query != null && TextUtils.getTrimmedLength(query) > 0) {
if (mOnQueryChangeListener == null
|| !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
if (mSearchable != null) {
launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
setImeVisibility(false);
}
dismissSuggestions();
}
}
}
And the modified version
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if(query == null) {query = "";}
if (mOnQueryChangeListener == null
|| !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
if (mSearchable != null) {
launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
setImeVisibility(false);
}
dismissSuggestions();
}
}
Hope this helps if anyone else is having this problem.
Perhaps it's not suited for your app but you also have the option to use a simple EditText, then add a TextWatcher listener. This way you catch every input even if the user enters an empty string.
I had same issue with SearchView but I did not want to use ActionBarSherlock so I came up with solution which consists of styling your own SearchView as shown in guide http://novoda.com/blog/styling-the-actionbar-searchview/ and setting OnEditorActionListener for EditText( How do I trigger an action when the user has hit enter?) which is part of the SearchView.
final SearchView searchView = (SearchView)findViewById(R.id.search);
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//Do something
}
return false;
});
I need drag and drop in my new project. I referred this blogpost. But I'm facing a problem while performing Drop. I cannot get the image which I hold and can't drop into view. DragListner which I used is given below. I don't know how to handle the dropevent.
class MyDragListener implements OnDragListener {
#Override
public boolean onDrag(View view, DragEvent dragEvent) {
int dragAction = dragEvent.getAction();
View dragView = (View) dragEvent.getLocalState();
if (dragAction == DragEvent.ACTION_DRAG_EXITED) {
System.out.println("exit------------");
containsDragable = false;
} else if (dragAction == DragEvent.ACTION_DRAG_ENTERED) {
System.out.println("enter------------");
containsDragable = true;
} else if (dragAction == DragEvent.ACTION_DRAG_ENDED) {
System.out.println("end------------");
dragView.setVisibility(View.VISIBLE);
} else if (dragAction == DragEvent.ACTION_DROP && containsDragable) {
dragView.setVisibility(View.VISIBLE);
}
return true;
}
maybe at first you can print out the
DragEvent.ACTION_DROP
code, and compare it to the value of the dragAction .
if the value is the same then the problem are at containsDragable function.
hope this helps