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:
- onAttach(Activity) called once the fragment is associated with its activity.
- onCreate(Bundle) called to do initial creation of the fragment.
- onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
- onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().
- onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
- onStart() makes the fragment visible to the user (based on its containing activity being started).
- 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
What Is the Android UI Thread Stack Size Limit and How to Overcome It
How to Search for a Value in Firebase Android
Android, Landscape Only Orientation
How to Get the Key from the Value in Firebase
Viewing Excel Files in My Android App
How to Reduce the Spacing Between the Action Item Icons on Action Bar
How to Detect "Recent Apps" System Button Clicks (Honeycomb+)
How to Pass Values Between Activities on Android
Onkeylistener Not Working with Soft Keyboard (Android)
How to Detect Bottom Soft Navigation Bar Available in Android Programmatically
How to Use Edittext Ontextchanged Event When I Press the Number
How to Get the Dreaded Write_Secure_Settings Permission for My Android App
Creating a Product Sdk: How to Add a Native Lib (.So) and a Jar with the Sdk I am Creating
Firebase Authentication State Change Does Not Fire When User Is Disabled or Deleted