How to change title in search box? - java

I tried to add android:title=""
that doesn't work.
SearchView searchView = (SearchView) miFind.getActionView();
searchView.setQueryHint("your message");
didn't work as well.
private SearchView.OnQueryTextListener onSearch() {
return new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
WebView mWebView= (WebView) findViewById(R.id.webView);
mWebView.loadUrl("http://www.example.com" +query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
};

If you have defined in your menu an item with this attribute:
app:actionViewClass="android.support.v7.widget.SearchView"
then you must get it in code in order to change its hint.
In onCreateOptionsMenu() method, add this code after you inflate the menu:
MenuItem miFind = menu.findItem(R.id.miFind);
SearchView searchView = (SearchView) miFind.getActionView();
searchView.setQueryHint("your message");
replace miFind with the id of the menu item which acts as your SearchView.
Edit: In onCreateOptionsMenu() add this listener:
miFind.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
searchView.setQueryHint("your message when expanded");
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
searchView.setQueryHint("your message when collapseded");
return true;
}
});

If you are talking about the search hint, you need use this method from SearchView: setQueryHint
From the documentation:
Sets the hint text to display in the query text field. This overrides
any hint specified in the SearchableInfo.
This value may be specified as an empty string to prevent any query
hint from being displayed.
Example:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<android.support.v7.widget.SearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionSearch|flagNoExtractUi"
app:searchHintIcon="#null"
app:searchIcon="#null"
app:queryBackground="#null"
app:submitBackground="#null"/>
</android.support.v7.widget.Toolbar>
Java:
final SearchView searchView = findViewById(R.id.search_view);
searchView.setQueryHint("Your search string");

Related

How do I implement SearchView with no errors

I am a beginner and new to coding. I have a problem with ListView in Android Studio. I have created a simple activity with a simple Listview. The Listview contains locations, and when the user clicks on an item the app will open google maps and takes the user to that location. The problem occurred when I implemented a SearchView. When search is applied, whatever result is filtered it will always open the first location. So could you please help me with that. Thanks.
This is my code and sorry for the mess.
MainActivity.java
import com.example.myapplicationsecond.R;
public class MainActivity9 extends AppCompatActivity {
ListView listView;
String[] name = {"First Location","Second Location","Third Location","Fourth Location",};
ArrayAdapter<String> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main9);
View view = getLayoutInflater().inflate(R.layout.abs_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
Title.setText("Search Here");
getSupportActionBar().setCustomView(view,params);
getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title
getSupportActionBar().setTitle("Search Here");
listView = findViewById(R.id.listview);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,name);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 21.422458, 39.826213"));
startActivity(intent);
}
if(position==1){
Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 24.467275, 39.610629"));
startActivity(intent);
}
if(position==2){
Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 25.173059, 45.142079"));
startActivity(intent);
}
if(position==3){
Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 26.348400, 43.766664"));
startActivity(intent);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setQueryHint("Search Here");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
arrayAdapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
MainActivity.Xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity9">
<ListView
android:id="#+id/listview"
android:textDirection="locale"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
thats how you coded it, you have if(position==0) so no matter what will be on first position you will open same geo:. you should check WHAT is on first position when clicked, so inside onItemClick put:
String clickedText = arrayAdapter.getItem(position);
then find position of this item in all-items array
int positionInArray = java.util.Arrays.asList(name).indexOf(clickedText);
and now use positionInArray for your is else
but thats a quick fix, you should have some model, your custom class with two variables, String name and String geoUri or two longs for lat and lng

Android search view not behaving right on toolbar

there you have the video that shows how my app actually behaves:
https://www.youtube.com/watch?v=81XIdEo0lks&feature=youtu.be
You see that when I click the search icon, it just moves it to the left side, then I need to click it again, then i need to click the x button 2 times before I actually get out of the search view, a complete mess.
Here's how I want my search view to behave:
https://www.youtube.com/watch?v=sJ-Z9G0SDhc&t
MainActivity.java (only the relevant part)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
MenuItem searchItem = menu.findItem(R.id.app_bar_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
return super.onCreateOptionsMenu(menu);
}
options_menu.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/app_bar_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/logout_btn"
android:title="Log Out" />
</menu>
I want my search view to behave like the search view in that video, click on the search, the keyboard appears, you can type, you can exit. I also want to make this 3 vertical dots icon disappear when I enter the search view.
First declare a state variable to determine when menu item(s) needs to be hidden
boolean isHideMenuState = false;
Then reference the item(s) needed to be hidden inside onCreateOptionsMenu block
MenuItem logoutItem = menu.findItem(R.id.logout_btn);
//... initialize other menu items here
//use hidden state to define when to hide item(s)
if(isHideMenuState){
logoutItem.setVisible(false);
//by now the ellipses(...) should go off
//if not try using setShowAsAction passing (SHOW_AS_ACTION_NEVER)
}
else{
logoutItem.setVisible(true);
}
Next: Add focus change listener to your searchView
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// Toggle isHideMenuState when search widget has focus/no focus
isHideMenuState = hasFocus
//Tell menu to re-create itself
invalidateOptionsMenu(); // onCreateOptionsMenu(...) is called again
}
});
And lastly for the UI try changing the actionViewClass of your searchView item
app:actionViewClass="android.support.v7.widget.SearchView"
click on the search, the keyboard appears, you can type, you can exit.
In order to do that, you need to do this:
in onCreateOptionsMenu:
add searchView.setIconifiedByDefault(false);
add
searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
searchView.setIconified(false);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
searchView.setIconified(true);
searchView.clearFocus();
return true;
}
});
The purpose of searchView.setIconifiedByDefault(false); is to show the expanded view of SearchView by default when user pressed the search icon. The searchView.setIconified(false); is to let the view has focus and show the soft-keyboard. The searchView.setIconified(true); is to clear the text when your user exit from the SearchView. Lastly, the searchView.clearFocus(); is to hide the soft-keyboard.
I also want to make this 3 vertical dots icon disappear when I enter the search view.
For this, you can do this:
add logoutItem.setVisible(false); in onMenuItemActionExpand
add logoutItem.setVisible(true); in onMenuItemActionCollapse

