Pages

Send Email Example

MainActivity.java
package com.blogspot.csdevbin.sendingemail;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        Button startBtn = (Button) findViewById(R.id.
sendEmail);
        startBtn.setOnClickListener(
new View.OnClickListener() {
           
public void onClick(View view) {
                sendEmail();
            }
        });
    }

   
protected void sendEmail() {
        Log.i(
"Send email", "");
        String[] TO = {
"amrood.admin@gmail.com"};
        String[] CC = {
"mcmohd@gmail.com"};
        Intent emailIntent =
new Intent(Intent.ACTION_SEND);
        emailIntent.setData(Uri.parse(
"mailto:"));
        emailIntent.setType(
"text/plain");
        emailIntent.putExtra(Intent.
EXTRA_EMAIL, TO);
        emailIntent.putExtra(Intent.
EXTRA_CC, CC);
        emailIntent.putExtra(Intent.
EXTRA_SUBJECT, "Your subject");
        emailIntent.putExtra(Intent.
EXTRA_TEXT, "Email message goes here");
       
try {
            startActivity(Intent.createChooser(emailIntent,
"Send mail..."));
            finish();
            Log.i(
"Finished sending email...", "");
        }
catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.
this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
        }
    }

}


activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

 
    <Button

        android:id="@+id/sendEmail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/compose_email" />

</LinearLayout>


Strings.xml
<resources>

    <string name="app_name">SendingEmail</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="compose_email">Compose Email</string>

</resources>




Fragment Example 1

MainActivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/LinearLayout1"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
tools:context=".MainActivity" >

    <
TextView
       
android:id="@+id/textView1"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Large Text"
       
android:textAppearance="?android:attr/textAppearanceLarge" />

    <
Button
       
android:id="@+id/button1"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Button" />

    <
fragment
       
android:id="@+id/frag"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
class="com.blogspot.csdevbin.fragment.MainActivity$SSFFragment" />

</
LinearLayout>



hellofrag.xml
<TextView

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:background="@color/colorAccent"

    android:text="fragment example "

    />


MainActivity.java
package com.blogspot.csdevbin.fragment;

  

  import android.annotation.SuppressLint;

  import android.app.Fragment;

  import android.support.v7.app.AppCompatActivity;

  import android.os.Bundle;

  import android.view.LayoutInflater;

  import android.view.View;

  import android.view.ViewGroup;

  

  public class MainActivity extends AppCompatActivity {

  

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

  

    @SuppressLint("NewApi")

    public static class SSFFragment extends Fragment {

        @Override

        public View onCreateView(LayoutInflater inflater, ViewGroup container,

                                 Bundle savedInstanceState) {

            // Inflate the layout for this fragment

            View v = inflater.inflate(R.layout.hellofrag, container, false);

            return v;

        }

    }

  

}