I've noticed that when I'm using action bar in my apps, there is a blank screen showing for couple of seconds when I start the app, then the activity layout is loaded. Does anyone know how can I resolve this?
If I dont use action bar this blank scren is now showing
Here is an example acyivity that I have:
public class Login extends Activity implements OnClickListener {
private Button signInFacebook;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
initWidgets();
}
private void initWidgets() {
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.nav_bg));
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
signInFacebook = (Button) findViewById(R.id.login_fb);
signInFacebook.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.login_fb:
intent = new Intent(this, ChangePassword.class);
startActivity(intent);
break;
}
}
}
I dont get any erros or warnings
Just add this line in your launcher activity
android:theme="#android:style/Theme.NoTitleBar" like below example
<activity
android:name="com.example.app.Login"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Use requestWindowFeature
public class Login extends Activity implements OnClickListener {
private Button signInFacebook;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
initWidgets();
}
private void initWidgets() {
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.nav_bg));
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
signInFacebook = (Button) findViewById(R.id.login_fb);
signInFacebook.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.login_fb:
intent = new Intent(this, ChangePassword.class);
startActivity(intent);
break;
}
}
}
Hope it is useful.
For Proper action bar with all functionality like search you can have the below code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.search_action_provider);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
MenuItemCompat.setOnActionExpandListener(searchItem, new OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
});
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
if (adapter != null) {
adapter.getFilter().filter(newText);
}
return true;
}
public boolean onQueryTextSubmit(String query) {
if (adapter != null) {
adapter.getFilter().filter(query);
}
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
In serch.xml you can have
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/search_action_provider"
android:icon="#drawable/abc_ic_search_api_holo_light"
android:title="#string/abc_action_bar_home_description"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
yourapp:showAsAction="ifRoom|collapseActionView"/>
Related
I am trying to implement tab functionality in an Android browser. I tried to save the webpage in a cache, but I can't figure out how to save multiple of them and load them when the user switches tabs. How could I implement this?
I would like to save the current webpage in a cache when the user wants to add a new tab and open Main_activity, and then next time when the user switches tabs, the current webpage should get saved in a cache, and the intended webpage of the intended tab gets loaded in the webview. Now the issue is, I don't know how to handle multiple webpages in a cache, and select which one to load when.
My code consists of two activities, main_activity with some icons and webView_activity with a simple webview. I am using a custom dialog box with a ListView in it, so I will need to add new rows for new tabs and edit them when tabs are switched.
Here is my main_activity
public class MainActivity extends AppCompatActivity {
ImageView yt,google,gm,twitter;
SearchView g_search;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g_search = (SearchView) findViewById(R.id.g_search);
g_search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("query",query);
startActivity(i);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
yt = (ImageView) findViewById(R.id.yt);
google = (ImageView) findViewById(R.id.google);
gm = (ImageView) findViewById(R.id.gm);
twitter = (ImageView) findViewById(R.id.twitter);
yt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("shortcut","https://www.youtube.com");
startActivity(i);
}
});
google.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("shortcut","https://www.google.com");
startActivity(i);
}
});
gm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("shortcut","https://www.gmail.com");
startActivity(i);
}
});
twitter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("shortcut","https://www.twitter.com");
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.m1, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Intent i = new Intent(MainActivity.this,webview1.class);
i.putExtra("query",query);
startActivity(i);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_new_tab){
}
if(item.getItemId()==R.id.action_incognito_mode){
}
if(item.getItemId()==R.id.action_bookmarks){
}
if(item.getItemId()==R.id.action_history){
}
if(item.getItemId()==R.id.action_downloads){
}
if(item.getItemId()==R.id.action_settings){
Intent i = new Intent(MainActivity.this,SettingsActivity.class);
startActivity(i);
}
if(item.getItemId()==R.id.action_about){
Intent i = new Intent(MainActivity.this,about.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
boolean doubleBackToExitPressedOnce = false;
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
}
Here is my web_view activity
public class webview1 extends AppCompatActivity {
CustomWebView web1;
String search_q,shortcut;
FloatingActionButton fab;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview1);
web1 = (CustomWebView) findViewById(R.id.web1);
web1.setGestureDetector(new GestureDetector(new CustomGestureDetector()));
web1.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(final WebView view, int errorCode, String description,
final String failingUrl) {
fragmentManager.beginTransaction()
.replace(R.id.container, new blankState())
.commit();
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
registerForContextMenu(web1);
// web1.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
web1.getSettings().setJavaScriptEnabled(true);
web1.getSettings().setBuiltInZoomControls(true);
web1.getSettings().setDisplayZoomControls(false);
web1.getSettings().setLoadWithOverviewMode(true);
web1.getSettings().setUseWideViewPort(true);
search_q=getIntent().getStringExtra("query");
shortcut=getIntent().getStringExtra("shortcut");
bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_toolbar);
ImageView action_back = (ImageView) findViewById(R.id.action_back);
action_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
bottomNavigationView.setVisibility(GONE);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// code for launching new tab here
}
});
web1.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress ){
frame_layout.setVisibility(View.VISIBLE);
progress_bar.setProgress(progress);
if(progress==100){
frame_layout.setVisibility(GONE);
srl.setRefreshing(false);
}
super.onProgressChanged(view, progress);
}
});
progress_bar.setProgress(0);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_url){
ShowUrlDialog();
return true;
}
if(item.getItemId()==R.id.action_home){
Intent i = new Intent(webview1.this,MainActivity.class);
startActivity(i);
}
if(item.getItemId()==R.id.action_new_tab){
// code to launch new tab here
}
if(item.getItemId()==R.id.action_incognito_mode){
}
if(item.getItemId()==R.id.action_bookmarks){
}
if(item.getItemId()==R.id.action_history){
}
if(item.getItemId()==R.id.action_downloads){
}
if(item.getItemId()==R.id.action_settings){
Intent i = new Intent(webview1.this,SettingsActivity.class);
startActivity(i);
}
if(item.getItemId()==R.id.action_about){
Intent i = new Intent(webview1.this,about.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.m2, menu);
// Associate searchable configuration with the SearchView
return true;
}
#Override
public void onBackPressed() {
if (web1.canGoBack()) {
web1.goBack();
} else {
super.onBackPressed();
}
}
}
I have searchView on actionbar and I here is codes for it
public class container extends AppCompatActivity {
private ListView drawerList;
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private ActionBarDrawerToggle toggle;
private TextView textContainer, titleContainer;
private ImageView imageContainer;
private drawerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
toolbar = (Toolbar) findViewById(R.id.mainToolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(0xffffffff);
titleContainer = (TextView) findViewById(R.id.containerTitle);
textContainer = (TextView) findViewById(R.id.textContainer);
imageContainer = (ImageView) findViewById(R.id.imageContainer);
titleContainer.setText(R.string.UtrujjTitle);
textContainer.setText(R.string.UtrujjContent);
imageContainer.setImageResource(R.drawable.utrujj);
final Animation animation = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
drawerLayout = (DrawerLayout) findViewById(R.id.mainDrawer);
drawerList = (ListView) findViewById(R.id.drawerList);
final SearchView searchView = (SearchView)findViewById(R.id.searchable);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
Spannable spannable = new SpannableString(newText);
spannable.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 100, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textContainer.setText(spannable);
}
return false;
}
});
and in the end I added these overrides
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.searchable:
return false;
default:
break;
}
return super.onOptionsItemSelected(item);
}
search.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/setting"
android:icon="#drawable/ic_setting"
app:showAsAction="ifRoom"
android:title="#string/setting"/>
<item android:id="#+id/searchable"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/search"/>
</menu>
when activity start I get this error in logcat
Unable to start activity ComponentInfo{com.example.tina.tibbenabvi/com.example.tina.tibbenabvi.container}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListener)' on a null object reference
====================================================
This view It's a menu item so you should initialize onCreateOptionsMenu().
Example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
Spannable spannable = new SpannableString(newText);
spannable.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 100, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textContainer.setText(spannable);
}
return false;
}
});
return true;
}
EDIT
Manifest file:
<activity ... >
...
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Create res/xml/searchable.xml file:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="search..." />
Create a Searchable Activity
public class SearchResultsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
...
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
...
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
}
...
}
And add it in the manifest file:
<activity android:name=".SearchResultsActivity" ... >
...
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
...
</activity>
Info from:
http://developer.android.com/intl/es/training/search/setup.html
I have been trying to redirect my searchview results to another activity upon onquerysubmit but the app just shows me a blank page. I have been trying to debug it but after the onquerysubmit returns false, the debugger just keeps on displaying that the app is running.
Here is my AndroidManifest.xml
<activity
android:name=".UniversalSearchActivity"
android:label="#string/title_activity_universal_search"
android:theme="#style/AppTheme"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name=".UniversalSearchResultActivity"
android:label="#string/title_activity_report_error"
android:parentActivityName=".UniversalSearchActivity"
android:theme="#style/AppTheme">
</activity>
UniversalSearchActivity.java
public class UniversalSearchActivity extends AppCompatActivity {
private RelativeLayout searchSuggestionsLayout;
private ListView searchListView;
private SearchView searchView;
private UniversalSearchAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_universal_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar_universal_search);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
searchSuggestionsLayout = (RelativeLayout) findViewById(R.id.search_suggestions_container);
searchListView = (ListView) findViewById(R.id.search_suggestions_list);
adapter = new UniversalSearchAdapter(this, this.getSearch());
searchListView.setAdapter(adapter);
searchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You clicked an item in the list view!",
Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
searchItem.expandActionView();
SearchManager searchManager =
(SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setSearchableInfo(
searchManager.getSearchableInfo(new ComponentName(this,
UniversalSearchResultActivity.class)));
searchView.setQueryHint(getString(R.string.placeholder_universal_search));
searchView.setSubmitButtonEnabled(true);
searchView.setIconified(false);
searchView.requestFocus();
menu.add("Search")
.setIcon(R.drawable.ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
if(query == "") {
Toast.makeText(getApplicationContext(), "Please type a hint", Toast.LENGTH_SHORT)
.show();
} else {
Intent intent = new Intent(getApplicationContext(),
UniversalSearchResultActivity.class);
startActivity(intent);
}
return false;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
public ArrayList<UniversalSearch> getSearch() {
ArrayList<UniversalSearch> searches = new ArrayList<UniversalSearch>();
UniversalSearch universalSearch;
for (int i=0; i<names.length; i++) {
universalSearch = new UniversalSearch(names[i], address[i], images[i]);
searches.add(universalSearch);
}
return searches;
}
}
UniversalSearchResultActivity.java
public class UniversalSearchResultActivity extends AppCompatActivity {
private RelativeLayout searchResultLayout;
private ListView searchResultListView;
private UniversalSearchAdapter universalSearchAdapter;
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_universal_search_result);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar_universal_search_result);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Toast.makeText(getApplicationContext(), "I'm heeeere!!!", Toast.LENGTH_SHORT).show();
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Intent resultIntent = new Intent(getApplicationContext(),
UniversalSearchResultActivity.class);
resultIntent.putExtra(SearchManager.QUERY, query);
}
}
}
The searchview works fine when the onquerytextchange is used but in the case of onquerytextsubmit, it doesn't. Please tell me what I am doing wrong. Thank you!
how to save the text given to edit text, after clicking the save action button in action bar that should save to another activity.
my first activity Add.java
public class Add extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflate=getMenuInflater();
getMenuInflater().inflate(R.menu.activity_add_action, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:save();
return true;
case R.id.cancel:cancelnavi();
return true;
}
return super.onOptionsItemSelected(item);
}
private void save() {
EditText titlename;
titlename=(EditText)findViewById(R.id.edittitle);
String title=titlename.getText().toString();
if (title.equalsIgnoreCase("")){
Toast.makeText(Add.this, "Script title should not be empty", Toast.LENGTH_LONG).show();
} else {
Intent i;
i=new Intent(getApplicationContext(), Scripts.class);
i.putExtra("n", titlename.getText().toString());
startActivityForResult(i, 0);
titlename.setText("");
}
}
}
It should be saved to scripts.java
public class Scripts extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scripts);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflate=getMenuInflater();
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:editscript();
break;
}
return true;
}
private void editscript() {
Intent editinIntent;
editinIntent=new Intent(Scripts.this, Edit.class);
startActivity(editinIntent);
}
}
Maybe this can help you.
public class Scripts extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scripts);
getActionBar().setDisplayShowHomeEnabled(false);
//Extract the data
String yourTitle = getIntent().getStringExtra("n");
}
}
I have few activities in my app which are meant to perform some specific tasks.
I would like to have a search features on almost all of these activities.
My design is as below:
Activity1: With SearchView widget in Actionbar
Activity2: With SearchView widget in Actionbar
SearchActivity: Which perform the search and display results in ListView.
I have done necessary configuration in my manifest file for the SearchActivity.
When I touch on the search icon(magnifying glass), the SearchView gets expanded to allow user to enter the query,then user trigger the search and search intent got delivered to the SearchActivity.
Since the user originally entered the search query on Activity, I don't see the query and SearchView in my SearchActivity (this is basically required to allow user to perform further searches).
I understand that to do so I will also need a SearchView in my SearchActivity too, but How to pre-populate it with the query entered in previous activity and show in expanded state ?
I usually do things like that (plus hving the adapter implements Filterable):
public class ActivityFiltrable extends SherlockFragmentActivity implements OnQueryTextListener{
protected String _searchCurrentQuery;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity);
// ABS
ActionBar bar = getSupportActionBar();
bar.setTitle(R.string.tle_search);
bar.setDisplayHomeAsUpEnabled(true);
//DO STUFF
super.onCreate(savedInstanceState);
}
#Override
protected void onDestroy() {
closeKeyboard(this);
super.onDestroy();
}
#Override
public void onBackPressed() {
finish();
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
getSupportMenuInflater().inflate(R.menu.action_bar_search, menu);
// SEARCH MENUITEM
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return super.onPrepareOptionsMenu(menu);
}
public static void closeKeyboard(Context context) {
try {
InputMethodManager inputManager = (InputMethodManager) ((SherlockFragmentActivity) context).getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(((SherlockFragmentActivity) context).getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
catch (Exception e) {
// TODO:ERROR CLOSING KEYBOARD
}
}
/**
* SEARCH
*/
#Override
public boolean onQueryTextSubmit(String query) {
searchAction(query);
closeKeyboard(this);
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
searchAction(newText);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (item.getItemId() == android.R.id.home) {
finish();
// overridePendingTransition(R.animator.anim_left, R.animator.anim_right);
return true;
}
break;
}
protected void searchAction(String query) {
_searchCurrentQuery = query.toString();
EtablissementApplication._adapterFilterSearch.getFilter().filter(_searchCurrentQuery);
}
}