2

I have 2 edittext .and i want to get value edittext1 and edittext 2 and set it in string.replace mehod

Like this; string.replace("set here1st edittex value",set here 2nd edittext value")

How to possible it?

Thanks in advnce!!

 public class MainActivity extends Activity {
EditText et1,et2,et3,etoutput,et5;
TextView tv;
Button btn;

String change; 
//String oldtext=et1.getText().toString();
//String newtext=et2.getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et1=(EditText) findViewById(R.id.editText1);
    et2=(EditText) findViewById(R.id.editText2);
    et3=(EditText) findViewById(R.id.editText3);
    et5=(EditText) findViewById(R.id.editText5);
    etoutput=(EditText) findViewById(R.id.editText4);
    btn=(Button) findViewById(R.id.button1);


btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
        String s1=et1.getText().toString();
        String s2=et2.getText().toString();

    ///  say error in oldString
       /// String newString=oldString.replace(s1,s2);


        }
    });
  //////Log.v("new vluew", newString);

  ////////////// for check that value is update ?? or not update/////////////////
 et3.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {
   }

   public void beforeTextChanged(CharSequence s, int start, 
     int count, int after) {
   }


public void onTextChanged(CharSequence s, int start, 
     int before, int count) {

       MainActivity.this.cha(s);
       MainActivity.this.change2(s);
   }
  });


}
@SuppressWarnings("unused")
private String cha(CharSequence arg) {
    String change = "";
    String s1=et1.getText().toString();
    String s2=et2.getText().toString();
    String change1=change.replace(s1,s2);
    this.etoutput.setText(change1);
    System.out.println(change1);
     return change;
     }
 @SuppressWarnings("unused")
private String change2(CharSequence arg) {
    String change = "";
    change= arg.toString().replace("", "" );
    this.et5.setText(change);
    System.out.println(change);
     return change;
     }



}

XML

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.replce.MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:inputType="textMultiLine" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:ems="10"
    android:inputType="textMultiLine" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText2"
    android:layout_below="@+id/editText2"
    android:layout_marginLeft="61dp"
    android:layout_marginTop="100dp"
    android:text="Button" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:ems="10"
    android:inputType="textMultiLine" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText3"
    android:layout_below="@+id/button1"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:inputType="textMultiLine" />

<EditText
    android:id="@+id/editText5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText4"
    android:layout_marginTop="37dp"
    android:ems="10"
    android:inputType="textMultiLine" />

 </RelativeLayout>

Thanks

8
  • Did it work? add your code so I can help you better. Commented Jan 1, 2017 at 10:59
  • Why do you have Uppercase letter E here: Et3.addTextChangedListener. var name is: et3 ? Commented Jan 1, 2017 at 12:16
  • Code looks good. can you tell me which line that causes the nullpointer exception? Commented Jan 1, 2017 at 12:46
  • @hasan83 ,This is my full code and i have no idea that what is nullpointer Commented Jan 1, 2017 at 13:01
  • I thin my nullpointer is this Commented Jan 1, 2017 at 13:27

1 Answer 1

1

Yes you can. with the following steps:

  1. Get you controls (edittexts, button) in you activity class.
  2. Add a click listener to your button.
  3. do the swap in the listener method.

Code:

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

    EditText edittext1 = (EditText)findViewById(R.id.edittext1);
    EditText edittext2 = (EditText)findViewById(R.id.edittext2);
    Button mButton = (Button)findViewById(R.id.button);

    mButton.setOnClickListener(
    new View.OnClickListener()
    {
        public void onClick(View view)
        { 
            String s1 = edittext1.getText().toString()
            String s2 = edittext2.getText().toString()
            String newString = oldString.replace(s1, s2);

            Log.v("new value", newString);
        }
    });
}
Sign up to request clarification or add additional context in comments.

4 Comments

Just look out for NullPointer as well. :D
Can you please. edit your question and add you activity code and your xml?
@hasan83 sorry.today my net problem.
I saw that. I will check it out. now you have NullPointer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.