1

I am trying to create a calculator with two fragments for simple and scientific calculators. But the Viewpager is empty though scrollable between two fragments no content of the fragments is visible. Also, if I change IsViewFromObject to true,Both the fragments are visible on top of each other.

Here is the viewpager:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.root.calculatorui.Science">
    <EditText
        android:inputType="none"
        android:textIsSelectable="true"
        android:gravity="right|bottom"
        android:textSize="50sp"
        android:background="#FFFFFF"
        android:id="@+id/txtResult"
        android:layout_width="match_parent"
        android:layout_height="180dp" />
<android.support.v4.view.ViewPager
    android:layout_below="@id/txtResult"
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
    </RelativeLayout>

And here's the fragmentActivity code used to initialize the ViewPager:

package com.example.root.calculatorui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

public class Science extends FragmentActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_for_calculator);
        ViewPager viewPager=(ViewPager)findViewById(R.id.viewPager);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        public MyPagerAdapter(FragmentManager supportFragmentManager) {
            super(supportFragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    Log.d("Load", "Started Science");
                    return new ScienceFragment();
                case 1:
                    Log.d("Load", "Back to Simple");
                    return new SimpleFragment();
                default:
                    return new SimpleFragment();
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return false;
        }
    }
}

Here is the simpleFragment:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.root.calculatorui.Science">

    <Button
        android:text="C"
        android:textColor="@android:color/holo_orange_dark"
        android:layout_below="@+id/txtResult"
        style="@style/Button"
        android:id="@+id/buttonClear" />

    <Button
        android:text="←"
        style="@style/Button"
        android:id="@+id/buttonDel"
        android:layout_below="@+id/txtResult"
        android:layout_toRightOf="@+id/buttonClear"
        android:layout_toEndOf="@+id/buttonClear" />

    <Button
        android:text="÷"
        style="@style/Button"
        android:id="@+id/buttonDiv"
        android:layout_below="@+id/txtResult"
        android:layout_toRightOf="@+id/buttonDel"
        android:layout_toEndOf="@+id/buttonDel" />

    <Button
        android:text="x"
        style="@style/Button"
        android:id="@+id/buttonMul"
        android:layout_toRightOf="@+id/buttonDiv"
        android:layout_below="@+id/txtResult" />

    <Button
        android:text="7"
        style="@style/Button"
        android:id="@+id/button7"
        android:layout_below="@+id/buttonClear"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="8"
        style="@style/Button"
        android:id="@+id/button8"
        android:layout_below="@+id/buttonDel"
        android:layout_toRightOf="@+id/button7"
        android:layout_toEndOf="@+id/button7" />

    <Button
        android:text="9"
        style="@style/Button"
        android:layout_below="@+id/buttonDiv"
        android:layout_toRightOf="@+id/button8"
        android:layout_toEndOf="@+id/button8"
        android:id="@+id/button9" />

    <Button
        android:text="-"
        android:textSize="30sp"
        style="@style/Button"
        android:layout_toRightOf="@+id/button9"
        android:layout_toEndOf="@+id/button9"
        android:id="@+id/buttonSub"
        android:layout_below="@+id/buttonDiv" />

    <Button
        android:text="5"
        android:id="@+id/btn5"
        style="@style/Button"
        android:layout_alignBottom="@+id/btn4"
        android:layout_toRightOf="@+id/button7"
        android:layout_toEndOf="@+id/button7"
        android:layout_below="@+id/button7" />

    <Button
        android:text="4"
        android:id="@+id/btn4"
        style="@style/Button"
        android:layout_below="@+id/button7"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/btn6"
        android:text="6"
        style="@style/Button"
        android:layout_below="@+id/button8"
        android:layout_toRightOf="@+id/btn5"
        android:layout_toEndOf="@+id/btn5" />

    <Button
        android:text="+"
        android:id="@+id/btnPlus"
        style="@style/Button"
        android:layout_below="@+id/buttonSub"
        android:layout_toRightOf="@+id/btn6"
        android:layout_toEndOf="@+id/btn6" />

    <Button
        android:text="1"
        android:id="@+id/btn1"
        style="@style/Button"
        android:layout_below="@+id/btn5"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="2"
        android:id="@+id/btn2"
        style="@style/Button"
        android:layout_below="@+id/btn5"
        android:layout_toRightOf="@+id/btn4"
        android:layout_toEndOf="@+id/btn4" />

    <Button
        android:text="3"
        android:id="@+id/btn3"
        style="@style/Button"
        android:layout_below="@+id/btn6"
        android:layout_toRightOf="@+id/btn2"
        android:layout_toEndOf="@+id/btn2" />

    <Button
        android:text="%"
        android:id="@+id/btnPercentage"
        style="@style/Button"
        android:layout_below="@+id/btn1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="0"
        android:id="@+id/btn0"
        style="@style/Button"
        android:layout_below="@+id/btn2"
        android:layout_toRightOf="@+id/btnPercentage"
        android:layout_toEndOf="@+id/btnPercentage" />

    <Button
        android:text="."
        android:textSize="40sp"
        android:id="@+id/btnDot"
        style="@style/Button"
        android:layout_below="@+id/btn2"
        android:layout_toRightOf="@+id/btn0"
        android:layout_toEndOf="@+id/btn0" />

    <Button
        android:text="="
        style="@style/Button"
        android:background="@android:color/holo_orange_light"
        android:id="@+id/buttonEquals"
        android:layout_toRightOf="@+id/btn3"
        android:layout_alignBottom="@+id/btnDot"
        android:layout_below="@+id/btn6" />
</RelativeLayout>

Here is SimpleFragment.Java

package com.example.root.calculatorui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by root on 14/1/17.
 */

