Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / Manifest.xml
Last active July 6, 2017 20:15
Prevent window from resizing when the sof keyboard is shown
<activity
android:windowSoftInputMode="adjustPan">
</activity>
@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 / 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 / xmlns.xls
Created July 8, 2017 12:59
xmlns:tools
xmlns:tools="http://schemas.android.com/tools"
@Tarelochkin
Tarelochkin / DateFormat.java
Last active July 15, 2017 18:29
Format date with a pattern: 2017-04-12T05:41:46Z for Medium UK locale
String dateString = "2017-04-12T05:41:46Z"
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
Date convertedDate;
String formattedDate = "";
try {
convertedDate = sourceFormat.parse(dateString);
//will return "12 Apr 2017"
formattedDate = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.UK).format(convertedDate);