Edittext Settext Not Working with Fragment

EditText Settext not working with Fragment

The EditText appears to have an issue with resetting text in onCreateView. So the solution here is to reset the text in onResume. This works.

Also there's an issue in onActivityCreated. I reset edittext's content in onStart and it works. [credits to @savepopulation]

EditText.setText() not working in Fragment

Change your return statement to:

return rootView;

The View you're currently returning is not the one you're initially inflating and setting up.

EditText setText not displaying on a Dialog Fragment

You don't use layout view for Dialog so that you can't see text on EditText. Please change as here:

 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle("Edit to-do list");

final View layout = View.inflate(MainActivity.getAppContext(), R.layout.custom_dialog, null);
alertDialogBuilder.setView(layout );

fragment - EditText.setText(String) not working when string comes from savedInstanceState but works when hard coded

If your views have IDs, which they do, then Android will automatically be restoring the view state. This happens after onCreateView(). So your code is working, your changes are just lost.

To elaborate, quoting the official Fragment documentation:

  1. onAttach(Activity) called once the fragment is associated with its activity.
  2. onCreate(Bundle) called to do initial creation of the fragment.
  3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
  4. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().
  5. onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
  6. onStart() makes the fragment visible to the user (based on its containing activity being started).
  7. onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).

Update:

Based on your comment below I'm 99% confident the problem is outside of the code you've displayed. Your problem is that you have two copies of ContactUsFragment in memory.

One that has had its state restored, including the EditText's text, and then another one that you must have recreated. Hence you're seeing the toast pop-up (as the old fragment is restored), but you're not seeing the values you place in the EditText, because you've replaced the old fragment's view with the new ContactUsFragment's view, which has not been restored.

Cannot setText inside a Fragment

By capturing editTextNumber.text.toString() during onViewCreated, you are retrieving the text in the EditText in onViewCreated, which is empty.

So you should be retrieving the value when the button is clicked as you mentioned.

TextView setText method not working in Fragment

First declare your mainView as global. Then use that one to look for your views, calling getActivty().findViewById... is not using the same layout as yout.

private View rootView;

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

rootView = inflater.inflate(R.layout.fragment_b, container, false);
return rootView;
}

And then

 tv = (TextView) rootView.findViewById(R.id.textView);

editText.getText().append in fragment is not working

Use

editText.setText().append(editText.getText().append(s));

What you were doing is that you are only requesting the text inside the editText , not updating the view (editText)

setText not working for a Custom Edittext

Try in this way.

filePathValue.post(new Runnable() {
@Override
public void run() {
filePathValue.setText(filePath);
}
})


Related Topics



Leave a reply



Submit