After change orientation searchview takes the entire screen - android

I have this problem after change orientation:
I would have screen like this:
I saw That problem in a lot of app, only Note doesn't has it

Try adding this line to the EditText or in SearchView
android:imeOptions="flagNoExtractUi"
<androidx.appcompat.widget.SearchView
android:layout_width="match_parent"
android:imeOptions="flagNoExtractUi"
android:layout_height="wrap_content"/>

If you are using Toolbar menu to search then update this like below in your Activity/Fragment
#Override
public void onPrepareOptionsMenu(#NonNull Menu menu) {
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setImeOptions(searchView.getImeOptions() | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
super.onPrepareOptionsMenu(menu);
}

Related

How to set Button as MediaRouteButton?

As we set menuItem as the MediaRouteButton,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.cast_item_menu, menu);
mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu, R.id.media_route_menu_item);
return true;
}
Is there any way to set a button or an ImageButton as the MediaRouteButton in android?
Finally came up with a solution. Hope this will help someone.
Layout XML file
Create a MediaRouteButton using xml.
<android.support.v7.app.MediaRouteButton
android:id="#+id/cast"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentRight="true"
android:scaleType="center"
android:src="#drawable/ic_google_cast_logo"
android:visibility="visible" />
This will create the button you need. Then use it in your activity.
Activity
Instantiate the button and set the media button this way.
MediaRouteButton mediaRouteButton = (MediaRouteButton)findViewById(R.id.cast);
CastButtonFactory.setUpMediaRouteButton(getApplicationContext(),mediaRouteButton);
Happy coding...(y)

Android SearchView - Move icon to right (without ActionBar)

I have an android SearchView like so -
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search the forum ..."
android:iconifiedByDefault="true"
android:id="#+id/search_forum"
android:layout_alignParentRight="true"
android:textColor="#aaa" />
This is not in my ActionBar, it's somewhere else in my activity.
I need the icon (the search icon) to stay on the right of my layout. On click it should expand into the SearchView (same behaviour as the ActionBar SearchView). I've tried android:layout_alignParentRight="true" but the icon stays to the left.
Any help would be great :)
I found a solution for this problem. Just set layout Direction to "rtl" and icon will be in Right Side. Here is the code for my search view and it is working as intended.
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
android:iconifiedByDefault="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I couldn't find an exact answer to this so I did this-
Create an image view with search icon. Placed it on the right of my layout.
On click, made the search view visible and set iconified false and requested focus for the search view.
I'd be so grateful if somebody could eventually help me find a proper solution for this.
If the question is still unanswered:
Just move the SearchView into a RelativeLayout, and align it to the right side of the parent:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SearchView
android:layout_width="wrap_content"
android:id="#+id/searchView"
android:layout_alignParentRight="true"
android:layout_height="match_parent" />
The final result looks like this:
CardView with Linearlayout containing aligned SearchView
Adding the SearchView as a menu item instead of a Toolbar element inherently solves the problem.
An elegent way to do it:
Step 1 - add the search field to you toolbar:
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
Step 2 - add the logic to your onCreateOptionsMenu()
import android.support.v7.widget.SearchView; // not the default !
#Override
public boolean onCreateOptionsMenu( Menu menu) {
getMenuInflater().inflate( R.menu.main, menu);
MenuItem myActionMenuItem = menu.findItem( R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
UserFeedback.show( "SearchOnQueryTextSubmit: " + query);
if( ! searchView.isIconified()) {
searchView.setIconified(true);
}
myActionMenuItem.collapseActionView();
return false;
}
#Override
public boolean onQueryTextChange(String s) {
// UserFeedback.show( "SearchOnQueryTextChanged: " + s);
return false;
}
});
return true;
}
Source: https://stackoverflow.com/a/34255229/384564
I've exactly same requirements for SearchView placed other than Toolbar, i.e. when collapsed search icon should be on right end side and when expanded it covers entire width.
I've tried out this solution for SearchView within RelativeLayout.
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true" />
And handled OnSearchClickListener and OnCloseListener in code to toggle width between MATCH_PARENT and WRAP_CONTENT.
searchView.setOnSearchClickListener {
val params = it.layoutParams as RelativeLayout.LayoutParams
params.width = RelativeLayout.LayoutParams.MATCH_PARENT
it.layoutParams = params
}
searchView.setOnCloseListener {
val params = searchView.layoutParams as RelativeLayout.LayoutParams
params.width = RelativeLayout.LayoutParams.WRAP_CONTENT
searchView.layoutParams = params
return#setOnCloseListener false
}
Try this:
SearchView searchView =
(SearchView) findViewById(R.id.search_view);
searchView.setIconifiedByDefault(false);
searchView.setIconified(false);
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
ViewGroup linearLayoutSearchView =
(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);
linearLayoutSearchView.addView(searchViewIcon);
Taken from here.
Worked for me this way. I use Toolbar not ActionBar.
searchView.setLayoutParams(new Toolbar.LayoutParams(Gravity.RIGHT));

