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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / ReadingPlainTextUsingScanner.txt
Created August 4, 2017 09:18
Simplest way of reading plain text from external file using Scanner.class. Remember to include this permission in your manifest file: READ_EXTERNAL_STORAGE but if you got write permission, it is not necessary to include read permission as it is already incorporated in write permission.
public String readText(File file){
StringBuilder builder = new StringBuilder();
try{
Scanner scanner = new Scanner(file);
while(scanner.hasNext()){
builder.append(scanner.nextLine());
builder.append('\n');
}
scanner.close();
}
@francisnnumbi
francisnnumbi / backgroundTaskOfActivity.txt
Created August 4, 2017 19:10
If you want a background task (thread) to update your ui, this is the snippet. You need 3 things: - a handler that enables repetition; - the activity's method runOnUiThread() and; - a runnable object in which run() method is implemented. All the ui updates are put inside run() method. And set the pause between repetitions with handler inside run…
private void bgdrun() {
final Handler handler = new Handler();
runOnUiThread(new Runnable(){
@Override
public void run() {
// update your views here.
// do not use loops in here.
handler.postDelayed(this, 1000);
}
});