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 / 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 / basics.md
Last active May 31, 2019 13:51
[Regex and Grep tool] #tutorial

regex for finding text with 2 substrings

Ex. 'git' and 'rm'

grep "git.*rm"

@girish3
girish3 / markdown.md
Last active May 31, 2019 13:50
[Markdown basics] #tutorial
  1. The following line is bold
    Hi, I am Bold (**Hi, I am Bold**)

  2. The following line is given emphasis
    I am cooool. (*I am cooool.*)

  3. To write something on the new line, give two spaces and hit enter
    Yay! we are on the next line

  4. Now comes the Headline

@girish3
girish3 / LaunchMode.md
Last active June 17, 2019 03:38
[Activity Launch Mode] Understanding Activity's launch mode #android #android_tutorial #tutorial

Since each Activity is made to work in different purpose. Some is designed to work separately with each Intent sent for example an Activity for email composing in email client. While some is designed to work as a singleton for example an email's inbox Activity. That's why it does matter to specify whether Activity is needed to be created a new one or to use the existed one. launchMode is designed for this specifically.

launchMode can be assigned directly as an attribute tag

<activity
    android:name=".SingleTaskActivity"
    android:label="singleTask launchMode"
    android:launchMode="singleTask">
@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 / 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 / speed_up.md
Last active April 5, 2020 05:11
[[Deprecated] Android studio/Gradle performance tips] #android

[Deprecated] migrated to notion

Gradle tweaks

Open or create a file called gradle.properties in .gradle directory. Inside the file, add following

  • org.gradle.parallel=true: Allow you to build multiple modules in the same project at the same time
  • org.gradle.daemon=true will turn on daemon so that every time we build the application, it doesn’t need to rerun the entire Gradle application every time.

Memory Allocation tweaks

@girish3
girish3 / AvoidSetter.md
Last active August 16, 2018 06:00
[Building an object model without setters] #design_pattern

Some pointers

  • Having setters violates open/close principle, prevents information hiding (breaks encapsulation)
  • Discussing getter/setters vs public fields often obscures bigger problems with objects manipulating each others' internal state in an intimate manner and hence being too closely coupled. The idea is to make methods do what your business logic wants it to do, rather than have setters which change things at a field level.
  • One way to avoid writing setters is a task based approach to the model. Think of every task that is performed in an application and add a method that changes all the affected fields at once, to perform this task.

Ref:

@girish3
girish3 / kotlin_basics.md
Last active May 12, 2019 14:02
[Kotlin basics] #android