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
Related
So, I wanted to make an app that has the main activity in which I can switch between two fragments containing a number randomizer and the other one a stopwatch. The stopwatch, meanwhile, must not stop if I switch to another fragment (it may only stop after I click again a start/stop button). I can switch between them normally, but when I try to generate a random number via button or I try to start the stopwatch, the app crashes. This is what I tried:
Stopwatch fragment:
public class frStopwatch extends Fragment {
View view;
EditText etTime;
Button btnStartStop;
Button btnReset;
Chronometer chronometer;
private boolean running;
private long pauseOffset;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_fr_stopwatch, container, false);
btnStartStop = (Button) view.findViewById(R.id.btnStartStop);
btnReset = (Button) view.findViewById(R.id.btnReset);
btnStartStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chronometer.start();
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chronometer.setBase(SystemClock.elapsedRealtime());
}
});
return view;
}
}
Randomizer fragment:
public class frRandomizer extends Fragment {
View view;
Button btnRandom;
EditText etShowNum;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_fr_randomizer, container, false);
btnRandom = view.findViewById(R.id.btnRandom);
etShowNum = view.findViewById(R.id.etShowNum);
btnRandom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Random r = new Random();
etShowNum.setText(r.nextInt(9));
}
});
return view;
}
}
The app crashes on both emulator and mobile phone.
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;
}
}
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
I have a Activity with TabLayout and ViewPager. I have two fixed buttons floating on top of the activity at bottom right corner. These two buttons are Plus and Minus buttons for increasing/decreasing the font sizes of the text inside the fragment. I created an Interface inside Activity and implemented it inside the fragment where I am changing the textsize of TextView.
Problem is that it does not work at the time when I click the button. When I swipe the fragment then it shows with updated textsize. Am I missing something? Is this the correct way?
Inside Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post_single);
//....
onCoachMark();
//....
}
public void onCoachMark() {
final FrameLayout overlayFramelayout = new FrameLayout(getApplicationContext());
setContentView(overlayFramelayout);
View view = getLayoutInflater().inflate(R.layout.post_single, overlayFramelayout, false);
final View overlay_view = getLayoutInflater().inflate(R.layout.zoom_overlay, overlayFramelayout, false);
final ImageView ivPlus = (ImageView) overlay_view.findViewById(R.id.ivPlus);
final ImageView ivMinus = (ImageView) overlay_view.findViewById(R.id.ivMinus);
overlayFramelayout.addView(view);
overlayFramelayout.addView(overlay_view);
ivPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommonFunctions.FONT_SIZE += 3;
CommonFunctions.putMySharedPreferences(getApplicationContext(), "fontSize", String.valueOf(CommonFunctions.FONT_SIZE));
iCoachMark.onTextSizeChange();
}
});
ivMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommonFunctions.FONT_SIZE -= 3;
CommonFunctions.putMySharedPreferences(getApplicationContext(), "fontSize", String.valueOf(CommonFunctions.FONT_SIZE));
iCoachMark.onTextSizeChange();
}
});
}
public interface ICoachMark {
void onTextSizeChange();
}
public void setOnCoachMark(ICoachMark iCoachMark) {
this.iCoachMark = iCoachMark;
}
Inside Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.post_content, container, false);
tvTitle = (TextView) rootView.findViewById(R.id.tvTitle);
tvDescription = (TextView) rootView.findViewById(R.id.tvDescription);
svPost = (NestedScrollView) rootView.findViewById(R.id.svPost);
String fs = CommonFunctions.getMySharedPreferences(getActivity().getApplicationContext(), "fontSize");
if (fs.matches("")) {
CommonFunctions.putMySharedPreferences(getActivity().getApplicationContext(), "fontSize", String.valueOf(CommonFunctions.FONT_SIZE));
} else {
CommonFunctions.FONT_SIZE = Integer.valueOf(fs);
tvTitle.setTextSize(CommonFunctions.FONT_SIZE);
tvDescription.setTextSize(CommonFunctions.FONT_SIZE);
}
((PostActivity) getActivity()).onTextSizeChange(new PostActivity.ICoachMark() {
#Override
public void onTextSizeChange() {
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, CommonFunctions.FONT_SIZE);
tvDescription.setTextSize(TypedValue.COMPLEX_UNIT_SP, CommonFunctions.FONT_SIZE);
}
});
}
I am new to Java/android develop and trying to follow instructions from android site but cannot find clear answer to implementing onclicklistener in fragment. Please help me why its keep crashing in runtime when I click.
public class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
try this
use this
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
instead of this
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
change your code as below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
return v;
}
Try this
public class TestFragment extends Fragment {
View v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
Move your code from onCreate() method to onCreateView() method and change the following code
From
(TextView)getActivity().findViewById(R.id.text_show);
to
(TextView)v.findViewById(R.id.text_show);