Currently I have a fragment_one.xml which has 5 CardViews on it and and each card has a Button on it that is meant to go to separate XML pages (Lesson_One,Lesson_Two etc...) but with the code I have in OneFragment.java, both buttons are opening Lesson_Two
How I can I fix this? Here is my code
FragmentOne.java
public class OneFragment extends Fragment{
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_one, container, false);
intent = new Intent(getActivity(), LessonOne.class);
final Button button = (Button) root.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
intent = new Intent(getActivity(), LessonTwo.class);
final Button button2 = (Button) root.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return root;
}
}
You're assigning intent twice, effectively overwriting the first intent with the second.
So no matter which click event is triggered LessonTwo.class is the activity that is started.
A simple fix would be to create the intents inside the click handlers like
public class OneFragment extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_one, container, false);
final Button button = (Button) root.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getActivity(), LessonOne.class));
}
});
final Button button2 = (Button) root.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getActivity(), LessonTwo.class););
}
});
return root;
}
}
This makes it explicit which click handler starts what activity
Alternative answer - implement the click listener on the class itself.
This cleans up the onCreateView method. You also don't need to "capture" the buttons to set their listeners.
public class OneFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_one, container, false);
root.findViewById(R.id.button1).setOnClickListener(this);
root.findViewById(R.id.button2).setOnClickListener(this);
return root;
}
#Override
public void onClick(View v) {
Class clz = null;
switch (v.getId()) {
case R.id.button1:
clz = LessonOne.class;
case R.id.button2;
clz = LessonTwo.class;
}
if (clz != null) startActivity(new Intent(getActivity(), clz));
}
}
Related
I'm trying to implent the next code in Android Studio and it does not work.
I want to pass from a Fragment (GalleryFragment) to an Activity (postropa) with a button.
I have linked the botton with the function (BotonPulsado) and I don't know what is wrong (In the design view).
Design View
Code:
import (...)
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
public void BotonPulsado(View view) {
Intent intent = new Intent(getContext(), postropa.class);
startActivity(intent);
}
}
You should create Button variаble as a class field.
private GalleryViewModel galleryViewModel;
Button button; <<-------
After that you need to define it in method onCreateView()
button = (Button) findViewById(R.id.button2);
And set onClickListener() on this button to handle the call.
There you must call the method that starts the activity.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BotonPulsado();
}
});
Your final code:
import (...)
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
Button button;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BotonPulsado();
}
});
return root;
}
public void BotonPulsado() {
Intent intent = new Intent(getContext(), postropa.class);
startActivity(intent);
}
}
I have the error noted in the title: error: not an enclosing class: Context
I already tried in some other forums to solve this problem but they couldn't help, I checked youtube and other questions on stackoverflow but couldn't find an answer to this problem.
My code looks like this:
public class TermineFragment extends Fragment {
private Button button;
Context c;
#Override
public void onAttach(Context c) {
super.onAttach(c);
Context context = c;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull final LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_3, container, false);
button = view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(Context.this, AnmeldungButton.class));
startActivity(intent);
}
});
return view;
}
}
This produces the error:
error: not an enclosing class: Context
Which comes from the line:
Intent intent = new Intent(getActivity(Context.this,AnmeldungButton.class));
I want that my button in the Fragment AnmeldungButton.java is I mean in the Activity but I hope you understand me ...
You can use getContext() for start activity in fragment
Intent intent = new Intent(getContext(), AnmeldungButton.class);
startActivity(intent);
Use this code
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),AnmeldungButton.class);
startActivity(intent);
}you a
});
Here is the problem you are using are passing parameter in the wrong way.
Intent(Context packageContext, Class<?> cls)
public class TermineFragment extends Fragment {
Context c;
#Override
public void onAttach(Context c) {
super.onAttach(c);
this.c = c; //this is one of the best way to get context of the activity to which the particular activity is associated with
}
#Nullable
#Override
public View onCreateView(#NonNull final LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_3, container, false);
button = view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(c,AnmeldungButton.class);//Pass the context like this.
startActivity(intent);
});
return view;
}
}
I am still relatively new in programming with Java and I would like to open a fragment with several buttons different activities. However, I always get an error (... is already defined in ...) at the following location:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
Also in other variants it did not work and there were even more errors.
Here is the full code of my Fragment:
public DashboardFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
ImageButton button = (ImageButton) rootView.findViewById(R.id.stundenplanbtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Stundenplan.class);
startActivity(intent);
}
});
return rootView;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
ImageButton button2 = (ImageButton) rootView.findViewById(R.id.vertretungsbtn);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Vertretungsplan.class);
startActivity(intent);
}
});
return rootView;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
ImageButton button2 = (ImageButton) rootView.findViewById(R.id.essenbtn);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Essen.class);
startActivity(intent);
}
});
return rootView;
}
}
What you actually want is this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
ImageButton button = (ImageButton) rootView.findViewById(R.id.stundenplanbtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Stundenplan.class);
startActivity(intent);
}
});
ImageButton button2 = (ImageButton) rootView.findViewById(R.id.vertretungsbtn);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Vertretungsplan.class);
startActivity(intent);
}
});
return rootView;
}
Also, in the Android specific context you don't really all the ImageButton casting etc. since you're not using any specific method of the ImageButton class and since it extends the View class you can simply use:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
rootView.findViewById(R.id.stundenplanbtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Stundenplan.class);
startActivity(intent);
}
});
rootView.findViewById(R.id.vertretungsbtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Vertretungsplan.class);
startActivity(intent);
}
});
return rootView;
}
Note the only thing here is not creating the buttonvariables/references, so it's just a small improvement. But if this is confusing you at the moment just ignore and use the first one.
I have a layout with a buttons. I use FragmentActivity:
public class slidepage extends FragmentActivity {
private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
Button Back = (Button) findViewById(R.id.back);
Back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
slide class:
public class slider extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup view;
if(GlobalConfig.tryb) {
view = (ViewGroup) inflater.inflate(R.layout.n_podglad, container, false);
} else {
view = (ViewGroup) inflater.inflate(R.layout.d_podglad, container, false);
}
/* Powrót */
Button Back = (Button) view.findViewById(R.id.back);
Back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//onBackPressed();
}
});
return view;
}
}
Unfortunately my button not works. I receive null reference in log. What is wrong? How can I set OnClickListener on my buttons in viewpager?
Inside ScreenSlidePagerAdapter in instantiateItem method write your button click not in your slidepage
#Override
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout= inflater.inflate(R.layout.pager_layout, null);
Button Back= (Button) layout.findViewById(R.id.back);
Back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
return layout;
}
I have an an Android app with an Intro class.
This Intro class has got three fragments.
in fragment 3 (IntroPage3) i would like to set an onclicklistener with an intent form the IntroPage3 to Overview.class like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragementView = inflater.inflate(R.layout.intro_page1, container, false);
Button FinishIntroButton = (Button) FragementView.findViewById(R.id.FinishIntroButton);
FinishIntroButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent = new Intent(IntroPage3.this, Overview.class);
startActivityForResult(intent, 0);
}
});
return FragementView;
}
Problem: is this line:
intent = new Intent(IntroPage3.this, Intro.class);
Error message:
use intent = new Intent(getActivity(), Overview.class);
public class Fragment3 extends Fragment{
#Nullable
#Override
public View onCreateView(#NonNull final LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
View view;
view = inflater.inflate(R.layout.page_4, container, false);
Button button=view.findViewById(R.id.exit);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent;
intent=new Intent(getActivity(),FragmentLogin.class);
startActivity(intent);
}
});
return view;
}
}
You must use getActivity() instead of IntroPage3.this
WHY
getActivity() in a Fragment returns the Activity the Fragment is currently associated with.
Button FinishIntroButton = (Button) FragementView.findViewById(R.id.FinishIntroButton);
FinishIntroButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent = new Intent(getActivity(), Overview.class);
startActivityForResult(intent, 0);
}
});