Get MIME Type on Android

    public String getMimeType(String url) {
        String type = null;
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        if (extension != null) {
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        }
        return type;
    }

Check whether there is an Internet connection available on Android

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

References
https://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android

Launch Telegram app from my own application

    public boolean isAppAvailable(Context context, String appName) {
        PackageManager pm = context.getPackageManager();
        try {
            pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
final String appName = "org.telegram.messenger";
                final boolean isAppInstalled = isAppAvailable(getApplicationContext(), appName);
                if (isAppInstalled) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/joinchat/BSLSdUJkvhEVnDI4lbNw3g"));
                    startActivity(intent);
                } else {
                    String msg = getResources().getString(R.string.telegram_not_installed);
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
                }

References
https://stackoverflow.com/questions/35721942/how-to-launch-telegram-app-from-my-own-android-application

Validate EditText Input on Android

emailEditText.setError("Invalid Email");
emailEditText.setError(null);//removes error

Custom Icon

Drawable drawable = getDrawable(R.drawable.ic_error_outline_black_24dp);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
users.setError("error", drawable);

References
http://stacktips.com/tutorials/android/edittext-validation-in-android-example
https://stackoverflow.com/questions/13195852/how-can-i-remove-edittext-error-and-request-focus-in-android

Working with greenDAO on Android

build.gradle

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "ir.mhdr.a097"
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'org.greenrobot:greendao:3.2.2'

    // This is only needed if you want to use encrypted databases
    compile 'net.zetetic:android-database-sqlcipher:3.5.6'

    testCompile 'junit:junit:4.12'
}

App.java

public class App extends Application {

    public static final boolean ENCRYPTED = false;

    @Override
    public void onCreate() {
        super.onCreate();

        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? "notes-db-encrypted" : "notes-db");
        Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
        Statics.daoSession = new DaoMaster(db).newSession();
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ir.mhdr.a097">

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Statice.java

public class Statics {

    public static DaoSession daoSession;
}

Note.java

@Entity(indexes = {
        @Index(value = "text, date DESC", unique = true)
})
public class Note {

    @Id(autoincrement = true)
    private Long id;

    @Index(unique = true)
    private String uuid;

    @NotNull
    private String text;

    @Property(nameInDb = "date")
    private Date date;

    @Transient
    private int tempUsageCount;

@Generated(hash = 102375063)
public Note(Long id, String uuid, @NotNull String text, Date date) {
    this.id = id;
    this.uuid = uuid;
    this.text = text;
    this.date = date;
}
@Generated(hash = 1272611929)
public Note() {
}
public Long getId() {
    return this.id;
}
public void setId(Long id) {
    this.id = id;
}
public String getText() {
    return this.text;
}
public void setText(String text) {
    this.text = text;
}
public Date getDate() {
    return this.date;
}
public void setDate(Date date) {
    this.date = date;
}
public String getUuid() {
    return this.uuid;
}
public void setUuid(String uuid) {
    this.uuid = uuid;
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private DaoSession daoSession;
    private NoteDao noteDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        daoSession = Statics.daoSession;
        noteDao = daoSession.getNoteDao();

        Note note = new Note();
        note.setUuid(UUID.randomUUID().toString());
        note.setDate(new Date());
        note.setText("Hello World");

        noteDao.save(note);
    }
}

if using Kotlin

android {
    ...
    sourceSets {
        main.java.srcDirs += 'build/generated/source/greendao'
    }
}

References
http://greenrobot.org/greendao/documentation/
https://github.com/mhdr/AndroidSamples/tree/master/097
https://github.com/greenrobot/greenDAO/issues/395

Create a Modal Bottom Sheet

build.gradle

compile 'com.android.support:design:<latest-library-version>'

Create a class that extends BottomSheetDialogFragment , inflated with the layout that will be used as the content of the modal dialog.

public class BottomSheetFragment extends BottomSheetDialogFragment {


    public BottomSheetFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_bottom_sheet, container, false);
    }

}

Create an instance of the modal bottom sheet and show it with the show method, passing the FragmentManager and a string tag as parameters.

CustomBottomSheetDialog bottomSheetDialog = CustomBottomSheetDialog.getInstance();
bottomSheetDialog.show(getSupportFragmentManager(), "Custom Bottom Sheet");

References
https://github.com/mhdr/AndroidSamples/tree/master/096
https://mayojava.github.io/android/bottom-sheets-android/

Working with SharedPreferences

    public void calculateAndShow(boolean swapRanges) {
        float value = 24;
        UserBL userBL = new UserBL(getContext());
        User user = userBL.getActiveUser();

        BMI bmi = new BMI(user.getLatestHeight(), user.getLatestWeight());
        value = Float.parseFloat(String.format(Locale.US, "%.2f", bmi.calculate()));

        String name = user.getName();
        String age = String.valueOf(calculateAge(user.getBirthdate()));
        String height = String.valueOf(user.getLatestHeight());

        textViewProfileInfoAge.setText(String.format(Locale.US, "%s سال", age));
        textViewProfileInfoName.setText(String.format(Locale.US, "%s", name));
        textViewProfileInfoHeight.setText(String.format(Locale.US, "%s سانتی متر", height));

        gauge.setHighValue(value, 12, 44);

        textViewBMI.setText(String.valueOf(value));

        textViewCurrentWeight.setText(String.format(Locale.US, "%s کیلوگرم", user.getLatestWeight()));

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        SharedPreferences sharedPreferences = getContext().getSharedPreferences("bmi", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        if (!swapRanges) {

            if (sharedPreferences.getInt("bmi_range", -1) == -1) {

                BmiRangeFragment bmiRangeFragment = new BmiRangeFragment();
                fragmentTransaction.replace(R.id.linearLayoutRangeContainer, bmiRangeFragment);

            } else if (sharedPreferences.getInt("bmi_range", -1) == 1) {

                BmiRangeFragment bmiRangeFragment = new BmiRangeFragment();
                fragmentTransaction.replace(R.id.linearLayoutRangeContainer, bmiRangeFragment);

            } else if (sharedPreferences.getInt("bmi_range", -1) == 2) {
                WeightRangeFragment weightRangeFragment = new WeightRangeFragment();

                Bundle bundle = new Bundle();
                bundle.putSerializable("bmi", bmi);
                weightRangeFragment.setArguments(bundle);

                fragmentTransaction.replace(R.id.linearLayoutRangeContainer, weightRangeFragment);

            }

            fragmentTransaction.commit();

        } else {

            Fragment previousFragment = fragmentManager.findFragmentById(R.id.linearLayoutRangeContainer);

            if (previousFragment instanceof WeightRangeFragment) {

                BmiRangeFragment bmiRangeFragment = new BmiRangeFragment();

                fragmentTransaction.setCustomAnimations(R.anim.scale_up, R.anim.scale_down)
                        .replace(R.id.linearLayoutRangeContainer, bmiRangeFragment);

                editor.putInt("bmi_range", 1);

            } else if (previousFragment instanceof BmiRangeFragment) {
                WeightRangeFragment weightRangeFragment = new WeightRangeFragment();

                Bundle bundle = new Bundle();
                bundle.putSerializable("bmi", bmi);
                weightRangeFragment.setArguments(bundle);


                fragmentTransaction.setCustomAnimations(R.anim.scale_up, R.anim.scale_down)
                        .replace(R.id.linearLayoutRangeContainer, weightRangeFragment);

                editor.putInt("bmi_range", 2);
            }

            fragmentTransaction.commit();
            editor.apply();
        }
    }

References
https://www.tutorialspoint.com/android/android_shared_preferences.htm