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