Skip to content

Instantly share code, notes, and snippets.

View girish3's full-sized avatar
🎯
Focusing

Girish Budhwani girish3

🎯
Focusing
View GitHub Profile
@girish3
girish3 / kotlin_basics.md
Last active May 31, 2019 13:49
[Kotlin basic syntax] #kotlin

1. Kotlin primary header constructor

// the immutable parameters passed to the constructor are not member variables!, they need to be explicitly created as below
// parent class takes in the passed values to the child class.
class MyListAdapter(ctx: Context, res: Int) : ArrayAdapter<String>(ctx, res)
	val context: Context
    val resource: Int
    
    init {
    	context = ctx
@girish3
girish3 / gradle_basics.md
Last active May 27, 2019 04:47
[Gradle basics] #android #android_tutorial #tutorial

Importand Gradle commands

how to list down all tasks of a project?
gradle tasks --all

Following command list down all task to be executed for a task but it does not execute the command.
gradle (some task) --dry-run
gradle assembleDebug --dry-run

@girish3
girish3 / kotlin_basics.md
Last active May 12, 2019 14:02
[Kotlin basics] #android
@girish3
girish3 / menu.md
Last active February 17, 2019 09:37
[Menus] Menus are common user interface component in Android. It provides consistent user experience at the cost of flexibility. #android_snippet #android #android_tutorial #tutorial

Menus are common user interface component in Android. It provides consistent user experience at the cost of flexibility. You should use the Menu APIs to present user actions and other options in your activities.

There are 3 types of Menu:

Options menu

The options menu is the primary collection of menu items for an activity. It's where you should place actions that have a global impact on the app, such as "Search," "Compose email," and "Settings."

Context menu and contextual action mode

A context menu is a floating menu, like dialog, that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

@girish3
girish3 / alert_dialog.java
Last active November 11, 2018 05:07
[Alert Dialog] Alert dialog is created using a builder pattern #android_snippet #android
// this is a context
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Check other builder methods like setCancelable, setIcon..
builder.setTitle("Set Title");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do something
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@girish3
girish3 / Intent.java
Last active November 11, 2018 05:07
[Intent] Intent snippets to start Activity, Service or sending broadcast. #android_snippet #android
// starting an activity from another activity
// this is a Activity context
Intent intent = new Intent(this, AnotherActivity.java);
intent.putExtra(Intent.EXTRA_TEXT, "some text");
intent.putExtra("id", 4);
startActivity(intent);
// Receiving intent in AnotherActivity
Bundle extras = getIntent().getExtras();
@girish3
girish3 / LayoutInflater.java
Last active November 11, 2018 05:07
[Layout Inflater] Ways to get layout inflator object and inflate the view. #android_snippet #android
// if you are in an activity
LayoutInflater inflater = getLayoutInflater();
// If you have the context
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// or in a cleaner way, from() uses system service interally so its the same inflater
LayoutInflater inflater = LayoutInflater.from(context);
// inflating a view
// if you need to attach the inflated to rootView, returned view will be rootView.
// 3rd parameter is attach_to_root
@girish3
girish3 / SpannableString.java
Last active November 11, 2018 05:07
[Spannable String] #android_snippet #android
// https://stackoverflow.com/questions/10696986/how-to-set-the-part-of-the-text-view-is-clickable
SpannableString ss = new SpannableString("Android is a Software stack");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
startActivity(new Intent(MyActivity.this, NextActivity.class));
}
};
// 0 is the start index and 7 is the end index
ss.setSpan(clickableSpan, 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
@girish3
girish3 / viewpager.md
Last active November 11, 2018 05:06
[View Pager incomplete, add fragment example] #android_snippet #android

A ViewPager is a ViewGroup that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.

Create a custom Pager Adapter

// override 4 methods as shown below
class MyPagerAdapter: PagerAdapter() {

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
@girish3
girish3 / notes.md
Created September 30, 2018 12:43
[Futures and Promises] #design_pattern

In the broadest sense,

  • A future or promise can be thought of as a value that will eventually become available.
  • Future is used for sychronizing program execution.
  • Future acts as a proxy for an result which will be eventually available.
  • The construct ( future X ) immediately returns a future for the value of the expression X and concurrently begins evaluating X. When the evaluation of X yields a value, that value replaces the future.
  • A future is a placeholder object for a result that does not yet exist. A promise is a writable, single-assignment container, which completes a future. Promises can complete the future with a result to indicate success, or with an exception to indicate failure.