Pages

Showing posts with label Button. Show all posts
Showing posts with label Button. Show all posts

Tuesday, September 20, 2016

Android Example- Implicit Intent


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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=".MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="14dp"
        android:background="#69d0ff" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="26dp"
        android:text="Button" />

</RelativeLayout>

MainActivity.java
package in.kantapp.example33;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private Spinner spinner_object;

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

        spinner_object = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
                R.array.intents, android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_object.setAdapter(adapter);
        Button but = (Button) findViewById(R.id.button1);
        but.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int position = spinner_object.getSelectedItemPosition();
        Intent intent = null;
        switch (position) {
            case 0:
                intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://www.google.com"));
                break;
            case 1:
                // dials the call directly to the number specified
                intent = new Intent(Intent.ACTION_CALL,
                        Uri.parse("tel:(+91)8888888888"));
                break;
            case 2:
                // opens up the to - dial but does not dials the number
                intent = new Intent(Intent.ACTION_DIAL,
                        Uri.parse("tel:(+91)8888888888"));

                break;
            case 3:
            /*
             * opens up the specified location in the google map by its
             * longitude and latitude
             */
                intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("geo:28.632323175362604,77.22595474023433?z=19"));

                break;
            case 4:
                // to search for india gate on google map
                intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("geo:0,0?q=india gate"));
                break;
            case 5:
                // opens up the camera to capture an image
                intent = new Intent("android.media.action.IMAGE_CAPTURE");
                break;
            case 6:
                // opens up contacts menu of your phone
                intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("content://contacts/people/"));
                break;
            case 7:
                // edits the first contact on your contact list
                intent = new Intent(Intent.ACTION_EDIT,
                        Uri.parse("content://contacts/people/1"));
                break;

        }
        if (intent != null) {
            startActivity(intent);
        }
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 0) {
            String result = data.toURI();
            Toast.makeText(this, result, Toast.LENGTH_LONG);
        }
    }

}

Intent.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="intents">
        <item>Open Browser</item>
        <item>Dial a Call </item>
        <item>Dial</item>
        <item>Show Map</item>
        <item>Search on Map</item>
        <item>Take picture</item>
        <item>Show contacts</item>
        <item>Edit first contact</item>
    </string-array>

</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example33">
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

Android Example- Explicit Intent


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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=".MainActivity"
    android:background="@drawable/background" >

    <TextView
        android:id="@+id/main_textview_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Activity to launch other activity via Intent Explicitily"
        android:textSize="30dp" >
    </TextView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/main_textview_1"
        android:layout_alignRight="@+id/main_textview_1"
        android:layout_centerVertical="true"
        android:text="Launch Activity1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/main_textview_1"
        android:layout_marginBottom="72dp"
        android:text="Launch Activity2" />

</RelativeLayout>

MainActivity.java
package in.kantapp.example32;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements OnClickListener {

    private static final int REQUEST_CODE1 = 1;
    private static final int REQUEST_CODE2 = 2;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b1 = (Button) findViewById(R.id.button1);
        Button b2 = (Button) findViewById(R.id.button2);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.button1:
                intent = new Intent(this, Activity1.class);
                intent.putExtra("Value1", "This value one for Activity1 ");
                intent.putExtra("Value2", "This value two Activity1");
                startActivityForResult(intent, REQUEST_CODE1);
                break;
            case R.id.button2:
                Intent intent = new Intent(this, Activity2.class);
                intent.putExtra("Value1", "This value one for Activity2");
                intent.putExtra("Value2", "This value two Activity2");
                startActivityForResult(intent, REQUEST_CODE2);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE1) {
            if (data.hasExtra("returnKey1")) {
                Toast.makeText(this, data.getExtras().getString("returnKey2"),
                        Toast.LENGTH_SHORT).show();
            }
        }
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE2) {
            if (data.hasExtra("returnkey2")) {
                Toast.makeText(this, data.getExtras().getString("returnkey1"),
                        Toast.LENGTH_LONG).show();
            }
        }

    }
}

Activity1.java
package in.kantapp.example32;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity1 extends Activity implements OnClickListener {

    private String str1, str2;
    private EditText text1, text2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
        Bundle data = getIntent().getExtras();
        if (data == null) {
            return;
        }
        String value1 = data.getString("Value1");
        String value2 = data.getString("Value2");
        if (value1 != null && value2 != null) {
            text1 = (EditText) findViewById(R.id.editText1);
            text2 = (EditText) findViewById(R.id.editText2);
            Button but = (Button) findViewById(R.id.layout_button1);
            but.setOnClickListener(this);
            str1 = text1.getText().toString();
            str2 = text2.getText().toString();
            TextView textview1 = (TextView) findViewById(R.id.textView1);
            TextView textview2 = (TextView) findViewById(R.id.textView2);
                      /*
                       * the text view displays data which are sent to the activity via
                       * intent
                       */
            textview1.setText(value1);
            textview2.setText(value2);
        }
    }

    @Override
    public void onClick(View view) {
        finish();
    }

    @Override
    public void finish() {
        Intent send_data = new Intent();
        send_data.putExtra("returnKey1", "Returned return key1 from Activity1");
        send_data.putExtra("returnKey2", "Returned return key2 from Activity1");
        setResult(RESULT_OK, send_data);
        super.finish();
    }
}

Activity2.java
package in.kantapp.example32;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity2 extends Activity implements OnClickListener {

    private String str1, str2;
    private EditText text1, text2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);

        Bundle data = getIntent().getExtras();
        if (data == null) {
            return;
        }
        String value1 = data.getString("Value1");
        String value2 = data.getString("Vlaue2");
        Button b1 = (Button)findViewById(R.id.layout_button1);
        b1.setOnClickListener(this);

        if (value1 != null && value2 != null) {
            text1 = (EditText) findViewById(R.id.editText1);
            text2 = (EditText) findViewById(R.id.editText2);
            Button but = (Button) findViewById(R.id.layout_button1);
            but.setOnClickListener(this);

            str1 = text1.getText().toString();
            str2 = text2.getText().toString();

            TextView textview1 = (TextView) findViewById(R.id.textView1);
            TextView textview2 = (TextView) findViewById(R.id.textView2);
                     /*
                      * the text view displays data which are sent to the activity via
                      * intent
                      */
            textview1.setText(value1);
            textview2.setText(value2);

        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        finish();
    }

    @Override
    public void finish() {
        Intent send_data = new Intent();
        send_data.putExtra("returnKey1", "Returned return key1 from Activity2");
        send_data.putExtra("returnKey2", "Returned return key2 from Activity2");
        setResult(RESULT_OK, send_data);
        super.finish();
    }

}

layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginTop="15dp"
        android:text="TextView"
        android:textSize="20dp" />
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="34dp"
        android:ems="10">

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginTop="20dp"
        android:text="TextView"
        android:textSize="20dp" />



    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:ems="10" />

    <Button
        android:id="@+id/layout_button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:text="Click To Finish" />

</LinearLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example32">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>
        <activity android:name=".Activity1"/>
        <activity android:name=".Activity2"/>
    </application>

</manifest>