Issue
I'm trying to make a new activity from a fragment that has 2 buttons. The button showed up on the emulator, but when I clicked it it doesn't do anything. I've followed many tutorials online but most of them do onClickListener
on a single button. Sorry I'm new to android development.
public class UserFragment extends Fragment implements View.OnClickListener {
Activity context = getActivity();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_user, container, false);
Button register = v.findViewById(R.id.btnAkunRegister);
Button login = v.findViewById(R.id.btnAkunLogin);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnAkunLogin:
Intent moveIntent = new Intent(context, LoginActivity.class);
startActivity(moveIntent);
break;
case R.id.btnAkunRegister:
moveIntent = new Intent(context, RegisterActivity.class);
startActivity(moveIntent);
break;
}
}
}
update: I've tried following the answer from stackoverflow, but it doesn't solve the problem.
Solution
You should set onClickListener
for your buttons. You implement this interface in you
Frgamnet, so you should set this
as listener in your onCreateView()
method.
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_user, container, false);
Button register = v.findViewById(R.id.btnAkunRegister);
Button login = v.findViewById(R.id.btnAkunLogin);
//set listener
register.setOnClickListener(this);
login.setOnClickListener(this);
return v;
}
Answered By - Sergey Melnik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.