Button click listener in a fragment - android

i know this question has been asked thousands of times, but i have this code and i have no idea it doesn't work. What i want to do is having a button in a fragment that when it is clicked, it has to show a toast.
public class MyFragment1 extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_my_fragment1, container, false);
Button bot = (Button)rootView.findViewById(R.id.button3);
bot.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "button clicked", Toast.LENGTH_LONG);
}
});
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
With this code literally nothing happens, it doesn't even show an error message. I've tried different solutions but none of them have solved the problem

You forgot to show the Toast, So kindly show the Toast using show() like
Toast.makeText(getActivity(), "button clicked", Toast.LENGTH_LONG).show();
There is actually no error, that is why you are not getting any error messages in your Logcat. Only thing is that you are not showing the Toast
See the docs for more info.

Related

How to create a button inside a Fragment?

I am creating a fragment where if I click on the button it should show a Toast message
This is my code:
public class Aboutus extends Fragment implements View.OnClickListener{
Button ps,fb;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.about_layout, container, false);
ps = (Button) view.findViewById(R.id.ps_btn);
fb = (Button) view.findViewById(R.id.fb_btn);
try {
ps.setOnClickListener(this);
}catch (ActivityNotFoundException exception) {
}
//fb.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fb_btn:
Toast.makeText(getActivity(), "This is my Toast message!", Toast.LENGTH_LONG).show();
break;
case R.id.ps_btn:
Toast.makeText(getActivity(), "This is my Toast message!", Toast.LENGTH_LONG).show();
break;
}
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("ABOUT US");
}
}
My app stops working when I open the fragment.
It does't seem like having issue with button. There may be a problem with implementing fragment inside activity. Once Watchout Fragment implementation. Thanks.

No such instance field: 'bottomNavigationView'

I am using new released Bottom Navigation View from android support library - 25.0.0. And I am trying to setOnNavigationItemSelectedListener and nothing is happened. When I tried to debug the project, I see the error: "No such instance field: 'bottomNavigationView'", but my bottomNavigationView is initialized and the method "getMaxItemCount" works perfectly.
I realized that it is not only the "navigationListener" problem, but also onClickListener. Because this code:
bottomNavigationView = (BottomNavigationView) viewHierarchy.findViewById(R.id.tender_bottom_navigation);
bottomNavigationView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast toast = Toast.makeText(getActivity(), "clicky-clicky", Toast.LENGTH_LONG);
toast.show();
}
});
also provides the same error.
I saw the same mistakes with the boolean values here on site, but the most common answer for this was "restart android studio/clean the project". I have done it. Do you have any thoughts on this?
May be there is no way to implement bottomNavigationView inside a fragment?
EDIT: I tried this code in activity class and it works! So why does he lose the field in the fragment? That is still a riddle to me.
EDIT 2: The whole fragment code.
public class NewTenderFragment extends Fragment {
BottomNavigationView bottomNavigationView;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View viewHierarchy = inflater.inflate(R.layout.fragment_new_tender, container, false);
bottomNavigationView = (BottomNavigationView) viewHierarchy.findViewById(R.id.tender_bottom_navigation);
int t = bottomNavigationView.getMaxItemCount(); //this returns "5"
bottomNavigationView.setOnClickListener(new View.OnClickListener() { //this returns such error
#Override
public void onClick(View view) {
Toast toast = Toast.makeText(getActivity(), "clicky-clicky", Toast.LENGTH_LONG);
toast.show();
}
});
return viewHierarchy;
}

Navigate from one fragment to another fragment on click of a button

This is the code II implemented by googling and reading another stack overflow answers. but any answer is not working for me.
Following code this error - "No view found for id 0x7f09009e
Please help me to implement this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//////more code here //////
OnClickListener listner = new OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment=null;
if(v==rootView.findViewById(R.id.Button)){
fragment = new SortListFragment();
}
FragmentManager manager =getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_sort, fragment);
transaction.commit();
}
};
ImageButton btn = (ImageButton)rootView.findViewById(R.id.button);
btn.setOnClickListener(listner);
return rootView;
}
ButtonOnClickInterface interface;
public void onCreateView(//parameters){
View view=//inflate the view;
Button button =(Button)view.findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnclickListener{
#Override
public void onClick(View v){
try{
interface.onClick();
}catch(Throwable e){
//may be null
}
}
});
}
#Override
public void onAttach(Context c){
super.onAttach(c);
try{
interface=(ButtonOnClickInterface)c;
}catch(Throwable e){
//not implemented
}
}
And your interface
interface ButtonOnClickInteface{
void onClick();
}
Implement this interface in your activity.
Add this line above on click listner:
rootview = inflater.inflate(R.layout.<your layout xml>, container,
false);
I think you forgot to provide layout file in which you have put buttons and all.
So please provide below line in your code after "super.OnCreateView()"
rootView = inflater.inflate(R.layout.your_layout_file, container, false);
And then check it is working or not, hope this will work

Fragment setRetainInstance not works (Android support lib) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android fragments setRetainInstance(true) not works (Android support library)
I wrote a simple test project, but I cant understand why I always receive savedInstanceState = null in lifecycle methods onCreate, onCreateView and onActivityCreated. I change the screen orientation, see the log, but state not saved.
Tell me please where is my mistake.
Thanks.
The code of fragment class is:
public class TestFragment extends Fragment {
private String state = "1";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
//never works
state = savedInstanceState.getString("state");
}
//always prints 1
Toast.makeText(getActivity(), state, Toast.LENGTH_SHORT).show();
return inflater.inflate(R.layout.fragment_layout, container, false);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("state", "2");
Log.e("", "saved 2");
}
}
EDIT
When i try to use setRetainInstance i have no result again((( I change a state to 2 using btn1, but after changing orientation i see 1 when pressing on btn2. Help please(
public class TestFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
super.onCreate(savedInstanceState);
}
private String state = "1";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
//button for changing state
((Button)view.findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
state = "2";
}
});
//button for showing state
((Button)view.findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(getActivity(), state, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
I have seen the same issue. But you can save the instate state in an other way, thanks to the method setRetainInstance(true), all your class data are saved automatically.
#Override
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
super.onCreate(savedInstanceState);
}
EDIT
Try to also add these params when you declare your activities :
<activity
android:name="MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize" />

Button in tab fragment

I was following tutorials on how to make Tabs using fragments.
each tab has this in the java files:
public class rdfri extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
return (LinearLayout)inflater.inflate(R.layout.rdfri, container, false);
}
}
I would like to try and get a imageButton in the fragment. I figured image bottons work with this code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rdmon);
ImageButton rainbowbook = (ImageButton) findViewById(R.id.imageButton1);
rainbowbook.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = Intent(rdmon.this, RainbowBookActivity.class);
rdmon.this.startActivity(myIntent);
}
});
}
So how would I go about getting the button code in the fragment code?
Thanks.
Put the button code in the overridden onActivityCreated method in your fragment class.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImageButton rainbowbook = (ImageButton) findViewById(R.id.imageButton1);
rainbowbook.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = Intent(rdmon.this, RainbowBookActivity.class);
rdmon.this.startActivity(myIntent);
}
});
}
This assumes of course that your imagebutton is contained in your fragment layout.

Categories

Resources