Search code examples
javaandroidandroid-edittextconcatenation

2 Buttons 2 EditTexts string inputs 1 TextVew output


One button adds the strings in 2 EditTexts and shows the result in the TextView. Then the second button is supposed to compare these strings to 2 separate words that are hard coded. "Hello" "World". I can't even get the strings to concatenate and show output in the Textview. Here is my code

public class MainActivity extends AppCompatActivity {
    //The stings to hold the user input
    EditText str1;
    EditText str2;
    //string to hold the output
    TextView display1;


    EditText enter1;
    EditText enter2;
    TextView WDisplay;
    Button add;
    Button display;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        str1 = (EditText)findViewById(R.id.enter1);
        str2 = (EditText)findViewById(R.id.enter2);
        add = (Button) findViewById(R.id.add);
        display = (Button) findViewById(R.id.display);
        display1 = (TextView)findViewById(R.id.WDisplay);


        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1 = enter1.getText().toString();
                String str2 = enter2.getText().toString();
                display1.setText(str1);



            }
        });

        display.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1 = enter1.getText().toString();
                String str2 = enter2.getText().toString();
                display1.setText(str1);

                /*String check1;

                {
                    check1 = "Hello";
                }

                String check2;

                {
                    check2 = "World";
                }*/
            }
        });
    }'

I just can't seem to connect the basic to what I need for my program. Please help!


Solution

  • to concatenate your strings just do the following

    add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String enter1= str1 .getText().toString();
                    String enter2= str2 .getText().toString();
                    display1.setText(enter1+" "+enter2);
                }
            });
    

    and for the button that compares the strings...what do you want to do after the comparision

        display.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String str1 = enter1.getText().toString();
                        String str2 = enter2.getText().toString();
                        display1.setText(str1);
        //comparision can be done as
        if (str1.equals("Hello"){
    //do anything
    }
    
     if (str2.equals("World"){
    //do anything
    }
    
                    }
                });
    

    hope this helps....happy coding