I have an application that I had implemented Fragment Pager Adapter for swiping pages using fragment. Then, on the fragment, it have a EditText and a TextView. I want to implement EditText.addTextChangedListener into the fragment, so at MainActivity.java
package testing.com.testing;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import layout.page1;
import layout.page2;
public class MainActivity extends AppCompatActivity {
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new SamplePagerAdapter(
getSupportFragmentManager()));
}
public class SamplePagerAdapter extends FragmentPagerAdapter {
public SamplePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new page1();
} else
return new page2();
}
@Override
public int getCount() {
return 2;
}
}
}
My fragment.java
package layout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import testing.com.testing.R;
public class page1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.page1, container,
false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
EditText text;
TextView textv;
text = (EditText) getView().findViewById(R.id.p1Name);
textv = (TextView) getView().findViewById(R.id.textView);
text.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) {
}
@Override
public void afterTextChanged(Editable s) {
textv.setText(s);
}
});
}
}
But it doesn't work, I get error on TextWatcher, error: cannot find symbol class TextWatcher, Execution failed for task ':appcompileDebugJavaWithJavac'. Can anyone please help me..
Assuming R.id.p1Name
and R.id.textView
in your fragment's layout
Inside your fragment in onActivityCreated(...)
method get the reference to the edittext and set the addTextChangedListener over there.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
text = (EditText) getView().findViewById(R.id.p1Name);
textv = (TextView) getView().findViewById(R.id.textView);
text.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) {
}
@Override
public void afterTextChanged(Editable s) {
textv.setText(s);
}
});
}