Android Open Navigation Drawer from Right

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
    android:id="@+id/drawerLayout"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    tools:openDrawer="end"
    tools:context="ir.mhdr.bmi.MainActivity">


    <!--Content-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            layout="@layout/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <RelativeLayout
            android:id="@+id/relativeLayoutMainContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.BottomNavigationView
                android:id="@+id/bottomNavigationView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                app:itemBackground="@color/colorBottomNavigation"
                app:menu="@menu/bottom_navigation">

            </android.support.design.widget.BottomNavigationView>

        </RelativeLayout>

    </RelativeLayout>

    <!--Drawer-->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="end">

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    DrawerLayout drawerLayout;
    Toolbar toolbar;
    ActionBarDrawerToggle actionBarDrawerToggle;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ViewCompat.setLayoutDirection(toolbar, ViewCompat.LAYOUT_DIRECTION_RTL);
        getSupportActionBar().setTitle(R.string.app_name_fa);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actionBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // it will not work for right to left navigation drawer
/*        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }*/


        // so we have to open and close the navigation drawer ourselves
        if(item.getItemId() == android.R.id.home) {
            if(drawerLayout.isDrawerOpen(Gravity.END)) {
                drawerLayout.closeDrawer(Gravity.END);
            }
            else {
                drawerLayout.openDrawer(Gravity.END);
            }
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

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

Android Right to Left Toolbar

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;

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

        toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ViewCompat.setLayoutDirection(toolbar,ViewCompat.LAYOUT_DIRECTION_RTL);
    }
}

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

Android Working with TableLayout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="mhdr.ir.a063.MainActivity">


    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:stretchColumns="1"
        android:layout_centerHorizontal="true">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="First Name"
                android:gravity="right"
                android:layout_marginRight="2dp"
                android:layout_column="0"/>

            <EditText
                android:id="@+id/editText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:ems="10"
                android:inputType="textPersonName" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="Last Name"
                android:layout_marginRight="2dp"
                android:layout_column="0"/>

            <EditText
                android:id="@+id/editText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:ems="10"
                android:inputType="textPersonName" />

            </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Age"
                android:gravity="right"
                android:layout_marginRight="2dp"
                android:layout_column="0"/>

            <EditText
                android:id="@+id/editText3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:ems="10"
                android:inputType="textPersonName" />
            </TableRow>

        <TableRow
            android:layout_marginTop="30dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2"
                android:text="Save" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

References
https://github.com/mhdr/AndroidSamples/tree/master/063
https://www.mkyong.com/android/android-tablelayout-example/
https://www.tutorialspoint.com/android/android_table_layout.htm

Which application is using a specific port in Linux

$ lsof -i :8080

COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    10165 mkyong   52u  IPv6 191544      0t0  TCP *:http-alt (LISTEN)
$ ps -ef | grep 10165

mkyong   10165  4364  1 11:58 ?        00:00:20 /opt/jdk/jdk1.8.0_66/jre/bin/java
//...
-Djava.endorsed.dirs=/home/mkyong/software/apache-tomcat-8.0.30/endorsed
-classpath /home/mkyong/software/apache-tomcat-8.0.30/bin/bootstrap.jar:
/home/mkyong/software/apache-tomcat-8.0.30/bin/tomcat-juli.jar
-Dcatalina.base=/home/mkyong/.IntelliJIdea15/system/tomcat/Unnamed_hc_2
-Dcatalina.home=/home/mkyong/software/apache-tomcat-8.

0.30
-Djava.io.tmpdir=/home/mkyong/software/apache-tomcat-8.0.30
/temp org.apache.catalina.startup.Bootstrap start

References
https://www.mkyong.com/linux/linux-which-application-is-using-port-8080/

Android SQLite Database Tutorial

Insert

View

Search

Update

Delete

Contact.java

package com.androidhive.androidsqlite; 
 
public class Contact {
     
    //private variables
    int _id;
    String _name;
    String _phone_number;
     
    // Empty constructor
    public Contact(){
         
    }
    // constructor
    public Contact(int id, String name, String _phone_number){
        this._id = id;
        this._name = name;
        this._phone_number = _phone_number;
    }
     
    // constructor
    public Contact(String name, String _phone_number){
        this._name = name;
        this._phone_number = _phone_number;
    }
    // getting ID
    public int getID(){
        return this._id;
    }
     
    // setting id
    public void setID(int id){
        this._id = id;
    }
     
    // getting name
    public String getName(){
        return this._name;
    }
     
    // setting name
    public void setName(String name){
        this._name = name;
    }
     
