0

I know this is very basic for you but I want the user to enter a specific value and the meaning of this value in another activity

ex : if the user entered "1FC3" the second activity will show "Milk"

Ted Josh
  • 1
  • 1

2 Answers2

0

make a static variable in one of activities.update it with your input then when 2. activity opened create a switch case that use the static variable

public static Sting var;
TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      tv = findv....
      if(var.equals("1FC3"))
          tv.setText("Milk");
}
mBogaz
  • 136
  • 12
0

Lets say you have Activity1.java: (your MainActivity) Its XML has an EditText and a Button. The button has id mybutton and the edit text myedittext. Inside its onCreate method, you get the Button from the XML and when you click the button, a new Activity (Activity2.java) starts with the text value of the edit text of the first activity to your new activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText editText = (EditText) findViewById(R.id.myedittext);
    Button button = (Button) findViewById(R.id.mybutton);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(this, Activity2.class);
            intent.putExtra("myinput", editText.getText().toString());
            startActivity(intent);

        }
      });


}

The Activity2 in its xml has a TextView with id mytv. Now inside the onCreate of the second Activity (Activity2.java):

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_two);
    TextView tv = (TextView) findViewById(R.id.mytv);
    Bundle extras = getIntent().getExtras(); 
    if(extras != null) {
          if(extras.getString("myinput").equals("1FC3")){
              tv.setText("Milk");
          }

    }


}
mantlabs
  • 352
  • 2
  • 6