Android Working with Relative Layout

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="iterator.ir.a003.MainActivity">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:hint="@string/firstNameHint"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/editTextFirstName" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:hint="@string/lastNameHint"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/editTextLastName"
        android:layout_toRightOf="@+id/editTextFirstName"
        android:gravity="center"
        android:layout_toEndOf="@+id/editTextFirstName" />

    <Button
        android:text="@string/buttonSave_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextLastName"
        android:id="@+id/buttonSave"
        android:layout_toRightOf="@+id/editTextFirstName"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

References
https://www.youtube.com/watch?v=QfF_oFPeh7c&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk&index=8
https://github.com/mhdr/AndroidSamples/tree/master/003

Android Working with Linear Layout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="iterator.ir.a002.MainActivity"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:hint="@string/to_hint"/>
    
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:hint="@string/subject_hint"/>
    
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:hint="@string/message_hint"
        android:layout_weight="1"
        android:gravity="top|left"/>
    
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:hint="@string/buttonSend_text"
        android:layout_gravity="right"/>

</LinearLayout>

References
https://www.youtube.com/watch?v=DVRN3EuF9ys&index=7&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk
https://github.com/mhdr/AndroidSamples/tree/master/002

Android Send data to an Activity using Bundle

MainActivity.java

    Button buttonShow;
    EditText editTextMessage;

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

        buttonShow= (Button) findViewById(R.id.buttonShow);
        editTextMessage= (EditText) findViewById(R.id.editTextMessage);
    }


    protected void buttonShow_onClick(View view){

        String message=editTextMessage.getText().toString();
        Intent intent=new Intent(this,SecondActivity.class);
        intent.putExtra("msg",message);
        startActivity(intent);
    }

SecondActivity.java

   TextView textViewMessage;

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

        Intent intent=this.getIntent();
        // Bundle bundle=intent.getExtras();
        String message=intent.getStringExtra("msg");

        textViewMessage= (TextView) findViewById(R.id.textViewMessage);
        textViewMessage.setText(message);
    }

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

Android Support Library

com.android.support:support-compat:25.1.0
com.android.support:support-core-utils:25.1.0
com.android.support:support-core-ui:25.1.0
com.android.support:support-media-compat:25.1.0
com.android.support:support-fragment:25.1.0
com.android.support:multidex:1.0.0
com.android.support:design:25.1.0
com.android.support:support-annotations:25.1.0
com.android.support:customtabs:25.1.0
com.android.support:percent:25.1.0
com.android.support:exifinterface:25.1.0
com.android.support:recommendation:25.1.0

References
https://developer.android.com/topic/libraries/support-library/packages.html

Requesting Permissions at Run Time in Android

Request the permissions you need

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

Handle the permissions request response

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

References
https://developer.android.com/training/permissions/requesting.html