-4

This is my 1st time working in AndroidStudio. What I want to do is I want to pass the value generated in the txresult(activity_main.xml), the one generated after it scans. I want to pass that value after I push Yes, to my SecondActivity.java, and to be able to display the value.

activity_main.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"
             android:weightSum="1">

             <View
                 android:layout_width="match_parent"
                 android:layout_height="1dp"
                 android:layout_marginTop="10dp"
                 android:layout_marginBottom="10dp"
                 android:background="#ddd" />

             <Button
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:text="Scan"
                 android:onClick="callZXing"
                 android:id="@+id/Button" />

             <View
                 android:layout_width="match_parent"
                 android:layout_height="1dp"
                 android:layout_marginTop="10dp"
                 android:layout_marginBottom="10dp"
                 android:background="#ddd" />

             <TextView
                 android:id="@+id/txResult"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:text="Rezultat:"
                 android:textSize="20sp" />

             <TextView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:textAppearance="?android:attr/textAppearanceLarge"
                 android:text="Is this the correct code?"
                 android:id="@+id/textView"
                 android:layout_marginTop="70dp"
                 android:layout_gravity="center_horizontal" />

             <Button
                 style="@style/CaptureTheme"
                 android:layout_width="112dp"
                 android:layout_height="68dp"
                 android:text="Yes"
                 android:layout_marginTop="30dp"
                 android:id="@+id/button2"
                 android:layout_weight="0.10"
                 android:layout_gravity="center_horizontal"
                 android:onClick="sendSecond" />

             <Button
                 style="@style/CaptureTheme"
                 android:layout_width="112dp"
                 android:layout_height="68dp"
                 android:text="No"
                 android:layout_marginTop="10dp"
                 android:id="@+id/button1"
                 android:layout_weight="0.10"
                 android:layout_gravity="center_horizontal"
                 android:onClick="callZXing" />

         </LinearLayout>

MainActivity.java

public class MainActivity extends Activity {    
public static final int REQUEST_CODE = 0;   
private TextView txResult;

        public class FirstActivity extends Activity {

            @Override       
public void onCreate(Bundle savedInstanceState) {        
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

    findViewById(R.id.button2).setOnClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                TextView textView = (TextView)findViewById(R.id.txResult);
        Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            intent.putExtra("textViewText", textView.getText().toString());
                        startActivity(intent);
                    }           
});         
}}

SecondActivity.java

   package br.exemplozxingintegration;

   import android.app.Activity; import android.os.Bundle; 
   import android.widget.TextView;

         public class SecondActivity extends Activity {


                 @Override
                 public void onCreate(Bundle savedInstanceState) {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.activity_second);
                     Bundle extras = getIntent().getExtras();
                     if (extras != null) {
                         TextView textView = (TextView) findViewById(R.id.text_view);
                         textView.setText(extras.getString("textViewText"));
                     }
                 }}

Activity_second.xml

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

             <TextView
                 android:id="@+id/text_view"
                 android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 android:layout_weight="0.53" /> </LinearLayout>
user3026270
  • 187
  • 1
  • 2
  • 9
  • 5
    Similar question has been asked here : http://stackoverflow.com/questions/3510649/how-to-pass-a-value-from-one-activity-to-another-in-android – Suyash Feb 10 '16 at 08:30

3 Answers3

0

Using Intent.putExtra() you can do what you want to do.

    Intent intent = new Intent( this, MyActivity.class );
 intent.putExtra( "paramName", textview.getText().toString() ); 
    startActivity( intent );

You can get the values by using

    Bundle extras = getIntent().getExtras(); 
if (extras != null) 
{
 String myParam=extras.getString("paramName");
 }

You would use this part on the second activity, specifically the onCreate methode.

ImAtWar
  • 1,073
  • 3
  • 11
  • 23
0

Try this:

    Intent i=new Intent(MainActivity.this, SecondActivity.class);
    i.putExtra("result",txResult.getText());
    startActivity(i);

and in your SecondActivity:

Bundle extras = getIntent().getExtras(); 
if (extras != null) 
{
String strResult=extras.getString("result");
}

strResult gives your string passed from your previous Activity

Jas
  • 3,207
  • 2
  • 15
  • 45
0

FirstActivity.java

public class FirstActivity extends Activity {

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

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView textView = (TextView) findViewById(R.id.text_view);
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            intent.putExtra("textViewText", textView.getText().toString());
            startActivity(intent);
        }
    });
}}

first_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/text_view"
    android:text="123"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Send Text to Second Activity"/>
</LinearLayout>

SecondActivity.java

public class SecondActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_activity);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        TextView textView = (TextView) findViewById(R.id.text_view);
        textView.setText(extras.getString("textViewText"));
    }
}}

SecondActivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>
finki
  • 2,063
  • 1
  • 20
  • 23
  • It works without any errors, but it doesn't copy the number in the 2nd activity – user3026270 Feb 10 '16 at 09:19
  • it does, how do you explain the value of the TextView in the SecondActivity is the same like in the FirstActivity? ;) – finki Feb 10 '16 at 09:20
  • In the 1st it's txresult, and in the 2nd it's text_view – user3026270 Feb 10 '16 at 09:22
  • what do you even want to say? Sry, your english is so bad give it another try explaining it to me – finki Feb 10 '16 at 09:23
  • In the FirstActivity the value is txresult(the one displayed by the scan), and in the second it's text_view. I think this is what you asked :). – user3026270 Feb 10 '16 at 09:28
  • if you cant give me a detailled explanation what you want (at least 100 words) i won't help you anymore because i have no idea what you want to do. – finki Feb 10 '16 at 09:32
  • I'm gonna edit the code, so that you can see the modifications that I made in the code. And sorry again for being a pain – user3026270 Feb 10 '16 at 09:33
  • and you update you despription to a detailled version. i wont look at it if the description is shorter than 100 words and not saying anything (like at the moment). – finki Feb 10 '16 at 09:34
  • fix the code, its all mashed up now. -> formatting... – finki Feb 10 '16 at 09:48
  • And what is your problem now? – finki Feb 10 '16 at 10:18
  • It scans the product and after I press button2(Yes), it should take me to Activity_second.xml(it does this), and it should display the barcode number(the one that is displayed in activity_main.xml, txresult) in the text field there. My problem is that it doesn't display. Take you so much for taking your time with me :). – user3026270 Feb 10 '16 at 10:21
  • well, my code does exactly this and works for me. so there have to be another mistake by you. – finki Feb 10 '16 at 10:24
  • Anyway, thanks a lot man for all the help. You're great ;) – user3026270 Feb 10 '16 at 10:32