I have given the code i worked out and it works fine but i dont no how to add one more button to redirect to another activity
public class courses extends Fragment {
Intent intent;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.courses, container, false);
intent = new Intent(getActivity(), software_course.class);
final Button button = (Button) root.findViewById(R.id.Software_Course);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return root;
} }
Only some tips before the real answer:
Use first capitalized letter for classes' name
Don't use first capitalized letter for ids
Don't create differents new OnClickListener instance as below, instead implement the interface
The global intent variable hasn't a lot of sense
Now here is the 'rapid' answer:
public class Courses extends Fragment {
Intent intent, anotherIntent;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.courses, container, false);
intent = new Intent(getActivity(), Software_course.class);
anotherIntent = new Intent(getActivity(), YourSecondActivityName.class);
final Button button = (Button) root.findViewById(R.id.software_Course);
final Button button2 = (Button) root.findViewById(R.id.software_Course2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(anotherIntent);
}
});
return root;
}
}
Here is what I recommend:
public class Courses extends Fragment implements View.OnClickListener {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.courses, container, false);
root.findViewById(R.id.software_Course).setOnClickListener(this);
root.findViewById(R.id.software_Course2).setOnClickListener(this);
return root;
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.software_Course:
startActivity(new Intent(getActivity(), Software_course.class));
break;
case R.id.software_Course2:
startActivity(new Intent(getActivity(), YourSecondActivityName.class));
break;
}
}
}
In your courses.xml add One more button with different id of other button.
<Button
android:id="#+id/hardware"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
In your courses.java initialize button and use setonClickListener.
public class courses extends Fragment {
Intent intent;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.courses, container, false);
intent = new Intent(getActivity(), software_course.class);
final Button button = (Button) root.findViewById(R.id.Software_Course);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
Button Hardware=(Button)root.findViewById(R.id.Hardware_Course);
Hardware.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do stuff you want to do on click of button
intent = new Intent(getActivity(), hardware_course.class);
startActivity(intent);
}
});
return root;
}
}
Related
This is a really easy question, but how do you move to a new page from pressing a button on a fragment?
The button doesnt seem to be doing anything once pressed, without any errors.
Here is the Java code -
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
Button btn = (Button) view.findViewById(R.id.help_button_ID);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
Intent intent = new Intent (getActivity(), help.class);
}
});
}
private HomeViewModel homeViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
You're creating an Intent but you're not starting the activity.
https://developer.android.com/training/basics/firstapp/starting-activity
This is the PagesFragment file.
public class PagesFragment extends Fragment {
public PagesFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
ImageButton s1 = (ImageButton)rootView.findViewById(R.id.speakers_1);
s1.setOnClickListener(listener);
ImageButton s2 = (ImageButton)rootView.findViewById(R.id.speakers_2);
s2.setOnClickListener(listener2);
ImageButton s3 = (ImageButton)rootView.findViewById(R.id.speakers_3);
s3.setOnClickListener(listener3);
return rootView;
}
ImageButton.OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
};
public void speak_1 (View theButton)
{
Intent i = new Intent(this.getActivity(),speaker1.class);
startActivity(i);
}
}
This is the xml file
<ImageButton
android:id="#+id/speakers_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtLabel"
android:layout_marginLeft="17dp"
android:layout_marginTop="17dp"
android:onClick="speak_1"
android:src="#drawable/speakers_1" />
How can I call speak_1? I also want to switch to another xml when the ImageButton is clicked. Thank you.
Passing different listener for each ImageButton separately is quite strange. Try to do it this way:
public class PagesFragment extends Fragment implements View.OnClickListener {
public PagesFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
ImageButton s1 = (ImageButton)rootView.findViewById(R.id.speakers_1);
s1.setOnClickListener(this);
ImageButton s2 = (ImageButton)rootView.findViewById(R.id.speakers_2);
s2.setOnClickListener(this);
ImageButton s3 = (ImageButton)rootView.findViewById(R.id.speakers_3);
s3.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// here you can handle your ImageButton clicks
int id = v.getId();
if (id == R.id.speakers_1) {
speak_1();
} else if (id == R.id.speakers_2) {
speak_2();
} // etc...
}
public void speak_1() {
Intent i = new Intent(this.getActivity(),speaker1.class);
startActivity(i);
}
public void speak_2() {
Intent i = new Intent(this.getActivity(),speaker2.class);
startActivity(i);
}
}
It's also good practice to make first letter of a class name capital, it's a standard naming convention (Speaker1 instead of speaker1).
To call a method just use speak_1(arg0) inside onClick()
Example
ImageButton.OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View arg0) {
speak_1(arg0);
}
};
Do it this way:
public class PagesFragment extends Fragment implements View.OnClickListener{
public PagesFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
ImageButton s1 = (ImageButton)rootView.findViewById(R.id.speakers_1);
s1.setOnClickListener(this);
ImageButton s2 = (ImageButton)rootView.findViewById(R.id.speakers_2);
s2.setOnClickListener(this);
ImageButton s3 = (ImageButton)rootView.findViewById(R.id.speakers_3);
s3.setOnClickListener(this);
return rootView;
}
public void speak_1(View theButton)
{
Intent i = new Intent(this.getActivity(),speaker1.class);
startActivity(i);
}
#Override
public void onClick(View view) {
if(view instance of ImageButton)
speak_1(view);
}
}
i wanna open the browser from my fragment
but when i start my app it crashes
what is wrong ?
public class C_spider extends Fragment {
Button btn_off2;
public C_spider() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_cspider, container, false);
btn_off2 = (Button)view.findViewById(R.id.btn_off2);
btn_off2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://www.google.com"));
getActivity().startActivity(i);
}
});
return view;
}
}
try putting your codes in the onActivityCreated()
public class C_spider extends Fragment {
Button btn_off2;
public C_spider() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_cspider, container, false);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btn_off2 = (Button)getActivity().findViewById(R.id.btn_off2);
btn_off2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://www.google.com"));
getActivity().startActivity(i);
}
});
}
}
My complete code is given here. But not showing the Activity.
Showing null pointer Exception when adding clickListener().
How Can i Access the button inflated using fragment
//Cant add complete code. Showing add more details.//
public class Activity extends Activity{
private static final String KEY_SUCCESS="success";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
Fragment fragment=new PlaceholderFragment();
transaction.add(R.id.container,fragment);
transaction.addToBackStack("welcome");
transaction.commit();
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
Button loginButton;
private String userNameString;
private String passwordString;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.fragment_welcome, container, false);
try{
loginButton= (Button) rootView.findViewById(R.id.LoginFormButton);
}catch (NullPointerException e){
e.printStackTrace();
}
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent logIntent = new Intent(getActivity(), BearerLoggedActivity.class);
startActivity(logIntent);
}
});
return rootView;
}
}
}
fragment.getView().findViewById(id)
That should do it, but I usually prefer to have all my listeners and business logic in the Fragment itself keeping the Activity as minimal possible. A small demo :
public class FirstFragment extends Fragment {
Button btn;
private OnFragmentClickListener listener;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_fragment,
container, false);
//Do stuff to the fragment view in here if you want
btn = (Button) v.findViewById(R.id.breplace);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent mainIntent = new Intent(getActivity(),
MyActivity.class);
startActivity(mainIntent);
}
});
return v;
}
Hope this helps !
If I understand correctly, what you are looking for is:
fragment.getView().findViewById(id);
Please add this on your Fragment class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.image_layout, container, false);
//ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
//imageView.setImageResource(imageResourceId);
//imageView.setBackgroundResource(imageResourceId);
Button button = (Button) view.findViewById(R.id.btn1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent mainIntent = new Intent(getActivity(),
NextActivity.class);
startActivity(mainIntent);
}
});
return view;
}
Your problem will be solved
public class MainFragment extends Fragment {
public static final String TAG = MainFragment.class.getSimpleName();
private static final String ABOUT_SCHEME = "settings";
private static final String ABOUT_AUTHORITY = "main";
public static final Uri ABOUT_URI = new Uri.Builder().scheme(ABOUT_SCHEME).authority(ABOUT_AUTHORITY).build();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.mainbutton, container, false);
return v;
}
}
According to below link:
How to handle button clicks using the XML onClick within Fragments
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;
}
}
}
source doesn't work too.
#J.Romero is right. Try this code, I change the onClick method and add some debug log.
public class StartFragment extends Fragment implements OnClickListener {
private static final LOG_TAG = "com.example.activity"
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_start, container, false);
if (v != null) {
Button mButton = (Button) v.findViewById(R.id.StartButton);
Log.d(TAG, "View is not null");
if (mButton != null) {
b.setOnClickListener(this);
Log.d(TAG, "mButton is not null");
}
}
return v;
}
#Override
public void onClick(View v) {
if (v == mButton) {
//do something
}
}
}
you can NOT access the UI element in onCreateView method - EVER
use onActivityCreated method , thats tell the fragment the activity is fully created and ready to interact
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Button b = (Button) v.findViewById(R.id.StartButton);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
Try this way:
Button b = (Button) v.findViewById(R.id.StartButton);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*Your code*/
}
});