public class SimpleFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_main,container,false);
        return view;
    }

    public SimpleFragment(){}

    public Fragment newInstance(int s, String title) {
        SimpleFragment sfrag=new SimpleFragment();
        Bundle args = new Bundle();
        args.putInt("someInt", s);
        args.putString("someTitle",title);
        sfrag.setArguments(args);
        return sfrag;
    }
}
Bismeet Singh
  • 65
  • 3
  • 10

5 Answers5

0

You're not making an instance of the fragments in the getItem(int position) method.

Change it as per the following and it should work:-

 @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        Log.d("Load", "Started Science");
                        return new ScienceFragment.newInstance("FirstFragment, Instance 1");

                    case 1:
                        Log.d("Load", "Back to Simple");
                        return new SimpleFragment.newInstance("SecondFragment, Instance 2");

                    default:
                        return new SimpleFragment.newInstance("DdefaultFragment, Instance default");
            }
        }
Abhriya Roy
  • 1,218
  • 16
  • 22
0

I am trying to create a calculator with two fragments for simple and scientific calculators

Fragments cannot hold other fragments: https://stackoverflow.com/a/6672688/4409113

If you are trying to create two Fragments, Create a single class called MyPagerAdapter, Then find the MainActivity (it can be done by searching in Manifest) initialize the ViewPager in the Activity(in the MainActivity for example), And not in the Fragment, Then paste the ViewPager xml codes inside the Activity layout and not the Fragment.

Note that you can create that MyPagerAdapter inside the Activity either but you'll need to change the codes a bit...

(Extending to AppCompatActivity and etc).

Then you'll be good to go!

Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,177
  • 7
  • 51
  • 106
0

Use FragmentPagerAdapter

class ViewPagerAdapter extends FragmentPagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

@Override
public Fragment getItem(int position) {
        return mFragmentList.get(position);
}

@Override
public int getCount() {
        return mFragmentList.size();
    }
public void addFragment(Fragment fragment, String title) {
    mFragmentList.add(fragment);
    mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
}}

And setup viewpager like this

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new ScienceFragment(), "Science");
    adapter.addFragment(new SimpleFragment(), "Simple");
    viewPager.setAdapter(adapter);
  • The science fragment is visible but the simple one isnt – Bismeet Singh Jan 15 '17 at 05:52
  • Try to replace getSupportFragmentManager() with getChildFragmentManager() – Rana Shahzaib Jan 15 '17 at 05:54
  • Inside ScienceFragment `public static ScienceFragment newInstance(int page, String title) { ScienceFragment fragmentFirst = new ScienceFragment(); Bundle args = new Bundle(); args.putInt("someInt", page); args.putString("someTitle", title); fragmentFirst.setArguments(args); return fragmentFirst; }` In MainActivity. ` case 0: return ScienceFragment.newInstance(0, "Instance 1"); case 1: return SimpleFragment.newInstance(1, "Instance 2"); default: return ScienceFragment.newInstance(0, "Default"); }` – Aman Shekhar Jan 15 '17 at 05:55
  • getChildFragmentManager: No such symbol – Bismeet Singh Jan 15 '17 at 06:01
  • Also add java of your fragment – Rana Shahzaib Jan 15 '17 at 06:05
  • You have assigned an id in you fragment's relative layout as activity_main .. And trying to use that to inflate the layout for your fragment.. There is a difference between layout and id.. When you're using R.layout then you will have to use exact name of your xml layout like your xml name is fragment_simple.xml then you will use R.layout.fragment_simple – Rana Shahzaib Jan 15 '17 at 06:15
  • My layout name is activity_main too – Bismeet Singh Jan 15 '17 at 06:19
0

Use this in ScienceFragment,

public static ScienceFragment newInstance(int page, String title) {
    ScienceFragment fragmentFirst = new ScienceFragment();
    Bundle args = new Bundle();
    args.putInt("someInt", page);
    args.putString("someTitle", title);
    fragmentFirst.setArguments(args);
    return fragmentFirst;
}

Similarly in SimpleFragment, and use this in MainActivity.

private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int pos) {
            switch(pos) {

                case 0: return ScienceFragment.newInstance(0, "Instance 1");
                case 1: return SimpleFragment.newInstance(1, "Instance 2");
                default: return ScienceFragment.newInstance(0, "Default");
            }
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
Aman Shekhar
  • 2,629
  • 1
  • 17
  • 28
-1

This is MainActivity Class.

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager viewPager=(ViewPager)findViewById(R.id.vpPager);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

    }
    private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int pos) {
            switch(pos) {

                case 0: return ScienceFragment.newInstance(0, "Science");
                case 1: return SimpleFragment.newInstance(1, "Simple");
                default: return ScienceFragment.newInstance(0, "Default");
            }
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
}

This is ScienceFragment
/**
 * Created by Techno Blogger on 15/1/17.
 */

public class ScienceFragment extends Fragment {
    private String title;
    private int page;

    // newInstance constructor for creating fragment with arguments
    public static ScienceFragment newInstance(int page, String title) {
        ScienceFragment fragmentFirst = new ScienceFragment();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", title);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }

    // Inflate the view for the fragment based on layout XML
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_science, container, false);
        TextView tvLabel = (TextView) view.findViewById(R.id.textScience);
        tvLabel.setText(page + " -- " + title);
        return view;
    }
}

This is SimpleFragment

public class SimpleFragment extends Fragment {

    // newInstance constructor for creating fragment with arguments
    public static SimpleFragment newInstance(int page, String title) {
        SimpleFragment fragmentFirst = new SimpleFragment();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", title);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_simple, null);
        return rootView;
    }
}
Aman Shekhar
  • 2,629
  • 1
  • 17
  • 28