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.
Related
I am trying to add a simple click on a button in a Fragment but the event is ignored.
This is the code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View i = inflater.inflate(R.layout.fragment_id_upload, container, false);
btn = i.findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i("data","ok clickled");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("hjgdfhfghfghf gfdgf");
AlertDialog dialog = builder.create();
dialog.show();
}
});
return i;
}
I don't know what is wrong in the code?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View i = inflater.inflate(R.layout.fragment_id_upload, container, false);
btn = i.findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i("data","ok clickled");
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setMessage("hjgdfhfghfghf gfdgf");
AlertDialog dialog = builder.create();
dialog.show();
}
});
return i;
}
Try requireActivity() instead of getActivity()
ok I have fixed this by using FragmentManager
like :-
FragmentManager fragment = getSupportFragmentManager();
fragment.beginTransaction().replace(R.id.fragmentContainerView, fragment_id_upload.class,null).commit();
R.id.fragmentContainerView //this is fragment container view
for more details:- go to Android Studio - Fragment onClickListener not working
I have implemented an onclicklistener on a button to open Gallery when I click the button in the app nothing happens
Code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_statusfragment,container,false);
imageView = view.findViewById(R.id.imageView);
button = (Button)view.findViewById(R.id.button2);
textView = view.findViewById(R.id.textView);
view.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Upload screenshot",Toast.LENGTH_LONG).show();
if(ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
2000);
}else {
Toast.makeText(getActivity(), "Choose Screenshot", Toast.LENGTH_LONG).show();
imageView.setVisibility(View.VISIBLE);
button.setVisibility(View.VISIBLE);
textView.setVisibility(View.VISIBLE);
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(intent,1000);
}
}
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_statusfragment, container, false);
}
it also does not display the Toast message
Instead of return inflater.inflate(R.layout.fragment_statusfragment, container, false);
do return view;
Very first try to do all the UI work in onViewCreated() method of Fragment.
Here you are not returning the initialized view above in the last and use button variable to implement click listener.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_statusfragment,container,false);
imageView = view.findViewById(R.id.imageView);
button = (Button)view.findViewById(R.id.button2);
textView = view.findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Upload screenshot",Toast.LENGTH_LONG).show();
if(ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
2000);
}else {
Toast.makeText(getActivity(), "Choose Screenshot", Toast.LENGTH_LONG).show();
imageView.setVisibility(View.VISIBLE);
button.setVisibility(View.VISIBLE);
textView.setVisibility(View.VISIBLE);
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(intent,1000);
}
}
}
});
// Inflate the layout for this fragment
return view;
}
You are already initializing the inflater.inflate(R.layout.fragment_statusfragment, container, false); at the beginning and assigning it to View view, so just return view; at the end.
Java Code:
`
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
viewRoot = inflater.inflate(R.layout.fragment_contacts, container,
false);
Button btnAdd = (Button) viewRoot.findViewById(R.id.btn_add_contacts);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,1);
Toast.makeText(getContext(), "Add Contacts",
Toast.LENGTH_SHORT).show();
}
});
return viewRoot;
}
`
Now i want to select the contacts from a list and then add them to a list view.
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));
}
}
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);
}
});