With SearchView (v7) keyboard is always visible? - java

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..." />

Related

Material SearchView get url query

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...

How do i make the search icon in the SearchView widget to align to the right end of the layout?

How do i make the search icon in the SearchView widget to align to the right end of the layout ?
Currently it shows at the leftmost end.
in a main activity xml layout i have
<SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:elevation="2dp"
android:background="#fff"
android:layout_height="wrap_content"
>
</SearchView>
main activity
public class MainActivity extends AppCompatActivity {
SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchView=(SearchView) findViewById(R.id.searchView);
searchView.setQueryHint("Search View");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getBaseContext(), query, Toast.LENGTH_LONG).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(getBaseContext(), newText, Toast.LENGTH_LONG).show();
return false;
}
});
}
You can do that programmatically in this way
SearchView searchView= (SearchView) item.getActionView();
searchView.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));
or in Xml you need to change the view Hierarchy
<RelativeLayout
android:id="#+id/not"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.SearchView
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>

ViewFlipper automatic slideshow without button

Could anyone please help me with the below java code as I want to create a image slideshow without a click of a button. I want the view flipper to automatically switch through the different iamges without a click of a button. I want it to keep on showing all the images again and again, without a click of a button. I have deleted the button in my XML file as I dont require it.
Java File Code
public class MainActivity extends Activity {
int mFlipping = 0 ; // Initially flipping is off
Button mButton ; // Reference to button available in the layout to start and stop the flipper
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Click event handler for button */
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.flipper1);
if(mFlipping==0){
/** Start Flipping */
flipper.startFlipping();
mFlipping=1;
mButton.setText(R.string.str_btn_stop);
}
else{
/** Stop Flipping */
flipper.stopFlipping();
mFlipping=0;
mButton.setText(R.string.str_btn_start);
}
}
};
/** Getting a reference to the button available in the resource */
mButton = (Button) findViewById(R.id.btn);
/** Setting click event listner for the button */
mButton.setOnClickListener(listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
XML File
<ViewFlipper
android:id="#+id/flipper1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:flipInterval="3000"
android:inAnimation="#android:anim/slide_in_left"
android:outAnimation="#android:anim/slide_out_right"
android:layout_centerInParent="true"
>
<ImageView
android:src="#drawable/img1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img1"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img2"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img3"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/str_img4"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img5"
android:layout_gravity="center_horizontal"
/>
</ViewFlipper>
</RelativeLayout>
For(int mflipping:0;mflipping less than total images-1 length; mflipping++)
Curly brases starts
Flipper.startflipping();
//Delay for sometime
Curly Brases End
Flipper.stopflipping();
Sodu code,write your code now
Just remove button listener and try.
public class MainActivity extends Activity {
int mFlipping = 0 ; // Initially flipping is off
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.flipper1);
if(mFlipping==0){
/** Start Flipping */
flipper.startFlipping();
mFlipping=1;
}
else{
/** Stop Flipping */
flipper.stopFlipping();
mFlipping=0;
}
}
};
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

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.

How can I change the content of a single WebView (Android) depending on which button the user clicks (out of three) in one activity?

Hi I am new to Android development and StackOverflow.
I have an Android WebView application in which a 'starter activity' (just intro text and a button to start main intent) starts my WebView activity, which loads a single URL (this is in the onCreate method).
In this main activity, I have three buttons, one of which opens the same URL in the current activity (effectively refreshing the page). The other two buttons I would like to open two different URLs in the same WebView activity, and I was wondering if/how this is possible.
Here is the main ReadWebpageAsyncTask.java activity code:
public class ReadWebpageAsyncTask extends Activity {
private WebView browser;
protected String urlStrOne = "url one";
protected String urlNewCourses = "url two";
protected String urlFutureCourses = "url three";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_webpage_async_task);
browser = (WebView)findViewById(R.id.WebView01);
browser.setWebViewClient(new MyBrowser());
browser.loadUrl(urlStrOne);
}
#SuppressLint("SetJavaScriptEnabled")
public void onClick(View view){
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(urlStrOne);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView browser, String urlStrOne) {
browser.loadUrl(urlStrOne);
return true;
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(browser.canGoBack()) {
browser.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
And the corresponding XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<Button
android:id="#+id/readWebpage"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/soon"
android:background="#+drawable/custom_button_one"
android:onClick="onClick"
android:text="#string/load_page" />
<Button
android:id="#+id/exitapp01"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/soon"
android:background="#+drawable/custom_button_one"
android:onClick="exitButton"
android:text="#string/exit_button" />
<Button
android:id="#+id/soon"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toLeftOf="#+id/exitapp01"
android:layout_toRightOf="#+id/readWebpage"
android:layout_alignParentTop="true"
android:onClick="comingSoon"
android:text="#string/future_courses" />
</RelativeLayout>
<WebView
android:id="#+id/WebView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/WebView01" >
</WebView>
</LinearLayout>
P.S. Please excuse the terrible naming of everything, I wrote this a while back and haven't gotten around to changing it all.
To clarify, the main URL which is loaded first is the same as the button "readWebpage", and the other two URLs which I would like to be loaded on click are buttons "exitapp01" (what can I say, I am a noob) and "soon".
You can add OnClickListeners to your buttons and switch the url depending on the case:
public class ReadWebpageAsyncTask extends Activity implements OnClickListener{
private WebView browser;
protected String urlStrOne = "url one";
protected String urlNewCourses = "url two";
protected String urlFutureCourses = "url three";
Button reload, exit, soon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_webpage_async_task);
browser = (WebView)findViewById(R.id.WebView01);
browser.setWebViewClient(new MyBrowser());
browser.loadUrl(urlStrOne);
reload = (Button)findViewById(R.id.readWebpage);
exit = (Button)findViewById(R.id.exitapp01);
soon = (Button)findViewById(R.id.soon);
reload.setOnClickListener(this);
exit.setOnClickListener(this);
soon.setOnClickListener(this);
}
#SuppressLint("SetJavaScriptEnabled")
public void onClick(View view){
}
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.readWebpage:
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(urlStrOne);
break;
case R.id.exitapp01:
//Do whatever you want with this button
break;
case R.id.soon:
//Do whatever you want with this button
break;
}
}
}
And remove all the android:onClick="" from your xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<Button
android:id="#+id/readWebpage"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"/>
<Button
android:id="#+id/exitapp01"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/soon"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<WebView
android:id="#+id/WebView01"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
If The buttons are on Activity You have to set the OnClickListener on all the buttons and within the onClick() do like this:
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.[Your first button Id]:
{
//Functionality
}
case R.id.[Your Second button Id]:
{
//Functionality
}
case R.id.[Your first button Id]:
{
//Functionality
}
//End Switch
}
//You also need To Write implements OnClickListener at the Class Definition Of Your activity

Categories