Android - Make whole search bar clickable

I am using a search-view element in my fragment to implement search feature.
<SearchView
android:id="#+id/search_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginBottom="7dp"
android:background="#color/white" />
The problem is only the search icon is clickable other area in the search bar is not clickable, when i click the icon only i can able to search.
Can you please help me to make the whole search area clickable.
This can be simply done by setting Iconified to false on OnClick of SearchView.
searchBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchBar.setIconified(false);
}
});
Reference: Eric Lui's answer
Hopefully it will help.
UPDATE:
You can also use it directly in your XML
app:iconifiedByDefault="false"
search_bar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
search_bar.onActionViewExpanded();
}
});
The trick isn't so much to make the entire area clickable as much as it is to expand it and then hide the keyboard.
First, your layout in the XML is fine, leave it as is, in your Java, you want to have the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//Define your Searchview
SearchView searchView = (SearchView) findViewById(R.id.search_bar);
//Turn iconified to false:
searchView.setIconified(false);
//The above line will expand it to fit the area as well as throw up the keyboard
//To remove the keyboard, but make sure you keep the expanded version:
searchView.clearFocus();
}
What this accomplishes is it expands the keyboard to the entire length of the area, it focuses on it, which allows the entire area to be clickable, and then it removes the keyboard so that it looks to the user like they are able to click that field and bring up a keyboard.
Should accomplish what you are looking for.
-Sil
add this to your xml android:iconifiedByDefault="false" it will keep
open your searchview.
and to clear it add clearfocus to your searchview object.
What is the clickable mean? trigger search action or just make the edit area focused? If it is the first, you can just make the icon clickable=false. and make the whole layout clickable and implement a event listener.
<SearchView
android:id="#+id/search_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:click="onClick"
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginBottom="7dp"
android:background="#color/white" />
The onClick method should be
public void onClick(View v) {
  InputMethodManager im = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
im.showSoftInput(editText, 0);
}
For latest versions of android -
Try:
app:iconifiedByDefault="false"
Example -
<android.support.v7.widget.SearchView
android:id="#+id/source_location_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_marginRight="20dp"
app:iconifiedByDefault="false"
app:queryHint="Your hint text" />
searchView.setIconified(false);
Just write it in xml
android:iconifiedByDefault="false"
Worked for me
Write the lines of code given below in your Java file:
SearchView searchView = (SearchView) findViewById(R.id.searchView);
searchView.setQueryHint("Enter your address");
searchView.setIconified(false);
searchView.clearFocus();
After that, don't forget to write android:focusable="true" in your XML file inside SearchView tag.
For example:
<SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true" />
Use This
searchView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
searchView.onActionViewExpanded();
OR
searchView.setIconified(false);
}
});
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View view, boolean b)
{
if(!b)
{
if(searchView.getQuery().toString().length() < 1)
{
searchView.setIconified(true); //close the search editor and make search icon again
OR
searchView.onActionViewCollapsed();
}
searchView.clearFocus();
}
}
});
in code:
private void initSearchView(View layout) {
// Locate the EditText in listview_main.xml
searchInput = (SearchView) layout.findViewById(R.id.search);
//searchInput.onActionViewExpanded();//force show keyboard at start
//==>region to enable search after click in input-text(not only at magnifier icon)
searchInput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchInput.onActionViewExpanded();
}
});
searchInput.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (!b) {
if (searchInput.getQuery().toString().length() < 1) {
searchInput.setIconified(true);
}
searchInput.clearFocus();
}
}
});
//endregion
}
and in my layout:
<androidx.appcompat.widget.SearchView
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar_main"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="2dp"
android:background="#drawable/search_border"
android:inputType="text"
android:layoutDirection="ltr"
android:searchSuggestThreshold="2"
app:queryHint="#string/search"
app:searchHintIcon="#null" />
Use the below code.
<androidx.appcompat.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/earningSearchView"
app:queryHint="Search"
app:iconifiedByDefault="false"
app:queryBackground="#android:color/transparent"/>
So recently I've had to do this and I tried all the answers provided but each had some dodgy behaviour - for example the x close/erase all button would only show up if you'd clicked on the search icon, otherwise you'd be able to edit etc. but you would only see the x as an erase all button.
Looking at the SearchView's code I noticed that clicking the mSearchButton calls onSearchClicked() and not the suggested onActionViewExpanded(), but the former is a package private function so it can't be called directly. So I came up with this:
private val searchButton by lazy { searchView.findViewById<ImageView>(R.id.search_button) }
searchView.setOnClickListener { searchButton.callOnClick() }
This way you get the same behaviour no matter where you click and you don't need to manually set the iconified property in either the xml or programatically.
What you ask can be done as follows
For the first click in the bar we use the method onClick of the SearchView to expand it.
public void onClick(View v) {
SearchView searchView = (SearchView) v;
searchView.setIconified(false);
}
When it doesn't have the focus then we collapse the SearchView.
Also for the next clicks on the bar, onClick just doesn't work, so we used onFocusChange to expand SearcView too.
final SearchView searchView = findViewById(R.id.searchView);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
searchView.setIconified(false);
}
if (!b) { // Not have focus
if (searchView.getQuery().toString().length() < 1) {
searchView.setIconified(true);
}
searchView.clearFocus();
}
}
});

