Reputation: 349
So I have a fragment with an EditText as well as a floating hint. The thing is when I start the fragment and the EditText already has a value the floating hint covers the text for a brief moment before the hint starts floating. How do I make it so the floating hints starts floating when I start the fragment instead of covering the text?
The picture is what the EditText looks like when the hint is covering the text
Edit: Okay looks like the way I worded the question is a little confusing. So I have a RecyclerView that is a list and when a list item is pressed it starts this fragment (First image from https://i.sstatic.net/JTY8s.jpg Let's call it infoFragment). The user enters a title and details into the infoFragment and the data is saved. Now whenever the user goes back to that same infoFragment the data they entered before should already be there like the second image in the imgur album. The info is saved but when the infoFragment is started for just a second the UI looks like the third image in the imager album for a split second then it looks like like the second image.
Here's my layout xml for the fragment: mExercise is an object from a class that holds values for title and details
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar">
</include>
<android.support.design.widget.TextInputLayout
android:id="@+id/title_textInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<EditText
android:id="@+id/exercise_title"
style="@style/Widget.AppCompat.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/details_textInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<EditText
android:id="@+id/exercise_details"
style="@style/Widget.AppCompat.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Details"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Here's the onCreateView method of my fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_exercise, parent, false);
mToolbar = (Toolbar)v.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(" ");
mTitleField = (EditText)v.findViewById(R.id.exercise_title);
if(mExercise.getTitle() != null) {
mTitleField.setText(mExercise.getTitle());
}
mTitleField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mExercise.setTitle(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
mDescriptionField = (EditText)v.findViewById(R.id.exercise_details);
if(mExercise.getDetails() != null) {
mDescriptionField.setText(mExercise.getDetails());
}
mDescriptionField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mExercise.setDetails(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
return v;
}
Upvotes: 1
Views: 1252
Reputation: 664
Try setting android:hint
on the TextInputLayout
rather than the EditText
<android.support.design.widget.TextInputLayout
android:id="@+id/title_textInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="Title">
<EditText
android:id="@+id/exercise_title"
style="@style/Widget.AppCompat.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
Upvotes: 1
Reputation: 66
Should be fixed for compile 'com.android.support:design:23.0.1'
Upvotes: 0