-4

I need the string from EditText from Mainactivity so that i can compare the value and show the desired image in the next..but only the else part is working in the second activity. I tried this code but it did't work..

private Button b1;
static EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et = (EditText)findViewById(R.id.pass);
    b1 = (Button)findViewById(R.id.clickhere);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(et.getText().toString().equals(getString(R.string.Ronnie)))
            {
                Intent myIntent = new Intent(MainActivity.this, 
        Thought.class);
                startActivity(myIntent);


            }
            else if(et.getText().toString().equals(getString(R.string.Ankita)))
            {
                Intent myIntent = new Intent(MainActivity.this, Thought.class);
                startActivity(myIntent);

            }

            else
            {
                Toast.makeText(getApplicationContext(),"Not for you",Toast.LENGTH_SHORT);
            }

        }
    });
}

and second activity code

public class Thought extends MainActivity {

public ImageView iv;
static String s1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent=getIntent();
    setContentView(R.layout.activity_thought);
    s1 = MainActivity.et.getText().toString();
    iv = (ImageView)findViewById(R.id.imageView);
    if(s1.equals(getString(R.string.Ronnie)))
    {
        iv.setImageResource(R.drawable.ronniel);
    }
    else if(s1.equals(getString(R.string.Ankita)))
    {
        iv.setImageResource(R.drawable.ankitat);
    }
    else
    {
        iv.setImageResource(R.drawable.subha);
    }
}
}
mehul chauhan
  • 1,792
  • 11
  • 26
  • Your question should be "How to pass data from activity to another activity", https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – Tenten Ponce Dec 29 '17 at 10:21
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Leonardo Alves Machado Dec 29 '17 at 10:44

4 Answers4

0

try this code : In Main Activity

Intent myIntent = new Intent(MainActivity.this, Thought.class);
myIntent.putstring("title",et.getText().toString());
startActivity(myIntent);

then in thought.class use :

s1 = getIntent().getstring("title");

and use this instead of s1 = MainActivity.et.getText().toString();

0

MainActivity

    b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(et.getText().toString().equals(getString(R.string.Ronnie)))
        {
            Intent myIntent = new Intent(MainActivity.this, 
        Thought.class);
             myIntent.putExtra("MyEditText",et.getText().toString);
            startActivity(myIntent);


        }
        else if(et.getText().toString().equals(getString(R.string.Ankita)))
        {
            Intent myIntent = new Intent(MainActivity.this, Thought.class);
              myIntent.putExtra("MyEditText",et.getText().toString);
            startActivity(myIntent);

        }

        else
        {
            Toast.makeText(getApplicationContext(),"Not for you",Toast.LENGTH_SHORT);
        }

    }
});

SecondActivity

 s1 = getIntent().getStringExtra("MyEditText")
mehul chauhan
  • 1,792
  • 11
  • 26
0

You can pass the edittext value through bundle from one activity to another. Through this way you can avoid the edit text value comparing code from second activity.

Here I have added sample code

First activity

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("editTextValue", et.getText().toString());
startActivity(intent);

Second Activity

if(getIntent()!=null){
   String editTextValue = getIntent().getStringExtra("editTextValue");
}

And change your code like this. Actually this is the proper way to pass some value from one activity to anothor.

        if(editTextValue.equals(getString(R.string.Ronnie)))
        {
            iv.setImageResource(R.drawable.ronniel);
        }
EKN
  • 1,886
  • 1
  • 16
  • 29
0

Use Bundle to pass Strings between activities

private Button b1;
static EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et = (EditText)findViewById(R.id.pass);
    b1 = (Button)findViewById(R.id.clickhere);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String pass = et.getText().toString();
            if(pass.equals(getString(R.string.Ronnie)) || pass.equals(getString(R.string.Ankita)))
            {
                Intent myIntent = new Intent(MainActivity.this, Thought.class);
                myIntent.putExtra("pass",pass);
                startActivity(myIntent);


            }else{
                Toast.makeText(getApplicationContext(),"Not for you",Toast.LENGTH_SHORT);
            }

        }
    });
}

get data like

public class Thought extends Activity {

public ImageView iv;
static String s1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_thought);

    iv = (ImageView)findViewById(R.id.imageView);

    final Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    if(bundle != null){
       String pass = bundle.getString("pass");
             if(pass.equals(getString(R.string.Ronnie)))
              {
                 iv.setImageResource(R.drawable.ronniel);
              }
             else if(pass.equals(getString(R.string.Ankita)))
             {
                 iv.setImageResource(R.drawable.ankitat);
             }

    }else
    {
        iv.setImageResource(R.drawable.subha);
    }
}
}
Omkar
  • 3,040
  • 1
  • 22
  • 42