How to implement actionbarsherlock

I am trying to use actionbar sherlock
I have implemented a sample project using actionbarsherlock
MainActivity.java
public class MainActivity extends SherlockActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// First Menu Button
menu.add("Help")
.setOnMenuItemClickListener(this.HelpButtonClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
// Second Menu Button
menu.add("Like")
.setOnMenuItemClickListener(this.LikeButtonClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
// Third Menu Button
menu.add("Exit")
.setOnMenuItemClickListener(this.ExitButtonClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return super.onCreateOptionsMenu(menu);
}
}
activity_main.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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/ActionBarSherlock" />
</RelativeLayout>
My Present output:: Sherlockbar is at bottom
What i am trying to do :: 2 sherlock bars one at top and one at bottom. both must be different bars
so that i can use them for different listeners
Is it possible ?
How to implement this !
You can implement only one action bar. There is no way to implement two action bar. Except when you split action bar. Refer this link to learn more about splitting action bar.

Search Edittext in Sherlock Acionbar

I have a problem with search view in sherlock actionbar so i decided to use edit text instead. but when I want to use edit text my app crash (android 2.3). how could I use search edittext or searchview in sherlock activity for android 2.3?
here is my onCreateOptionsMenu function:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater minflater = getSupportMenuInflater();
minflater.inflate(R.menu.activity_main, menu);
View v = (View) menu.findItem(R.id.search).getActionView();
txtSearch = ( EditText ) v.findViewById(R.id.txt_search);
txtSearch.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
return false;
}
});
return true;
}
and this is my edittext xml source:
<EditText
android:id="#+id/txt_search"
android:inputType="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:hint="#string/hint"
android:imeOptions="actionDone"
android:textColor="#android:color/black"
android:textCursorDrawable="#android:color/black"
/>
and my search menu item:
<item
android:id="#+id/search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="#layout/search_layout"
/>
If anyone knows a way to use searchview in sherlock activity or use edittext for search in sherlock actionbar, notice me.
You should use
android:actionLayout="#layout/search_layout"
More info here: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
work for me:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.your_menu, menu);
// "0" is the number of menu item search in my example
menu.getItem(0).setActionView(R.layout.your_layout_search); //your layout with edittext
EditText editSearch = (EditText) menu.getItem(0).getActionView().findViewById(R.id.edit_search);
return super.onCreateOptionsMenu(menu);
}

Categories

Resources