    // getting phone number
    public String getPhoneNumber(){
        return this._phone_number;
    }
     
    // setting phone number
    public void setPhoneNumber(String phone_number){
        this._phone_number = phone_number;
    }
}

DatabaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper {

    public static class Schema_DB {
        private static int DATABASE_VERSION = 5;
        private static String DATABASE_NAME = "062";
    }

    public DatabaseHandler(Context context) {
        super(context, Schema_DB.DATABASE_NAME, null, Schema_DB.DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(Schema_Contacts.QUERY_CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            db.execSQL(Schema_Contacts.QUERY_DROP_TABLE);
            db.execSQL(Schema_Contacts.QUERY_CREATE_TABLE);
        }
    }


    public static class Schema_Contacts {
        public static final String TABLE_NAME = "contacts";
        public static final String COL1_ID = "_id";
        public static final String COL2_NAME = "name";
        public static final String COL3_PHONE_NUMBER = "phone_number";
        public static final String COL4_EMAIL_ADDRESS = "email_address";
        public static final String QUERY_CREATE_TABLE =String.format("CREATE TABLE \"%s\" (\n" +
                "\"%s\"  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" +
                "\"%s\"  TEXT NOT NULL,\n" +
                "\"%s\"  TEXT,\n" +
                "\"%s\"  TEXT\n" +
                ");",TABLE_NAME,COL1_ID,COL2_NAME,COL3_PHONE_NUMBER,COL4_EMAIL_ADDRESS);

        public static final String QUERY_DROP_TABLE =String.format("DROP TABLE IF EXISTS \"%s\";",TABLE_NAME);
    }
}

Contacts.java

public class Contacts {
    private DatabaseHandler dbHandler;

    public Contacts(DatabaseHandler dbHandler) {
        this.dbHandler = dbHandler;
    }


    public long addContact(Contact contact) {
        SQLiteDatabase db = this.dbHandler.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(DatabaseHandler.Schema_Contacts.COL2_NAME, contact.getName());
        values.put(DatabaseHandler.Schema_Contacts.COL3_PHONE_NUMBER, contact.getPhoneNumber());
        values.put(DatabaseHandler.Schema_Contacts.COL4_EMAIL_ADDRESS, contact.getEmailAddress());

        long inserted_id = db.insert(DatabaseHandler.Schema_Contacts.TABLE_NAME, null, values);
        db.close();

        return inserted_id;
    }

    public Cursor getContactCursor(int id) {
        SQLiteDatabase db = this.dbHandler.getReadableDatabase();


        String[] columns = {
                DatabaseHandler.Schema_Contacts.COL1_ID,
                DatabaseHandler.Schema_Contacts.COL2_NAME,
                DatabaseHandler.Schema_Contacts.COL3_PHONE_NUMBER,
                DatabaseHandler.Schema_Contacts.COL4_EMAIL_ADDRESS
        };

        String selection = DatabaseHandler.Schema_Contacts.COL1_ID + " = ?";
        String[] selectionArgs = {String.valueOf(id)};

        Cursor cursor = db.query(DatabaseHandler.Schema_Contacts.TABLE_NAME, columns,
                selection,
                selectionArgs, null, null, null, null);

        return cursor;
    }

    public Contact getContact(int id) {
        SQLiteDatabase db = this.dbHandler.getReadableDatabase();

        String[] columns = {
                DatabaseHandler.Schema_Contacts.COL1_ID,
                DatabaseHandler.Schema_Contacts.COL2_NAME,
                DatabaseHandler.Schema_Contacts.COL3_PHONE_NUMBER,
                DatabaseHandler.Schema_Contacts.COL4_EMAIL_ADDRESS
        };

        String selection = DatabaseHandler.Schema_Contacts.COL1_ID + " = ?";
        String[] selectionArgs = {String.valueOf(id)};

        Cursor cursor = db.query(DatabaseHandler.Schema_Contacts.TABLE_NAME, columns,
                selection,
                selectionArgs, null, null, null, null);

        if (cursor != null)
            cursor.moveToFirst();

        Contact contact = new Contact(cursor.getInt(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL1_ID)),
                cursor.getString(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL2_NAME)),
                cursor.getString(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL3_PHONE_NUMBER)),
                cursor.getString(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL4_EMAIL_ADDRESS)));


        cursor.close();
        db.close();

        return contact;
    }

    public List<Contact> getAllContacts() {
        List<Contact> contactList = new ArrayList<Contact>();

        String selectQuery = String.format("SELECT * FROM %s", DatabaseHandler.Schema_Contacts.TABLE_NAME);

        SQLiteDatabase db = this.dbHandler.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact(cursor.getInt(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL1_ID)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL2_NAME)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL3_PHONE_NUMBER)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL4_EMAIL_ADDRESS)));

                contactList.add(contact);
            } while (cursor.moveToNext());
        }


        cursor.close();
        db.close();

        return contactList;
    }

    public Cursor getAllContactsCursor() {
        String selectQuery = String.format("SELECT * FROM %s", DatabaseHandler.Schema_Contacts.TABLE_NAME);

        SQLiteDatabase db = this.dbHandler.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        return cursor;
    }

    public List<Contact> getContactsByName(String name) {
        List<Contact> contactList = new ArrayList<Contact>();

        SQLiteDatabase db = this.dbHandler.getWritableDatabase();

        String[] columns = {
                DatabaseHandler.Schema_Contacts.COL1_ID,
                DatabaseHandler.Schema_Contacts.COL2_NAME,
                DatabaseHandler.Schema_Contacts.COL3_PHONE_NUMBER,
                DatabaseHandler.Schema_Contacts.COL4_EMAIL_ADDRESS
        };

        String selection = DatabaseHandler.Schema_Contacts.COL2_NAME + " LIKE ?";
        String[] selectionArgs = {"%" + name + "%"};

        Cursor cursor = db.query(DatabaseHandler.Schema_Contacts.TABLE_NAME, columns, selection, selectionArgs, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact(cursor.getInt(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL1_ID)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL2_NAME)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL3_PHONE_NUMBER)),
                        cursor.getString(cursor.getColumnIndex(DatabaseHandler.Schema_Contacts.COL4_EMAIL_ADDRESS)));

                contactList.add(contact);
            } while (cursor.moveToNext());
        }


        cursor.close();
        db.close();

        return contactList;
    }

    public Cursor getContactsByNameCursor(String name) {

        SQLiteDatabase db = this.dbHandler.getWritableDatabase();

        String[] columns = {
                DatabaseHandler.Schema_Contacts.COL1_ID,
                DatabaseHandler.Schema_Contacts.COL2_NAME,
                DatabaseHandler.Schema_Contacts.COL3_PHONE_NUMBER,
                DatabaseHandler.Schema_Contacts.COL4_EMAIL_ADDRESS
        };

        String selection = DatabaseHandler.Schema_Contacts.COL2_NAME + " LIKE ?";
        String[] selectionArgs = {name};

        Cursor cursor = db.query(DatabaseHandler.Schema_Contacts.TABLE_NAME, columns, selection, selectionArgs, null, null, null);


        return cursor;
    }

    public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + DatabaseHandler.Schema_Contacts.TABLE_NAME;
        SQLiteDatabase db = this.dbHandler.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);

        int count = cursor.getCount();

        cursor.close();
        db.close();


        return count;
    }

    public int updateContact(Contact contact) {
        SQLiteDatabase db = this.dbHandler.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(DatabaseHandler.Schema_Contacts.COL2_NAME, contact.getName());
        values.put(DatabaseHandler.Schema_Contacts.COL3_PHONE_NUMBER, contact.getPhoneNumber());
        values.put(DatabaseHandler.Schema_Contacts.COL4_EMAIL_ADDRESS, contact.getEmailAddress());


        int rows_affected = db.update(DatabaseHandler.Schema_Contacts.TABLE_NAME, values,
                DatabaseHandler.Schema_Contacts.COL1_ID + " = ?",
                new String[]{String.valueOf(contact.getId())});
        db.close();

        return rows_affected;
    }

    public int deleteContact(Contact contact) {
        SQLiteDatabase db = this.dbHandler.getWritableDatabase();
        int rows_affected = db.delete(DatabaseHandler.Schema_Contacts.TABLE_NAME,
                DatabaseHandler.Schema_Contacts.COL1_ID + " = ?",
                new String[]{String.valueOf(contact.getId())});
        db.close();

        return rows_affected;
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/062
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
https://www.youtube.com/watch?v=38DOncHIazs&t=1510s&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk&index=34
https://www.youtube.com/watch?v=ahE8bQRD4f0&t=3s&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk&index=35
https://www.youtube.com/watch?v=V4FqE83K1n0&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk&index=36
https://www.youtube.com/watch?v=Im6oY8QSVHU&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk&index=37
https://www.youtube.com/watch?v=HSTt_M4bpBY&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk&index=38
https://www.youtube.com/watch?v=g2x1lzBKB8M&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk&index=39