I am using fragment have to Textfield in it. one is for name and other is phone number. Validation for both work just fine. But when I Toast it says null/false.
EditText tPername = (EditText) getView().findViewById(R.id.txtPer);
This is xml
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtPer"
android:layout_marginBottom="15dp"
android:background="#drawable/textbox"
android:layout_gravity="center_horizontal"
android:inputType="textPersonName"/>
When I Toast R.id.txtPer it says null.
This is my java code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_add_customer, container, false);
view.findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
doAdd(view);
}
});
return view;
}
public void doAdd(View view) {
String MobilePattern = "[0-9]{10}";
EditText tPername = (EditText) getView().findViewById(R.id.txtPer);
EditText tMobnum = (EditText) getView().findViewById(R.id.txtMob);
if (TextUtils.isEmpty(tPername.getText().toString())) {
tPername.setError("Please fill this field");
return;
} else if (!tMobnum.getText().toString().matches(MobilePattern)) {
tMobnum.setError("Mobile number must be entered 10 digits only");
return;
}
}
type a (EditText tPername = (EditText) getView().findViewById(R.id.txtPer);)
in the onViewCreated
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
EditText tPername = (EditText) getView().findViewById(R.id.txtPer);
}
String name= tPername.getText().toString();
This solved my problem.Thanks
Related
I use fragment with view-binding but there is an interesting thing, I can not get the value in the onViewCreated scope, only in the binding button with click event listener scope.
Fragment.java:
public class Fragment extends Fragment {
private FragmentBinding binding;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
return rootView;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding = FragmentBinding.bind(view);
final String amount1 = binding.formAmountInput.getText().toString();
binding.formSaveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String amount2 = binding.formAmountInput.getText().toString();
Toast.makeText(mContext, amount1, Toast.LENGTH_SHORT).show(); // gives nothing, why?
Toast.makeText(mContext, amount2, Toast.LENGTH_SHORT).show(); // works, shows input data
}
});
}
xml part:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/form_amount_layout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:endIconMode="clear_text">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/form__amount_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number|numberDecimal|numberSigned"
/>
</com.google.android.material.textfield.TextInputLayout>
final String amount1 = binding.formAmountInput.getText().toString();
// gives nothing, why?
Well you are getting the content of the field at that particular point in time. And since you just inflated the view and there is no default text, amount1 is empty.
If you add android:text="default text" to your TextInputEditText, your amount1 will have something to read.
amount1 will not automatically update when you edit the text! Hence it only makes sense to call .getText().toString() in the onClick Listener or in a TextWatcher.
MyDao Class isn't responding the right way it gives me each time an error , need help to identify problem. Even though it is copies from a youtube class, and i checked the comment if there is something but everybody seems got it right.
public class AddUserFragment extends Fragment {
private EditText UserId,UserName,UserEmail;
private Button Savebn;
public AddUserFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_add_user, container, false);
UserId=view.findViewById(R.id.txt_user_id);
UserName=view.findViewById(R.id.txt_user_name);
UserEmail=view.findViewById(R.id.txt_user_email);
Savebn=view.findViewById(R.id.bn_save);
Savebn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int userid = Integer.parseInt(UserId.getText().toString());
String username = UserName.getText().toString();
String useremail = UserEmail.getText().toString();
User user = new User();
user.setId(userid);
user.setName(username);
user.setEmail(useremail);
MainActivity.myAppDatabase.myDao().addUser(user);
Toast.makeText(getActivity(),"User added successfully",Toast.LENGTH_SHORT).show();
UserId.setText("");
UserName.setText("");
UserEmail.setText("");
}
});
return view;
}
}
Hi I am new to fragments and I am working with this activity.I populated an edit text but I want to highlight the borders when the user focus on one of the edit text.I created the method focus and I am calling it,but it is not working.
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Fragment_Info extends Fragment {
EditText CompanyName;
EditText CompanyOwner;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fragment__info, container, false);
CompanyName = (EditText) view.findViewById(R.id.CompName);
CompanyOwner = (EditText) view.findViewById(R.id.CompOwner);
focus(CompanyName);
focus(CompanyOwner);
//Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_fragment__info, container, false);
}
public void focus(View view){
view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
v.setBackgroundResource(R.drawable.focus_border_style);
else
v.setBackgroundResource(R.color.colorPrimary);
}
});
}
}
You are applying focus on a view and returning a different view from the method.
Change
return inflater.inflate(R.layout.activity_fragment__info, container, false);
to
return view;
Change
return inflater.inflate(R.layout.activity_fragment__info, container, false);
to
return view;
I'm a beginner in Android :( I'm using a text input layout but I cant catch any value of them! This is my code. For now I want to print the value "usuario" just to check if it has any value. I'm using Butterknife btw D:
public class IngresoFragment extends Fragment {
#Bind(R.id.tilUsuario) TextInputLayout mTilUsuario;
#Bind(R.id.tilPassword) TextInputLayout mTilPassword;
#Bind(R.id.btnIngresar) Button btnIngresar;
#Bind(R.id.tviLibre) TextView tviLibre;
public IngresoFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ingreso, container, false);
ButterKnife.bind(this,view);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
app();
}
void app(){
mTilUsuario.setHint(getResources().getString(R.string.usuario));
mTilPassword.setHint(getResources().getString(R.string.password));
events();
}
void events(){
btnIngresar.setOnClickListener(new View.OnClickListener() {
String usuario = mTilUsuario.getEditText().getText().toString().trim();
String password = mTilPassword.getEditText().getText().toString().trim();
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), usuario, Toast.LENGTH_LONG).show();
}
});
}
in Android text input box is define as EditText just chenge TextInputLayout to EditText
I know this question have been asked many times here but I tried all the answers with no success.
My EditText looses everything I typed after I rotate the screen even though I get the data logged after recreation
I have this code in my fragment
#Override
public void onSaveInstanceState(Bundle saveState){
super.onSaveInstanceState(saveState);
saveState.putString("A1", A1.getText().toString());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
if(savedInstanceState != null) {
Log.d("Novinyo2", savedInstanceState.getString("A1"));
String A1_value = savedInstanceState.getString("A1");
EditText A1_ = (EditText) rootView.findViewById(R.id.txta1);
A1_.setText(A1_value);
}
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
text1 = (TextView) getActivity().findViewById(R.id.accord1);
text2 = (TextView) getActivity().findViewById(R.id.accord2);
text3 = (TextView) getActivity().findViewById(R.id.accord3);
Code = (EditText) getActivity().findViewById(R.id.stationcode);
Name = (EditText) getActivity().findViewById(R.id.stationname);
A1 = (EditText)getActivity().findViewById(R.id.txta1);
A2 = (EditText)getActivity().findViewById(R.id.txta2);
text1.setOnClickListener(this);
text2.setOnClickListener(this);
text3.setOnClickListener(this);
}
here
#Override
public void onSaveInstanceState(Bundle saveState){
super.onSaveInstanceState(saveState);
saveState.putString("A1", A1.getText().toString());
}
you are saving an empty Bundle and then you are putting a value to bundle,
so when you are trying to retrieve those data the bundle is empty.
try that
#Override
public void onSaveInstanceState(Bundle saveState){
saveState.putString("A1", A1.getText().toString());
super.onSaveInstanceState(saveState);
}
UPDATE
on your Manifest file do you have that attribute android:configChanges="orientation|screenSize" on your activity tag?
You need to call super.onSaveInstanceState(saveState) after saving your data.
The resulting code:
saveState.putString("A1", A1.getText().toString());
super.onSaveInstanceState(saveState);