Material SearchView get url query - java

I was using traditional searchview, but I decided to use the material searchview (Google), but I am not able to apply the old rule that I used using webview.
This is my code I was using:
#SuppressLint("RestrictedApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
if(menu instanceof MenuBuilder){
MenuBuilder menuBuilder = (MenuBuilder) menu;
menuBuilder.setOptionalIconsVisible(true);
}
MenuItem searchItem = menu.findItem(R.id.search);
final SearchView searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchItem);
searchViewAndroidActionBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchViewAndroidActionBar.clearFocus();
view.loadUrl("https://br-androidtech.blogspot.com/search?q="+query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
This is the layout scheme:
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:titleCentered="true">
<com.google.android.material.search.SearchBar
android:id="#+id/searchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/app_name"
android:theme="#style/Platform.MaterialComponents.Light"/>
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.search.SearchView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/search"
app:layout_anchor="#id/searchBar">
</com.google.android.material.search.SearchView>
Can anybody help me?
Sorry for my bad English...

Related

How to change the background of the Search View in the Appbar in Android?

I want to change the background of the SearchView in the Appbar which is available default in Android using Java.
The below image shows how my current app bar looks like:
The below image shows what kind of SearchView background I want to achieve (Something similar to this):
The below is the code that I have for the search bar:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
adapter.getFilter2().filter(newText);
return false;
}
});
The xml code is:
<androidx.appcompat.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#1F2D78"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#color/white">
Please help me to figure out how to achieve this
Instead of using menu search view you can use custom edittext inside toolbar.
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
app:elevation="0dp">
<com.google.android.material.appbar.MaterialToolbar
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/white"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="#+id/btnBack"
android:layout_width="22dp"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:layout_marginStart="15dp"
android:src="#drawable/ic_back" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/searchContainer"
android:layout_weight="1"
app:endIconMode="none"
android:layout_width="0dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edtSearch"
android:drawableStart="#drawable/ic_search"
android:drawablePadding="3dp"
android:hint="Search.."
android:paddingStart="7dp"
android:paddingEnd="7dp"
android:inputType="textPersonName"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>
and you can customize your TextInputLayout

Using animations on items

