Showing posts with label intent. Show all posts
Showing posts with label intent. Show all posts

Sunday, 11 August 2013

StartActivityForResult with Dialog Activity Example in Android

In activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="95dp"
        android:layout_marginTop="62dp"
        android:textSize="19sp"
        android:text="**********" />

    <Button
        android:id="@+id/set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name"
        android:layout_below="@+id/name"
        android:layout_marginTop="24dp"
        android:text="Set Name" />

</RelativeLayout>


In dialog_box.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
        android:id="@+id/name_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/ok"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name_edit_text"
        android:layout_marginTop="50dp"
        android:layout_toLeftOf="@+id/cancel"
        android:text="Ok" />

    <Button
        android:id="@+id/cancel"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/ok"
        android:layout_alignBottom="@+id/ok"
        android:layout_alignParentRight="true"
        android:layout_marginRight="14dp"
        android:text="Cancel" />

</RelativeLayout>


MainActivity.java

package com.rajeshvijayakumar.safr;

public class MainActivity extends Activity implements OnClickListener {

    private TextView mNameTextView;
    private Button mSetButton;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNameTextView = (TextView) findViewById(R.id.name);
        mSetButton = (Button) findViewById(R.id.set);
        mSetButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.set:
            Intent intent = new Intent(this, NameDialogActivity.class);
            startActivityForResult(intent, 1);
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       
        if(resultCode == RESULT_OK && data.getExtras().containsKey("name")) {
            String name = data.getExtras().getString("name");
            mNameTextView.setText(name);
        }
    }   
}


NameDialogActivity.java

package com.rajeshvijayakumar.safr;

public class NameDialogActivity extends Activity implements OnClickListener {

    private EditText mNameEditText;
    private Button mOk;
    private Button mCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_box);
        mNameEditText = (EditText) findViewById(R.id.name_edit_text);
        mOk = (Button) findViewById(R.id.ok);
        mCancel = (Button) findViewById(R.id.cancel);
        mOk.setOnClickListener(this);
        mCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        Intent intent = getIntent();
        switch (v.getId()) {
        case R.id.ok:
            String name = mNameEditText.getText().toString();
            intent.putExtra("name", name);
            setResult(RESULT_OK, intent);
            break;
        case R.id.cancel:
            setResult(RESULT_CANCELED, intent);
            break;
        }
        finish();
    }
}


In manifest.xml add theme as @android:style/Theme.Dialog for NameDialogActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rajeshvijayakumar.safr"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rajeshvijayakumar.safr.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NameDialogActivity"
            android:theme="@android:style/Theme.Dialog" />
    </application>

</manifest>


Output :






















Thursday, 28 February 2013

Making Phone Call Example in Android

main.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <EditText
            android:id="@+id/number_edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="35dp"
            android:layout_marginTop="16dp"
            android:ems="10"
            android:inputType="phone" />

        <Button
            android:id="@+id/call_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/number_edit_text"
            android:layout_marginLeft="72dp"
            android:layout_marginTop="36dp"
            android:text="Make Call" />

    </RelativeLayout>

 MainActivity.java

 package com.rajeshvijayakumar.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    private Button mCallbutton;
    private EditText mNumberEditText;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mNumberEditText = (EditText) findViewById(R.id.number_edit_text);
        mCallbutton = (Button) findViewById(R.id.call_button);

        PhoneCallListener phoneListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);

        // add button listener
        mCallbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+mNumberEditText.getText().toString()));
                startActivity(callIntent);

            }

        });

    }

    private class PhoneCallListener extends PhoneStateListener {

        private boolean isPhoneCalling = false;
        private String TAG = PhoneCallListener.class.getSimpleName();
       
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (TelephonyManager.CALL_STATE_RINGING == state) {
                // phone ringing
                Log.d(TAG, "RINGING STATE");
            }

            if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                // active
                Log.d(TAG, "OFFHOOK STATE");
                isPhoneCalling = true;
            }

            if (TelephonyManager.CALL_STATE_IDLE == state) {
                Log.d(TAG, "IDLE");
            }
        }
    }
}

 Output :








Source Code : Download this Example Here









Sending Mail Example in Android

main.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="To : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/to_edit_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Subject : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/subject_edit_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Message : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/message_edit_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/send_email_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send" />

</LinearLayout>

EmailActivity.java

package com.rajeshvijayakumar.android;

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;

public class EmailActivity extends Activity {

    Button mSendButton;
    EditText mTo;
    EditText mSubject;
    EditText mMessage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mSendButton = (Button) findViewById(R.id.send_email_button);
        mTo = (EditText) findViewById(R.id.to_edit_text);
        mSubject = (EditText) findViewById(R.id.subject_edit_text);
        mMessage = (EditText) findViewById(R.id.message_edit_text);

        mSendButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

              String to = mTo.getText().toString();
              String subject = mSubject.getText().toString();
              String message = mMessage.getText().toString();

              Intent email = new Intent(Intent.ACTION_SEND);
              email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
              email.putExtra(Intent.EXTRA_SUBJECT, subject);
              email.putExtra(Intent.EXTRA_TEXT, message);

              //prompts email client only
              email.setType("message/rfc822");
             
              startActivity(Intent.createChooser(email, "Choose an Email client :"));
             
            }
        });
    }
}

Output :






Source Code :  Download this example here

Sunday, 6 January 2013

Sending ArrayList of Objects as extras from one Activity to Other

Question.java
public class Question implements Serializable {

    private int[] operands;
    private int[] choices;
    private int userAnswerIndex;

    public Question(int[] operands, int[] choices) {
        this.operands = operands;
        this.choices = choices;
        this.userAnswerIndex = -1;
    }

    public int[] getChoices() {
        return choices;
    }

    public void setChoices(int[] choices) {
        this.choices = choices;
    }

    public int[] getOperands() {
        return operands;
    }

    public void setOperands(int[] operands) {
        this.operands = operands;
    }

    public int getUserAnswerIndex() {
        return userAnswerIndex;
    }

    public void setUserAnswerIndex(int userAnswerIndex) {
        this.userAnswerIndex = userAnswerIndex;
    }

    public int getAnswer() {
        int answer = 0;
        for (int operand : operands) {
            answer += operand;
        }
        return answer;
    }

    public boolean isCorrect() {
        return getAnswer() == choices[this.userAnswerIndex];
    }

    public boolean hasAnswered() {
        return userAnswerIndex != -1;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();

        // Question
        builder.append("Question: ");
        for(int operand : operands) {
            builder.append(String.format("%d ", operand));
        }
        builder.append(System.getProperty("line.separator"));

        // Choices
        int answer = getAnswer();
        for (int choice : choices) {
            if (choice == answer) {
                builder.append(String.format("%d (A) ", choice));
            } else {
                builder.append(String.format("%d ", choice));
            }
        }
        return builder.toString();
       }

      }

In your Source Activity, use this :

List<Question> mQuestionList = new ArrayList<Question>;
int[] ops = {1,2,3,4,5};
int[] choices = {12,45,23,16};
mQuestionList.add(new Question(ops, choices));

ops1 = {11,22,33,44,55};
choices1 = {122,453,423,516};
mQuestionList.add(new Question(ops1, choices1));

Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("QuestionListExtra", ArrayList<Question>mQuestionList);


In your Target Activity, use this :

List<Question> questions = new ArrayList<Question>();
 questions = (ArrayList<Question>)getIntent().getSerializableExtra("QuestionListExtra");