What happens is that when Activity runs it shows the camera fragment but when I swipe the view pager for other fragment, it shows the screen fully black. I am not able to access the content of other fragments.
This is the layout of my view Pager.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="lifeline.learn.com.suggestions.WallOfTheApp">
<android.support.design.widget.TabLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/tabLayout"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:animateLayoutChanges="true"
android:background="@drawable/backgroundcorner"
android:layout_height="match_parent"
android:layout_below="@+id/tabLayout"/> </RelativeLayout>
Here is the class of that activity
public class WallOfTheApp extends AppCompatActivity {
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_wall_of_the_app);
requestPermissions (new String[]{Manifest.permission.CAMERA}, 10);
TabLayout tabLayout = findViewById (R.id.tabLayout);
viewPager = findViewById (R.id.viewPager);
//Button cameraBtn = findViewById (R.id.camer_button);
tabLayout.addTab (tabLayout.newTab ().setText ("MainFragment"));
tabLayout.addTab (tabLayout.newTab ().setText ("PrfileInfo"));
tabLayout.addTab (tabLayout.newTab ().setText ("Camera"));
PagerAdapter adapter = new PageAdapter (getSupportFragmentManager (), tabLayout.getTabCount ());
viewPager.setAdapter (adapter);
viewPager.setOffscreenPageLimit (tabLayout.getTabCount ());
viewPager.setCurrentItem (1);
//viewPager.setPageTransformer (false, new PageTransformerAnimation ());
viewPager.setOnPageChangeListener (new TabLayout.TabLayoutOnPageChangeListener (tabLayout));
tabLayout.setOnTabSelectedListener (new TabLayout.OnTabSelectedListener () {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem (tab.getPosition ());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public void onBackPressed() {
if(viewPager.getCurrentItem () != 1){
viewPager.setCurrentItem (1);
}else{
super.onBackPressed ();
}
}
}
Here is the Camera.java fragment
public class Camera extends android.support.v4.app.Fragment {
Context context;
private android.hardware.Camera mCamera;
private SurfaceHolders surfaceHolders;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate (R.layout.fragment_camera, container, false);
context = getActivity ();
if(checkCameraHardware (context)){
try {
surfaceHolders = new SurfaceHolders (context,mCamera);
FrameLayout preview = view.findViewById(R.id.camera_layout);
preview.addView(surfaceHolders);
}catch (Exception e){
Toast.makeText (context, e.getMessage (), Toast.LENGTH_SHORT).show ();
}
}
return view;
}
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
return true;
} else {
return false;
}
}
public static android.hardware.Camera getCameraInstance(){
android.hardware.Camera c = null;
try {
c = android.hardware.Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
}
return c;
}
@Override
public void onDestroyView() {
super.onDestroyView();
if(mCamera != null)
mCamera.release();
}
}
Here is the surface holder class
public class SurfaceHolders extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private android.hardware.Camera mCamera;
public SurfaceHolders(Context context, android.hardware.Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
if(mCamera != null) {
mCamera = Camera.open();
mCamera.setPreviewDisplay (holder);
mCamera.startPreview();
mCamera.setDisplayOrientation (90);
}
} catch (IOException e) {
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
if(mCamera != null){
mCamera.stopPreview ();
mCamera.release ();
mCamera = null;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null){
return;
}
try {
mCamera.stopPreview();
} catch (Exception e){
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.e ("Error",e.getMessage ());
}
}
}
Here is the camera fragment layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="10dp"
tools:context="lifeline.learn.com.suggestions.Camera">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/camera_layout">
</FrameLayout>
</FrameLayout>
What I found is that the fragment is there on swipe but it is under a screen of black color.