I have two problems with the following code. This code is supposed to provide an overflow menu with three options, left, right and down. They must move the button accordingly with default animations. The first problem is that the TransitionManager doesn't work at all. The button moves with no animation whatsoever. And the second problem is, when the button moves to left and right, it gets lost behind action bar, what can I do about it?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Button button = (Button)findViewById(R.id.button);
RelativeLayout main_view = (RelativeLayout) findViewById(R.id.main_view);
switch(item.getItemId()){
case R.id.right:
if(item.isChecked()){
item.setChecked(false);
}
else
item.setChecked(true);
TransitionManager.beginDelayedTransition(main_view);
RelativeLayout.LayoutParams positionRulesRight = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRulesRight.addRule(RelativeLayout.ALIGN_PARENT_TOP);
positionRulesRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(positionRulesRight);
return true;
case R.id.left:
if(item.isChecked()){
item.setChecked(false);
}
else
item.setChecked(true);
TransitionManager.beginDelayedTransition(main_view);
RelativeLayout.LayoutParams positionRulesLeft = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRulesLeft.addRule(RelativeLayout.ALIGN_PARENT_TOP);
positionRulesLeft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
button.setWidth(250);
button.setHeight(250);
button.setLayoutParams(positionRulesLeft);
return true;
case R.id.down:
if(item.isChecked()){
item.setChecked(false);
}
else
item.setChecked(true);
TransitionManager.beginDelayedTransition(main_view);
RelativeLayout.LayoutParams positionRulesDown = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRulesDown.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
positionRulesDown.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(positionRulesDown);
return true;
default:
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: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="com.revolise.animations.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"
android:id="#+id/include" />
<Button
android:id="#+id/button"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="70dp"
android:text="#string/button_text"/>
<RelativeLayout
android:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</RelativeLayout>

Implementing searchview custom toolbar

I have a toolbar that i created by myself, a custom toolbar and want to place a searchview above so everytime i click the icon it replaces my toolbar.
My Custom toolbar:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/emerald">
<ImageView
android:id="#+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="0"
android:src="#drawable/ic_002_keyboard_left_arrow_button"
android:onClick="previousActivity"
android:tint="#color/white"/>
<com.example.afcosta.inesctec.pt.android.Helpers.NexusBoldTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:layout_gravity="left"
android:layout_marginLeft="25dp"
android:id="#+id/toolbar_title"
android:textSize="#dimen/toolbar_title" />
<ImageView
android:id="#+id/user_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:src="#drawable/ic_user"
android:onClick="userProfile"
android:layout_marginRight="25dp"
android:tint="#color/white"/>
<ImageView
android:id="#+id/user_ic2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:src="#drawable/ic_search_black_24dp"
android:tintMode="#color/white"
android:onClick="userProfile"
android:layout_marginRight="25dp"
android:tint="#color/white"/>
</android.support.v7.widget.Toolbar>
i include it on other activities like this:
<include
layout="#layout/custom_toolbar"
android:id="#+id/my_toolbar"
tools:layout_constraintTop_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />
now i want to use the searchview on this custom toolbar, but i can't even add it, any help?
Create a menu with item
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
In your activity
#Override
public boolean onCreateOptionsMenu( Menu menu) {
getMenuInflater().inflate( R.menu.menu_main, menu);
MenuItem search = menu.findItem( R.id.action_search);
final SearchView searchView = (SearchView) search.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//Do your action
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}

With SearchView (v7) keyboard is always visible?

I'm using SearchView in my activity and I see that when I change activity and then I return to the Searchview activity the keyboard opens by itself.
Here is my code:
final android.widget.SearchView msearchView = (android.widget.SearchView) findViewById(R.id.search_frontend);
msearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
...
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
How can I do to start the activity with hidden keyboard?
Edit: I don't want it iconized by default
Try adding these attributes in the layout tag.
android:focusable="true"
android:focusableInTouchMode="true"
I solved it setting:
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
android:id="#+id/search_frontend"
android:iconifiedByDefault="false"
android:focusable="false"
android:focusableInTouchMode="true"
android:queryHint="Search..." />

NullPointerException in android while using actionLayout in menu.xml

I want to use custom SearchView in menu of my App but I'm encountering a NullPointerException in android while using actionLayout in menu.xml
I have custom layout for menu :
<?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" >
<Button
android:id="#+id/search_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:drawable/ic_menu_search"/>
<EditText
android:id="#+id/search_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/search_btn"
android:layout_toLeftOf="#+id/search_btn"
android:ems="10"
android:inputType="none" >
<requestFocus />
</EditText>
and my menu.xml is :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search_view"
android:icon="#android:drawable/ic_menu_search"
android:actionLayout="#layout/search_menu"
android:showAsAction="collapseActionView|ifRoom"
android:title="#string/search_title"/>
</menu>
Now I want to add OnClickListener on _search_btn_ So I did that likewise :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
searchButton = (Button) menu.findItem(R.id.search_btn);
searchButton.setOnClickListener(new OnClickListener() { // SEE HERE I'M GETTING NullPointerException
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, ((EditText) findViewById(R.id.search_et)).getText().toString(), Toast.LENGTH_LONG).show();
}
});
return true;
}
but I'm getting my NullPointerException on above mentioned line. How can I add ClickListener to that button???
You're using the wrong id. Use the id for your menu item. You can use getActionView on the MenuItem to get the layout.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.search_view);
Button searchButton = searchItem.getActionView().findViewById(R.id.search_btn);
searchButton.setOnClickListener(new OnClickListener() { // SEE HERE I'M GETTING NullPointerException
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, ((EditText) findViewById(R.id.search_et)).getText().toString(), Toast.LENGTH_LONG).show();
}
});
return true;
}
please try on onOptionsItemSelected method.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.search_view:
//write your code here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Well your not getting a reference to your button. i.e in searchButton = (Button) menu.findItem(R.id.search_btn); you searchButton is null.
Your using a wrong id.

Categories