Sun
Sun

Reputation: 6888

EditText to String using Fragment

Like we always use getText().toString(); to get String from EditText, So why i am facing problem, earlier i was using Activity and now just switched to Fragment, see Log:

 02-19 00:17:27.508: V/strDate..... >>>(1354): android.widget.EditText{b1ecd2c0 V.ED..CL ......I. 0,0-0,0 #7f080001 app:id/editTextToShowDate}

Fragment

@Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
     Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.android_frag, container, false);
        final EditText editTextDate = (EditText) view.findViewById(R.id.editTextToShowDate);
        String strDate = editTextDate.getText().toString();
        Log.v("strDate..... >>>", strDate);
        return view;
 }

Upvotes: 0

Views: 995

Answers (2)

Ksharp
Ksharp

Reputation: 102

You should populate the following code into onStart() not onCreateView().

final EditText editTextDate = (EditText) getView().findViewById(R.id.editTextToShowDate);
    String strDate = editTextDate.getText().toString();

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

You need to get the text from editText on button click event

String strDate = editTextDate.getText().toString();

Upvotes: 2

Related Questions