-
-
Save sreereddymenon/73a927076cf69ea06a40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<activity android:name=".SearchableActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<action android:name="android.intent.action.SEARCH" /> | |
</intent-filter> | |
<!-- note: this is needed to show a keyboard with proposals --> | |
<meta-data | |
android:name="android.app.searchable" | |
android:resource="@xml/searchable" /> | |
</activity> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!-- in src/xml --> | |
<!-- label is used in Quick Search Box | |
see http://developer.android.com/guide/topics/search/search-dialog.html#InvokingTheSearchDialog | |
--> | |
<searchable xmlns:android="http://schemas.android.com/apk/res/android" | |
android:label="@string/app_name" | |
android:hint="Search Mikes stuff" > | |
</searchable> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.siobra.mikesgreatandroid4app.view; | |
import android.app.Activity; | |
import android.app.SearchManager; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.CompoundButton; | |
import android.widget.LinearLayout; | |
import android.widget.SearchView; | |
import android.widget.Switch; | |
import android.widget.TextView; | |
import java.lang.reflect.Field; | |
import de.siobra.mikesgreatandroid4app.R; | |
/** | |
* extended SearchView with a toggle to the right, behaving like iOS toggle | |
* Created by mike on 11.09.13. | |
*/ | |
public class SearchViewWithToggle extends SearchView { | |
private static final String TAG = SearchViewWithToggle.class.getSimpleName(); | |
private Switch searchSwitch; | |
private TextView leftLabel; | |
private TextView rightLabel; | |
public SearchViewWithToggle(Context context) { | |
super(context); | |
} | |
public SearchViewWithToggle(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public void initSearchView(SearchManager searchManager, Activity activity, final CompoundButton.OnCheckedChangeListener onCheckCallback) { | |
SearchViewWithToggle searchView = this; | |
searchView.setSearchableInfo(searchManager.getSearchableInfo(activity.getComponentName())); | |
//use whole width | |
searchView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); | |
View searchToggleLayout = activity.getLayoutInflater().inflate(R.layout.search_toggle, null); | |
searchView.getSearchBarLayout().addView(searchToggleLayout); | |
searchSwitch = (Switch) searchToggleLayout.findViewById(R.id.searchSwitch); | |
leftLabel = (TextView) searchToggleLayout.findViewById(R.id.leftLabel); | |
leftLabel.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
searchSwitch.setChecked(false); | |
} | |
}); | |
rightLabel = (TextView) searchToggleLayout.findViewById(R.id.rightLabel); | |
rightLabel.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
searchSwitch.setChecked(true); | |
} | |
}); | |
updateLabels(searchSwitch.isChecked()); | |
searchSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { | |
updateLabels(checked); | |
if (null != onCheckCallback) | |
onCheckCallback.onCheckedChanged(compoundButton, checked); | |
} | |
}); | |
} | |
private void updateLabels(boolean checked) { | |
final int uncheckedColor = getResources().getColor(R.color.textdark_gray); | |
final int checkedColor = getResources().getColor(R.color.textlight_color); | |
boolean left = !checked; | |
if (left) { | |
leftLabel.setTextColor(checkedColor); | |
rightLabel.setTextColor(uncheckedColor); | |
} else { | |
leftLabel.setTextColor(uncheckedColor); | |
rightLabel.setTextColor(checkedColor); | |
} | |
} | |
public LinearLayout getSearchBarLayout() { | |
try { | |
Field searchField = SearchView.class.getDeclaredField("mSearchEditFrame"); | |
searchField.setAccessible(true); | |
searchField = SearchView.class.getDeclaredField("mSearchEditFrame"); | |
searchField.setAccessible(true); | |
LinearLayout searchEditFrame = (LinearLayout) searchField.get(this); | |
return searchEditFrame; | |
} catch (NoSuchFieldException e) { | |
Log.e(TAG, e.getMessage(), e); | |
} catch (IllegalAccessException e) { | |
Log.e(TAG, e.getMessage(), e); | |
} | |
throw new IllegalStateException(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment