onClick inside fragment called on Activity - android

i am encapsulating stuff into a fragment at the moment and run into a problem that is hard to google.
Inside my fragment are some buttons with onClick attributes but they are called on the Activity rather the fragment from the android system - this makes encapsulating a bit clumsy. Is there a way to have the reflection stuff from onClick to call on the fragment? The only solution to this I see at the moment is not to use onClick in the xml and set click-listeners inside the fragment via code.

I spoke to some googlers # #adl2011 - they recognize the problem and perhaps there will be a fix of that in the future. Until then - one should use .setOnClick in the Fragment.

The problem is that when layout's are inflated it is still the hosting Activity that is receiving the button clicks, not the individual Fragments.
I prefer using the following solution for handling onClick events. This works for Activity and Fragments as well.
public class StartFragment extends Fragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_start, container, false);
Button b = (Button) v.findViewById(R.id.StartButton);
b.setOnClickListener(this);
return v;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.StartButton:
...
break;
}
}
}
Then problem is gone.

It works for me
Add import:
import android.view.View.OnClickListener;
Fragment.java
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = (ViewGroup) inflater.inflate(R.layout.fragment, container, false);
Button mButton = (Button) rootView.findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
return rootView;
}

ISSUE:
1.In XML onClick attribute will call activity's public method.
2.Fragment's public method not called.
3.Fragment reusability.
SOLUTION:
In Fragment's layout add this to the View.
android:onClick="onFragmentViewClick"
In each activity the fragment may belong..
public void onFragmentViewClick(View v) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (fragment != null && fragment.isVisible()) {
if (fragment instanceof SomeFragment) {
((SomeFragment) fragment).onViewClicked(v);
}
}
}
In the Fragment include this method..
public void onViewClicked(View v) {
switch (v.getId()) {
case R.id.view_id:
Log.e("onViewClicked", "yehhh!!");
break;
}
}

You could have the listener that is being called in the activity forward the call onto a listener in the fragment. You should have a reference to the fragment inside of the FragmentActivity to pass the call on. You will have to cast to call the method or have your fragment implement an interface you define. I know that isn't the best solution but it will work. You could also use the tag of a button to specify the method name to call if you wanted. Hope this helps a bit.

Try this...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_ipadditional_users, container, false);
FloatingActionButton button7 = (FloatingActionButton) rootView.findViewById(R.id.fab_ip_additional_user);
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), IPAdditionalUsersEdit.class);
startActivity(intent);
}
});
return rootView;

Related

fragment error upon starting new activity

I have trouble in starting new activity within the fragment class. Everytime, I click the specified image button it will have an error stating that it is undefined from another class. This class is the holder of the said fragment activity.
Here is the code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
rootView = inflater.inflate(R.layout.activity_seat_plan, container, false);
button1 = (ImageButton)rootView.findViewById(R.id.ImageButton1);
return rootView;
}
Upon clicking the button
public void AssignAddress(View view){
Intent i = new Intent(getActivity(), AssignSeat.class);
startActivity(i);
}
Here's the logcat
03-15 23:27:56.842: W/dalvikvm(23000): threadid=1: thread exiting with uncaught exception (group=0x40bdc438)03-15 23:27:56.852: E/AndroidRuntime(23000): FATAL EXCEPTION: main03-15 23:27:56.852: E/AndroidRuntime(23000): java.lang.IllegalStateException: Could not find a method AssignAddress(View) in the activity class com.example.mcr.InstructorMenu for onClick handler on view class android.widget.ImageButton with id 'ImageButton1'
First, show more code; secondly, the code is complaining about the non-existent method. Here is what might help you:
In your fragment, you need to implement the click listener for your button (which MUST be in the fragment layout as well).
Inside the click method, you should then start the new activity - you don't even need a method for that.
Again, more code of your activity structure might help and the rest of your fragment.
I think you are trying to hook the AssignAddress(View view) method from the xml like this android:onClick="AssignAddress"
which will not work because after the user clicks this button the compiler will try to find the method in the activity that the fragment resides in but not in the fragment where you have declared the method. So You must remove this from the xml and change to the following
public class YourFragment extends Fragment implements
OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
rootView = inflater.inflate(R.layout.activity_seat_plan, container, false);
button1 = (ImageButton)rootView.findViewById(R.id.ImageButton1);
button1.setOnClickListener(this);
return rootView;
}
////your other codes
///////
//the OnClickListener
#Override
public void onClick(View v) {
Intent i=null;
switch (v.getId()) {
case R.id.ImageButton1:
i= new Intent(getActivity(), AssignSeat.class);
startActivity(i);
break;
default:
break;
}
}
}

Open a new activity with a button from a static fragment?

I have an Activity (main) with four fragments. I want to add one button to the third fragment that opens another Activity (secondary) with three fragments. When I press back I want to return to the main Activity.
I have searched for an answer, but I can't find one that works. My fragments' codes are inside the main activity, and the fragments are static because otherwise the app Force Closes when rotating to landscape mode.
I took the button code from here:
http://developer.android.com/reference/android/widget/Button.html
public class fragmentFour extends Fragment {
Intent intent = new Intent(getActivity(), musikteori_ackord.class);
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout1, container, false);
}
final Button button = (Button) findViewById(R.id.buttonAckord);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
}
Errors:
- Cannot resolve symbol 'setOnClickListener'
- Cannot resolve symbol 'v'
So just add a Button in the third fragment and onClick of that button, start an Intent that opens up the new desired Activity containing the other three fragments.
[EDIT]
Try this:
public class fragmentFour extends Fragment {
Intent intent;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_layout1, container, false);
intent = new Intent(getActivity(), musikteori_ackord.class);
final Button button = (Button) root.findViewById(R.id.buttonAckord);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return root;
}
}

Unable to switch from Fragment to Activity / Activity to Fragment in Android Programming

Currently, I am mainly using Fragments to connect to Facebook.
However, for the other codes, I am using normal activites (no Fragments).
My issue now is that I wish to have a button to link from my "Home Page" to the Fragment, and from the Fragment back to my "Home Page"
I am unable to do so.
I tried to use the same code to switch between activities for this but it does not work.
Is there a way to Link Normal Activities to Fragments and Vice Versa ? Or can they only link to each other ?
This is my Code:
public class SplashFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.splash, container, false);
// return view;
Button btnNextScreen = (Button) view.findViewById(R.id.btnNextScreen);
// Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
//Listening to button event
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(**getApplicationContext()**, SecondScreenActivity.class);
startActivity(nextScreen);
}
});
return view;
}
}
I am getting an error at getApplicationContext().
If I change it to getActivity(), they will prompt with another error that they are expecting to switch to a Fragment, not an activity.
Thank you for your help !
Regards,
AndroidStudent
use this getActivity() if u want to navigate from fragment to activity.
Intent nextScreen = new Intent(getActivity(), SecondScreenActivity.class);
startActivity(nextScreen);
do like this:
Intent i = new Intent(getActivity(), activityname.class);
i.putExtra("key", value);
getActivity().startActivity(i);
In my case, I am also unable to find the class from fragment then I write this code and it works;
public class userprofile extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_userprofile, container, false);
final ImageView iconoptions = view.findViewById(R.id.iconoptions);
iconoptions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),com.example.miniproject. options.class));
}
});
return view;
}
}

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.

Fragment implements OnClickListener

I've got an application that I'm modernizing. One step of this process is changing to a Fragment based layout (using the Fragments from the support library).
I converted my Activities into Fragments, and got the layout working nicely (using a ViewPager, cool stuff!)
I was having my Activities implement OnClickListener for all of my button-pressing needs. I have the new Fragment incarnations doing the same thing of course, but it looks like "onClick" is never getting hit. Is there something special about Fragments that prevents them from working this way?
Just do one this
public class fragmentOne extends Fragment implements OnClickListener {
Button myButton;
#Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
View myView = inflater.inflate(R.layout.fragment_1, container, false);
myButton = (Button) myView.findViewById(R.id.myButton);
myButton.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View v) {
// implements your things
}
}
very simple
I will Focus to use the OnClick action for global access, You have to do like this is your project, Must Implement the View.OnClickListener, then Override the Method
OnClick(), In OnCreateView() have to do like this button_submit.setOnClickListener(this); for the Views you need,
Please see the below code for Clear Answer,Thankyou.
public class New_Project extends Fragment implements View.OnClickListener{
private View mView;
private Button button_submit;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_newproject, container,false);
button_submit=(Button)mView.findViewById(R.id.button_submit);
button_submit.setOnClickListener(this);
return mView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_submit:
//do your stuff
break;
}
}
}
I want to comment on Abhijit Chakra answer but it seems that I need to have 50 reps for that. For those who are wondering if you can't use Abhijit's answer, it is because of:
public void OnClick(View v) {
// implements your things
}
You need to make sure that it is onClick, NOT OnClick. Thankfully Android Studio internal error message come to rescue.
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
switch (v.getId()) {
case R.id.imgView1:
Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
updateImage();
break;
case R.id.imgView2:
Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
updateImage();
break;
case R.id.imgView3:
Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
updateImage();
break;
default:
break;
}

Categories

Resources