original source : https://stackoverflow.com/questions/31489109/android-searchview-in-toolbar
menu.xml – (res
-> menu
)
<menu 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"
tools:context=".HomeActivity">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
MainFragment.java
public class MainFragment extends Fragment {
private SearchView searchView = null;
private SearchView.OnQueryTextListener queryTextListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
Log.i("onQueryTextChange", newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
Log.i("onQueryTextSubmit", query);
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
}
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
// Not implemented here
return false;
default:
break;
}
searchView.setOnQueryTextListener(queryTextListener);
return super.onOptionsItemSelected(item);
}
}
Done
Sending an Intent to browser to open specific URL
original source : https://stackoverflow.com/questions/3004515/sending-an-intent-to-browser-to-open-specific-url
To open a URL/website you do the following:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Here’s the documentation of Intent.ACTION_VIEW
.
Making a LinearLayout act like an Button
original source : https://stackoverflow.com/questions/5546514/making-a-linearlayout-act-like-an-button
If you want add the Android default background behavior to make a Layout
acts like a “clikable” View
, set on the targeted Layout
:
API 11+ (Pure Android):
android:background="?android:attr/selectableItemBackground"
API 7+ (Android + AppCompat Support Library):
android:background="?attr/selectableItemBackground"
Any API:
android:background="@android:drawable/list_selector_background"
Answers above still true but didn’t help me for just add the default pressed and released UI state (like in a ListView
for instance).
https://stackoverflow.com/questions/5546514/making-a-linearlayout-act-like-an-button
I ran into this problem just now. You’ll have to set the LinearLayout to clickable. You can either do this in the XML with
android:clickable="true"
Or in code with
yourLinearLayout.setClickable(true);
Cheers!
What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?
original source : https://stackoverflow.com/questions/4905370/what-are-the-differences-between-linearlayout-relativelayout-and-absolutelayou
LinearLayout
means you can align views one by one (vertically/ horizontally).
RelativeLayout
means based on relation of views from its parents and other views.
ConstraintLayout
is similar to a RelativeLayout in that it uses relations to position and size widgets, but has additional flexibility and is easier to use in the Layout Editor.
WebView
to load html, static or dynamic pages.
FrameLayout
to load child one above another, like cards inside a frame, we can place one above another or anywhere inside the frame.
deprecated – AbsoluteLayout
means you have to give exact position where the view should be.
For more information, please check this address http://developer.android.com/guide/topics/ui/layout-objects.html