Skip to content

Instantly share code, notes, and snippets.

@Tarelochkin
Tarelochkin / HideSoftKeyoard.java
Last active August 18, 2017 19:40
Hide the soft keyboard programmatically or alternatively in Manifest.xml
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
@Tarelochkin
Tarelochkin / Activity.java
Last active July 7, 2017 19:49
Embed search and show search the results in the same activity
public class MainActivity extends AppCompatActivity {
String mQuery;
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
@Tarelochkin
Tarelochkin / Search.java
Created July 5, 2017 20:16
Lose the default focus on a searchview on activity resume while showing the current query
if (!TextUtils.isEmpty(mQuery)) {
searchItem.expandActionView();
searchView.setQuery(mQuery, false);
searchView.clearFocus();
}
@Tarelochkin
Tarelochkin / Search.java
Created July 5, 2017 20:22
Control behaviour when menu items are expanded/collapsed
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
//your code
return true; //or false in case you don't want to expand
}
@Override
//Control the behaviour of the {@link SearchView}'s own back button
public boolean onMenuItemActionCollapse(MenuItem item) {
@Tarelochkin
Tarelochkin / JSONUtils.java
Last active July 7, 2017 19:03
Parse JSON file from a URL
public final class QueryUtils {
/**
* For every known problem with fetching data there is an error code created
* which is then used to provide a user with an idea of what's gone wrong
*/
static int errorCode;
public QueryUtils() {
@Tarelochkin
Tarelochkin / ParseJSON.java
Last active July 5, 2017 20:53
An example of parsing JSON and creating an arrayllist of custom objects
private static List<Book> getBooksInfoFromJson(String jsonStr) {
List<Book> booksList = new ArrayList<>();
if (jsonStr == null || jsonStr.equals("")) {
return booksList;
}
try {
JSONObject rootObj = new JSONObject(jsonStr);
JSONArray itemsArray = rootObj.getJSONArray("items");
@Tarelochkin
Tarelochkin / KeepListviewPosition.java
Last active July 15, 2017 18:34
Save listview position on orientation change
public class KeepListviewPosition extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Article>> {
private ListView newsView;
private static NewsAdapter mAdapter;
/**
* Parcelable variable to keep the position in the listview
*/
private Parcelable mListState;
@Tarelochkin
Tarelochkin / AsyncTaskMultiple.java
Created July 6, 2017 19:13
Create AsyncTask with multiple threads
MyAsynkTask task = new MyAsynkTask();
// Create multiple threads.
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, arg1);
@Tarelochkin
Tarelochkin / BitmapUtils.java
Created July 6, 2017 19:26
Download images from url and use them in a listview
final class BitmapUtils {
/**
* Define a size of the memory available for an LruCache object.
*/
private static int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
private static int cacheSize = maxMemory / 8;
/**
* Create the LruCache object and define its size.
@Tarelochkin
Tarelochkin / FadeIn.java
Last active July 15, 2017 18:29
Fade in for images
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein);
ImageView imageView = (ImageView) findViewById(R.id.imageViewId);
imageView.startAnimation(fadeInAnimation);