I have a Tabbed activity with 3 tabs and each tab has its own fragment.
there's a button inside the first tab which i want to click and navigate to another Tabbed activity (or any activity which has a fragment). How do I accomplish this ? I am trying to click on a button and open a new activity but i am unable to do so. I would really appreciate if i get some headers as I'm learning android. Here's a link to my fragment class for reference.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tutorials,container, false);
Button button1 = (Button) view.findViewById(R.id.introbtn1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tutorial = new Intent(getActivity(), TutorialIntroduction.class);
startActivity(tutorial);
//Tried to create a toast to check if the button works but it doesn't
//Toast.makeText(getActivity(), "button is clicked!", Toast.LENGTH_LONG).show();
}
});
return view;
}
In your fragment create a ViewPager variable and a setter method for it that is called when the fragment is created in your activity(its better to do this in constructor but it says you require it empty). Then in the onClick
ViewPager.setCurrentItem(FragmentPostion)
FragmentPostion is the int of which page you want to switch to, in your case from 0-2 (or 1-3 can't remember, try both haha).
Related
I have added a button to a fragment that starts a new activity. It works, but when returning from the intent, I have no access to the ui elements any more (from the activity that holds the fragment) - they are null.
What is the proper way to handle this setup? I suspect the problem to be related to the activity life cycle.
I have the following configuration:
Activity A includes two fragments (Fragment 1 and Fragment 2)
Fragment 1 has a button to start a new Activity B. The intent is given activity (Activity A) as an argument.
When returning from Activity B, Activity A cannot access the ui elements inside Fragment 1 and Fragment 2 anymore.
This block initiates Second Activity from the A fragment. And it didn't cause an error.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_a, container, false);
Button button = (Button) root.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), SecondActivity.class);
startActivity(intent);
}
});
return root;
}
The rest of the code is default.
I have a rather simple screen that only has 4 buttons. I'm implementing it as a Fragment like so:
public class MainFragment extends Fragment implements View.OnClickListener {
// ...
#Override
public void onClick(View view) {}
}
Each button already has onClick specified to a function in the Activity that the Fragment is attached. The issue I'm having is that the onClick functions aren't called when the buttons are clicked. I've left MainFragment.onClick() empty - but is that the right approach? Does it need to be implemented for the functions to be invoked? If so, the onClick attributes in the Button layouts would seem redundant.
Any help will be appreciated.
Thanks
The right approach is to use a fragment listener to communicate back with the activity:
public static class MainActivity extends Activity
implements MainFragment.onFragmentInteraction{
...
public void onFragmentInteraction() {
// Do something
callFunction();
}
}
Then in your fragment:
mYourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (mListener != null) {
mListener.onFragmentInteraction();
}
}
});
FWIW I never use the xml onClick attributes. Although they may save a couple of lines of typing, they make it more difficult to follow what's happening in your code.
If your class implements View.OnClickListener and you have correctly overriden the onClick method (which it looks like you have), then you can safely remove any onClicks in your layout files and instead assign methods to your widget clicks in the following way:
public class MainFragment extends Fragment implements View.OnClickListener {
private Button viewOne, viewTwo, viewThree;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.your_layout, container, false);
viewOne = (Button) rootView.findViewById(R.id.view_one);
viewTwo = //etc...
//"this" refers to the current object. As the object is of a class that implements OnClickListener,
//passing "this" satisfies the View.OnClickListener parameter required for the setOnClickListener() method.
viewOne.setOnClickListener(this);
viewTwo.setOnClickListener(this);
viewThree.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View view) {
//To identify the correct widget, use the getId() method on the view argument
int id = view.getId();
switch (id) {
case R.id.view_one:
//viewOne clicked
break;
case R.id.view_two:
//And so on...
}
}
}
If you set the onClick in your XML, the click events will go to your container Activity. But you can have the click events go directly to your Fragment by setting the onClickListener to your Fragment's implementation of it. So in your Fragment's onCreateView() method, you would inflate your layout, then set the Button's onClickListener to your Fragment's implementation like this...
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_fragment, container, false);
Button button = (Button) view.findViewById(R.id.your_button);
button.setOnClickListener(this);
return view;
}
By setting the setOnClickListener() to this, you are sending all click events for that button to your Fragment instead of your Activity. Then you would just handle your onClick events as you're already doing...
#Override
public void onClick(View view) {
Log.d("YOUR BUTTON", "This is called from your Fragment instead of your Activity");
}
I am learning how to use ViewPager in my android app. Right now, I was able to create a basic app with three tabs with swipe support. Now, what I'm thinking to do is, how can I go from one page to another using a button inside one of the fragments currently in the ViewPager, I have tried this one
Android ViewPager Prev/Next Button
but it does not work for me, for a better understanding of what I am trying to achieve:
I have three(3) tabs (3 Fragments),
TAB A,
TAB B,
TAB C
in TAB A, I have a button. I want the action of that button INSIDE TAB A(Fragment A) to go to TAB C (or TAB B). I've been searching for the right approach but to no avail. I hope someone can help me, Thanks!
okay here is how to add button in fragment A to switch fragment c :
1- add button in Fragment a in xml file which id foo like this :
<Button
android:id="#+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to fragemnt c" />
go to fragment a class overide method called onCreateView and set onclickListner create by creating interface called it buttonClick and create var from it in side fragment a then override method onAttch initialize interface var inside onAttch method:
public class FragmentA extends Fragment {
buttonClick click;
Button foo;
interface buttonClick {
void buttonClicked(View v);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
click = (buttonClick) activity;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container,false);
foo = (Button) view.findViewById(R.id.foo);
foo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
click.buttonClicked(v);
}
});
return view;
}
}
then back to your MainActivity implements buttonClick interface and override methods in the method called buttonClicked(View v); setCurrentItem for view pager like this :
calss MainActivity implements FragmentA.buttonClick {
// your code here ...
public void buttonClicked(View v){
//get your viewPager var
viewPager.setCurrentItem(3);
}
}
i hope this helps
Get your ViewPager, use
ViewPager.setCurrentItem(int item)
I want to open an xml file in my project on button click this is my code could somebody please suggest me how can i do it. I'm unable to do it the one i did was using onclicklistener. Also how can i make the second fragment the default fragment so that when i slide from left to right i can view the first fragment.
public class FragmentFirstPage extends Fragment {
View root;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle){
root = inflater.inflate(R.layout.fragment_page1, container, false);
//If you want to retrieve a textview inside fragment_page1.xml for example, you shall do:
//TextView text = (TextView) root.findViewById(R.id.blablabla);
Button bt = (Button) root.findViewById(R.id.button1);
// final ViewPager viewPager = (ViewPager) getActivity().findViewById (R.id.activity_main_viewpager);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v){
Intent i=new Intent(fragment_page1.this, SplashActivity.class);
startActivity(i);
}
});
return root;
}
Gives me error at
Intent i=new Intent(fragment_page1.this, SplashActivity.class);
says fragment_page1 cannot be resolved to a type or else
Intent i=new Intent(FragmentFirstPage.this, SplashActivity.class);
says Multiple markers at this line
- The constructor Intent(FragmentFirstPage, Class) is
undefined
FragmentB fragmentB = new FragmentB();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragment, customerFragment).commit();
write this in your onClick method for navigating from FragmentA to navigate to FragmentB.
You have FragmentFirstPage and FragmentSecondPage, you may have fragment_page1.xml and fragment_page2.xml
PS: remove any android:onClick attribute
Inside your FirstFragmentPage, on onCreateView (), , add the following if your button is on fragment_page1.xml
Button bt = (Button) root.findViewById(R.id.your_button_id);
ViewPager viewPager = (ViewPager) getActivity().findViewById (R.id.your_viewpager_id ();
bt.setOnClickListener(new View.onClickListener (){
#Override
public void onClick (View v){
viewPager.setCurrentItem (viewPager.getCurrentItem() + 1); //or -1 to go previous
}
}
Forgive my code format, I'm typing from my smartphone : /
I am porting Activity-type app to Fragments-type app. I am a bit confused where I am supposed to initiate UI elements of the fragment.
For example, if I initiate a button from Fragment class in FragmentActivity class, I get no error.
btnOne = (Button) findViewById(R.id.btn_one);
But, if I try to initiate onClick listener, then I get error of type java.lang.NullPointerException.
Then again, I cannot find method findViewById in the Fragment class.
Am I really forced to initiate in FragmentActivity and to specify listeners in Fragment?
You initiate your button in the fragment usually. An example would be:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.yourlayout, container, false);
Button yourbutton = (Button) fragmentView.findViewById(R.id.button);
yourbutton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//Do your thing
}
}
return fragmentView;
}