onOptionsItemSelected not being called from within fragment

For some reason, my optionItemSelected is not being called when i click the button associated with it. The timber log is not called, but this appears in the logs the moment after i click the button:
D/ViewRootImpl#ba4d36e[POIHistoryFlowActivity]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl#ba4d36e[POIHistoryFlowActivity]: ViewPostImeInputStage processPointer 1
Here is the code i have:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.activity_recent_destinations, menu);
Timber.d("Menu was inflated");
}
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_action_item_clear:
Timber.d("Clear button was clicked");
// clear the destinations
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In similar fragments, the parent's activity does not override either onCreateOptionsItem() nor onOptionsItemSelected(), but their associated buttons work while in this fragment they do not and after combing through everything with the debugger I'm at a loss as to what the root cause could be. Any help would me super appreciated!
update:
activity_recent_destinations.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/menu_action_item_clear"
android:title="#string/menu_item_title_clear"
app:actionLayout="#layout/action_item_clear"
app:showAsAction="always" />
</menu>
You are using an "action layout" for this item. This is just going to act like a normal view and clicking on it won't trigger the onOptionsItemSelected callback. You have to get a reference to the layout itself and add a click listener:
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.activity_recent_destinations, menu);
MenuItem item = menu.findItem(R.id.menu_action_item_clear);
View actionView = item.getActionView();
// Set the listener on the root view or any children if necessary
actionView.setOnClickListener(...);
}

Search view implementation in Landscape mode

I know that this question has been asked before . I tried some of the solutions but nothing is working for me .
What I want in landscape mode is for the searchview to be fully stretched in width and I dont want the keyboard to appear on full screen which makes the UI invisible.
And I want the searchview to be stretched in width
The code below is menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/Search_Box"
android:title="Search"
app:showAsAction="always"
android:icon="#drawable/search_white_24dp"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
And here is my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
MenuItem item= menu.findItem(R.id.Search_Box);
searchView = (SearchView) MenuItemCompat.getActionView(item);
if(text!=null && !text.isEmpty())
{
// MenuItemCompat.expandActionView(searchView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
item.expandActionView();
Toast.makeText(this,"here",Toast.LENGTH_SHORT).show();
}
else {
MenuItemCompat.expandActionView((MenuItem) searchView);
}
searchView.setIconified(false);
searchView.setQuery(text,false);
searchView.clearFocus();
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
please help me where i am wrong

Custom Action View layout not responding to menu item lisetener

I have created one custom action view layout using MenuItemCompat Java code is as shown in code below :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem shoppingCartitem = menu.findItem(R.id.my_action_item_id);
shoppingCartitem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.v("MyAPP", "Listener called");
return true;
}
});
MenuItemCompat.setActionView(shoppingCartitem, R.layout.my_custom_action_view_layout);
return super.onCreateOptionsMenu(menu);
}
menu_main.xml as follows :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/my_action_item_id"
android:icon="#drawable/ic_shopping_cart_white_24dp"
android:orderInCategory="9999"
android:title="#string/menu"
myapp:showAsAction="always" />
</menu>
What am I missing here, any suggestions ?
From the documentation:
Set a custom listener for invocation of this menu item. In most situations, it is more efficient and easier to use onOptionsItemSelected(MenuItem) or onContextItemSelected(MenuItem).
So you can simply override the onOptionsItemSelected callback, which is easier.
By the way to me your code seems to be right, but it doesn't do anything. Have you tried doing something like Log.v("MyAPP", "Listener called"); inside it?
EDIT:
I wrote this code:
MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.my_action_item_id);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.v("MyApp", "Inside listener");
return true;
}
});
MenuItemCompat.setActionView(item, R.layout.main_activity);
return super.onCreateOptionsMenu(menu);
}
}
main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/my_action_item_id"
android:icon="#drawable/ic_launcher"
android:orderInCategory="9999"
android:title="Menu"
myapp:showAsAction="always" />
</menu>
And this is the result of the LogCat (I clicked the icon twice):
As you can see it works without problems
please try
MenuItem shoppingCartitem = menu.findItem(R.id.my_action_item_id);
change to
MenuItem shoppingCartitem = (MenuItem) findViewById(R.id.my_action_item_id);

Categories