Android Get Screen width and height

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

In a view you need to do something like this:

((Activity) getContext()).getWindowManager()
                         .getDefaultDisplay()
                         .getMetrics(displayMetrics);

Or

public static int getScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;
}

public static int getScreenHeight() {
    return Resources.getSystem().getDisplayMetrics().heightPixels;
}

References
http://stackoverflow.com/questions/4743116/get-screen-width-and-height

Android Percent Layouts

build.gradle

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:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    compile 'com.android.support:percent:25.3.1'
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ir.mhdr.a081.MainActivity">

    <View android:background="@color/colorPrimary"
        app:layout_heightPercent="50%"
        app:layout_marginLeftPercent="25%"
        app:layout_marginTopPercent="25%"
        app:layout_widthPercent="50%"
        android:id="@+id/viewColor">

    </View>

    <android.support.percent.PercentFrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/viewColor"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true">

        <View
            android:layout_gravity="center"
            android:background="@color/colorAccent"
            app:layout_heightPercent="30%"
            app:layout_widthPercent="50%" />
    </android.support.percent.PercentFrameLayout>

</android.support.percent.PercentRelativeLayout>

References
https://github.com/mhdr/AndroidSamples/tree/master/081
https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html
https://developer.android.com/reference/android/support/percent/PercentFrameLayout.html
https://developer.android.com/topic/libraries/support-library/packages.html

Android Gauge Library

root build.gradle

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

build.gradle

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:25.3.1'
    compile 'com.github.Paroca72:sc-widgets:2.1.2'
    testCompile 'junit:junit:4.12'

}

References
https://github.com/mhdr/AndroidSamples/tree/master/080

Store the result of query into a variable in MySQL

INSERT INTO users (first_name, last_name, password, user_name)
VALUES (N'ادمین',
        N'ادمین',
        N'c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8de9077ec',
        N'admin');

INSERT INTO permissions (id, name) VALUES (1, N'ادمین');

SET @insertedId := (SELECT id
                    FROM users
                    WHERE user_name = N'admin');

INSERT INTO user_permissions (permission_id, user_id)
VALUES (1, @insertedId);

Android Gradient Background Color for Button

/res/drawable/gradient_color.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

    <gradient
        android:angle="135"
        android:startColor="@color/colorBackground"
        android:endColor="@color/colorPrimary"
        android:type="linear" />

    <corners
        android:radius="0dp"/>

</shape>

activity_main.xml

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editTextWeight"
                android:background="@drawable/gradient_color">
                <Button
                    android:id="@+id/buttonStart"
                    android:background="?android:attr/selectableItemBackground"
                    android:textAllCaps="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/start"
                    />
            </FrameLayout>

References
https://github.com/mhdr/AndroidSamples/tree/master/077

Android Focus on TextView

activity_main.xml

            <TextView
                android:id="@+id/textViewProfileName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/textViewFirstRunHeader2"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:gravity="center|start"
                android:text="@string/profile_name"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

MainActivity.java

        textViewProfileName= (TextView) findViewById(R.id.textViewProfileName);
        textViewProfileName.requestFocusFromTouch();

References
https://github.com/mhdr/AndroidSamples/tree/master/075

Android Custom Farsi Font for Views Using Calligraphy Library

build.gradle

dependencies {
    compile 'uk.co.chrisjenx:calligraphy:2.2.0'
}

Add Application Class :
MyApplication.java

public class MyApplication extends Application {

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

        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath("fonts/BYekan.ttf")
                .setFontAttrId(R.attr.fontPath)
                .build());
    }
}

Register your Application Class :
AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:name=".MyApplication"
        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>

Add attachBaseContext to all Activities

MainActivity.java

public class MainActivity extends AppCompatActivity {

    NumberPicker numberPicker;

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

        numberPicker= (NumberPicker) findViewById(R.id.numberPicker);
        numberPicker.setMinValue(0);
        numberPicker.setMaxValue(99);
        numberPicker.setValue(24);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(base));
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/073
https://github.com/chrisjenx/Calligraphy
https://blog.goyello.com/2014/08/01/how-to-use-custom-fonts-in-android-apps-and-not-get-fat-3/