Perl Allinone
Perl Allinone

Reputation: 71

Java in Android from one screen to another screen

I am doing a Android app where I want to show the list of travels contact numbers in a separate screen when clicked on "Travels and Holidays" button. Here is the code:

AppActivity.java

package com.example.android;

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

public class AppActivity extends Activity {
Button button;
Context context = this;

private final CharSequence[] TRAVELS ={"Nirmala travels","RR Tours and Travels","Surabhii Travels"};
String numbertodial;
String phonenumberNT ="08242497051";
String phonenumberRR ="08244280999";
String phonenumberST ="08242212111";

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

button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    Dialog dialog = null;
    AlertDialog.Builder builder = null;
    builder = new AlertDialog.Builder(context);

    String travels = getString(R.string.app_name);
    builder.setTitle(travels);
    builder.setSingleChoiceItems(TRAVELS, 3,
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {

    CharSequence s = (TRAVELS[item]);

    if (s.equals("Nirmala travels")){numbertodial =phonenumberNT; }
    if (s.equals("RR Tours and Travels")){numbertodial=phonenumberRR; }
    if (s.equals("Surabhii Travels")){numbertodial=phonenumberST ;}
    Intent callIntent = new Intent(Intent.ACTION_DIAL);
    callIntent.setData(Uri.parse("tel:"+numbertodial ));
    startActivity(callIntent);

    dialog.dismiss();
    }});
    dialog = builder.create();
    dialog.show();
    return;

        }
    });

    }  
}

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

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

</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="app_name">Travels and Holidays Details</string>

</resources>

AndroidManifest.xml

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

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

When clicked on "Travels and Holidays" button, it shows the 3 travels names in the same screen. When selecting any 1, it directly goes to call that number.

But what I want is: When I click on "Travels and Holidays" button in the first screen, I want the travels name and contact number to be shown on another screen. When clicked on any travels, it should redirect to the call option of that number.

Where am I going wrong? Please help. Thanks in advance

Upvotes: 1

Views: 190

Answers (2)

Dilavar Malek
Dilavar Malek

Reputation: 1167

You have to create Another Activity and then You redirect to that place and put yourno and name in that.

 Intent i=new Intent(yourcontext,yourActivity.class);
    i.putExtra("no","yourno");
    i.putExtra("name","yourname");
    startActivity(i);

and get to youranothetactivity Using

String no=getIntent().getStringExtra("no");
String name=getIntent().getStringExtra("no");

for more info You see this link

Upvotes: 1

Dilip
Dilip

Reputation: 2311

Create a separate activity/fragment to show "Travels and Holidays" details. Call this activity/fragment on "Travels and Holidays" button click, Pass "Travels and Holidays" details value to the Details activity/fragment using Intent putExtra(). If you create Activity then make sure to declare activity inside AndroidManifest.xml

Upvotes: 0

Related Questions