Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have a simple fragment inside I have EditText. I'm trying to set the EditText setText onCreateView, onResume and onStart but I couldn't get the text to show. setHint works just fine unlike setText. any idea what could be the reason?
Thanks in advance!!

What I have tried:

XML:
EditText is inside ScrollView
HTML
<EditText
	android:id="@+id/output_value"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:inputType="number"
	android:maxLength="3"
	android:hint="@string/output_function_value_hint"
	android:layout_marginStart="20dp"
	android:layout_marginEnd="20dp"
	android:saveEnabled="false"/>


onCreateView:
Java
private View rootView;
private EditText outputEditText;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
	rootView = inflater.inflate(R.layout.fragment_io,container,false);	

	outputEditText = rootView.findViewById(R.id.output_value);       
	outputEditText.addTextChangedListener(new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) { }
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
		@Override
		public void afterTextChanged(Editable s) {
			if(!TextUtils.isEmpty(s.toString())){
				try{
					if(Integer.valueOf(outputEditText.getText().toString()) > MAX_OUTPUT_VALUE || Integer.valueOf(outputEditText.getText().toString()) <MIN_OUTPUT_VALUE ){
						Toast.makeText(getSmartControlActivity(), getString(R.string.output_function_value_hint),Toast.LENGTH_SHORT).show();
						outputEditText.setText("");
					}
				}catch(NumberFormatException ex){
					ex.printStackTrace();
					outputEditText.setText("");
				}
			}
		}
	});

  //....

	return rootView;
}


Trying to setText onResume or onStart with no luck
Java
@Override
    public void onResume() {
        super.onResume();
        outputEditText.setText("42");
    }

    @Override
    public void onStart(){
        super.onStart();
        
        outputEditText.setText("42"); //,TextView.BufferType.EDITABLE
    }
Posted
Comments
David Crow 3-Jan-19 16:30pm    
If I were to hazard a guess, I'd say the calls to setText("42") in onResume() and onStart() are triggering the afterTextChanged() handler, thus erasing the text.

Why don't you comment out the addTextChangedListener() handler to narrow the problem down?
Samira Radwan 4-Jan-19 8:07am    
@David Crow, you are absolutely right, removing the addTextChangedListner() eliminate s the problem. my question is If i can't use addTextChangedListner() what should be done to handle any EditText validations? or maybe is there a way to set the text without triggering it? i know i can't do it onCreateView
David Crow 4-Jan-19 9:05am    
"...what should be done to handle any EditText validations?"

See here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900