Skip to content

Instantly share code, notes, and snippets.

View francisnnumbi's full-sized avatar
😀
Learn and share

Francis Nduba Numbi francisnnumbi

😀
Learn and share
View GitHub Profile
@francisnnumbi
francisnnumbi / hidekeyboardsnippet.txt
Created August 4, 2017 07:17
Do you want to hide your soft keyboard programmatically? For example, when you click a button or when you press ENTER (for single line editText). This snippet method is all you need. Call it and pass in any view available (layout, textView, editText, button,...)
private void hideKeyboard(View v) {
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
} catch (Exception e) {}
}
@francisnnumbi
francisnnumbi / Notification.txt
Created July 20, 2017 18:31
This is a snippet of a method that enables you to show a notification with minimum requirements. 3 arguments are required: - the id that is unique, if you use same id for different notifications the previous ones will be replaced by recent ones. - title of the notification. - message to display. But remember that a notification needs an icon for…
/**
* This is the simplest method that enables you to pop a notification with a minimum requirement
* Remember to add an icon as an icon or a drawable
*/
public void popNotification(int notifId, String title, String msg){
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification.Builder(getBaseContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
@francisnnumbi
francisnnumbi / Snacker.java
Created July 19, 2017 11:17
An implementation of a Snackbar on which Imageview is added. It is the same Snackbar. I retrieved its view, cast into layout and added Imageview private void popInfo(View view, String info) { Snacker sn = Snacker.build(view, info, Snacker.INDEFINITE); sn.getView().setBackgroundColor(Color.CYAN); sn.setImageResource(R.drawable.ic_launcher); sn.se…
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.graphics.drawable.Icon;
import android.graphics.Bitmap;
public class Snacker
@francisnnumbi
francisnnumbi / Snacker.java
Created July 19, 2017 11:17
An implementation of a Snackbar on which Imageview is added. It is the same Snackbar. I retrieved its view, cast into layout and added Imageview private void popInfo(View view, String info) { Snacker sn = Snacker.build(view, info, Snacker.INDEFINITE); sn.getView().setBackgroundColor(Color.CYAN); sn.setImageResource(R.drawable.ic_launcher); sn.se…
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.graphics.drawable.Icon;
import android.graphics.Bitmap;
public class Snacker
@francisnnumbi
francisnnumbi / InnerReader.java
Created July 13, 2017 06:05
Simple class that reads res/raw text file using Scanner.class It has got one static method that require the Context and the resource id of the file to be read. It is a simple demonstration of reusability. The other way is to use a succession of InputStream - InputStreamReader - BufferedReader to read that res/raw text file.
import android.content.res.*;
import java.util.*;
import android.content.*;
public class InnerReader
{
public static String readText(Context context, int resId){
StringBuilder sb = new StringBuilder();
Scanner reader = new Scanner(context.getResources().openRawResource(resId));
@francisnnumbi
francisnnumbi / MultiChoiceListDialog.java
Created July 8, 2017 21:01
a multichoice dialog class
import android.app.*;
import android.content.*;
import android.os.*;
import java.util.*;
public class MultiChoiceListDialog extends DialogFragment {
private String title;
private String[] choices;
private ArrayList<String> selectedItems;
private OnDismissListener listener;
@francisnnumbi
francisnnumbi / ListDialog.java
Created July 8, 2017 19:37
demonstrating a dialog with a list of item to select from. this list is passed from the constructor during initialization
import android.app.*;
import android.content.*;
import android.os.*;
import java.util.*;
public class ListDialog extends DialogFragment {
private String title;
private String[] choices;
private OnDismissListener listener;
private FragmentManager fragmentManager;
@francisnnumbi
francisnnumbi / MyDialog.java
Created July 8, 2017 18:17
a demonstration of confirm dialog.
import android.app.*;
import android.os.*;
import android.content.*;
public class MyDialog extends DialogFragment {
public static final int YES = 1, NO = 0, CANCEL = 2;
private String title, message;
private OnDismissListener listener;
private FragmentManager fragmentManager;
@francisnnumbi
francisnnumbi / assetsReader.txt
Created June 12, 2017 08:40
#ANDROID Reading simple and plain text from assets folder.
/** reading plain text from assets folder */
public static String loadText(Context ctx, String address){
String _data = "";
try{
InputStream is = ctx.getAssets().open(address);
int size = is.available();
byte[] buffer = new byt[size];
is.read(buffer);
is.close();
_data = new String(buffer);
@francisnnumbi
francisnnumbi / VoiceReadText.java
Created May 22, 2017 13:53
Simple implementation of Text to Speech.
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.widget.Toast;
import java.util.Locale;
/*** the implementation of TextToSpeech */
public final class VoiceReadText{
private static TextToSpeech tts;
private static boolean READING = false;