I'm working with a ViewPager
and I have two fragments. The first fragment is showing a CameraPreview. I want to include another layout above this Fragment but I only can add this layout to the activity (It shows the layout above two fragments and not only in the first fragment).
This is what I have. But the Button and the TextView are also showing in the another fragment of the parent Activity.
I'm using this code to add the overlay layout (R.layout.control)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
/* Instance barcode scanner */
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
mPreview = new CameraPreview(getActivity(), mCamera, previewCb, autoFocusCB);
dialogProduct = new Dialog(getActivity(), R.style.DialogSlideAnim);
dialogProduct.setContentView(R.layout.dialog_register);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_scan, container, false);
FrameLayout preview = (FrameLayout) rootView.findViewById(R.id.cameraPreviewFrame);
preview.addView(mPreview);
scanText = (TextView) rootView.findViewById(R.id.scanText);
scanButton = (Button) rootView.findViewById(R.id.ScanButton);
scanButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (barcodeScanned) {
barcodeScanned = false;
scanText.setText("Scanning...");
mCamera.setPreviewCallback(previewCb);
mCamera.startPreview();
previewing = true;
mCamera.autoFocus(autoFocusCB);
}
}
});
//Add overlay layout
View headerView = View.inflate(getActivity(), R.layout.control, null);
LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
getActivity().addContentView(headerView,layoutParamsControl);
return rootView;
}
This is the fragment's layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/totalText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/total_text"
android:textSize="30sp" >
</TextView>
<FrameLayout
android:id="@+id/cameraPreviewFrame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<TextView
android:id="@+id/scanText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/escaneando" >
</TextView>
<Button
android:id="@+id/ScanButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/btn_scan" />
</LinearLayout>
Questions
I have tried using the method
addView()
after add themPreview
(CameraPreview)
but I got an error because I don't remove the previous view. ¿Is there a method likeaddContentView
(which don't remove the previous view) in the Fragment Class?¿Do you have any other suggestion to solve this problem?
I'd really appreciate your help
Thank you