[ViewPager - How to Navigate from one page to another using a button? - android

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)

Related

How to make changes in widget in parent activity based on the logic written in fragment

As an exercise i am writing a simple Ebook reader app. This has only 1 Main Activity with 3 widgets on it, 2 buttons (Previous, Next) and one Fragment container(I have used Frame Layout). All pages are different fragments that I have created that will go inside the container, and these fragments have only 1 scrollable text view that will only display text. when "next" button is pressed it should go to page2(fragment2) and when previous is pressed it should go back(previous fragment).
My problem is I don't want the "previous" button to show up on the initial screen (page1) and similarly the "next" button should not be observed on the last page.
The approach i tried was, in my fragment1(page1) class, i wrote an if condition like,
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragmentpage1,container,false);
textView1=(TextView)view.findViewById(R.id.tview);
**View listView = getActivity().findViewById(R.id.previous);**
textView1.setText(R.string.page1);
if (textView1.isEnabled()){
listView.setVisibility(View.INVISIBLE);
}
return view;
}
I am checking if textview1(the first fragment) is present or not , if yes then hide the previous button on the Main Activity. This works but it completely hides the previous button even when i go to page2. I tried all possible "is" options but none of them are giving me the results I want.
One workaround that i found was to add "setvisibility" of previous button to all fragments, so on fragment1 it is invisible and then on fragment2 i changed that to visible. But that becomes lengthy if there are 100s of fragments(pages).
Please provide me with a simple solution, I am new to Android.
Below is my Main Activity code:(Do let me know if any changes needs to be done to make code more clean)
public class MainActivity extends FragmentActivity {
Fragment fragment;
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft;
private static Button next;
private static Button previous;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next=(Button)findViewById(R.id.next);
previous=(Button)findViewById(R.id.previous);
create();
}
public void create(){
fragment= new FragmentPage1();
ft= fm.beginTransaction();
ft.add(container,fragment);
ft.commit();
}
public void next(View view){
fragment= new FragmentPage2();
ft= fm.beginTransaction();
ft.replace(container,fragment);
ft.commit();
}
}
Maybe you can try to use the getItem() method in FragmentPagerAdapter class.
Try to disable or enable your buttons using the switch case. Hope this can help =)

Tabbed activity(fragment) to activity navigation using onClick

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).

When click button inside fragment I want to go to another fragment in the parent TabLayout

I have a MainActivity, Contains TabLayout having two fragments FA, FB
The First Fragment FA having another Tablayout contains 2 fragments FAA, FAB
I have a button inside FAA, i want when i click on it, it send me to FB which already inside the parent TabLayout
I tried many solutions but it didn't help, any suggestions?
#OnClick(R.id.frag_timeline_fab_faa)
public void goToFBFromFAA(View view) {
// Code shoud be here
}
The way to do this is through interfaces.Here is a similar question I found
In fragment FAA:
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Param param);
}
Then in your parent fragment you need to implement that interface:
public class FA extends Fragment implements
FAA.OnFragmentInteractionListener {
#Override
public void onFragmentInteraction(Param param) {
//Your code here
}
}

Fragment with buttons: onClick() vs. XML onClick

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");
}

Fragment: reloading on back press

I am using Fragments and Activity extended by AppCompatActivity in an application.
Working:
I have two fragments "Dashboard" and "Order". I am replacing fragment after clicking "Order Button" from Dashboard Fragment, and coming back to "Dashboard Fragment" after pressing back button.
Problem
I have called an API on onCreateView() of Dashboard Fragment. When I press back from Order Fragment then I come to "Dashboard Fragment" the it recall the API. I don't want to recall the API if I come to the fragment by back press.
Thanks in advance.
Code to replace dashboard fragment with order fragment
// Click event of Order
#OnClick(R.id.ll_order)
void openOrder() {
if (isOrderNotClicked) {
OrderFragment fragment = new OrderFragment();
this.getFragmentManager().beginTransaction().replace(R.id.flContent, fragment, "Order").addToBackStack(null).commit();
isOrderNotClicked = !isOrderNotClicked;
}
}
I did had same problem so i solved by `View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view == null) {
//inflate layout and codes
}
return view;
}` but i dont know it is the best solution

Categories

Resources