I’m trying to creating a layout that contains a "SurfaceView" at the top with a horizontal toolbar that is section into 3 segments in the bottom, however my SurfaceView which contains camera preview is only the only element that is showing. Please point out the error in my layout xml or provide me the similar layout xml, Thanks.
Below is my layout & updates based on user recommendations
Attempt # 1 (RelativeLayout as root):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LayoutForPreview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:screenOrientation="portrait" >
<SurfaceView
android:id="#+id/surfaceViewBarcodeScanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayoutToolbar"
android:layout_gravity="top"
android:scaleType="fitXY" />
<LinearLayout
android:id="#+id/linearLayoutToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_left" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.68"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_center_toolbar" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_right_landscape_button" />
</LinearLayout>
</RelativeLayout>
Below is my desired GUI look:
Attempt #1 Result , device snapshot , which only shows the SurfaceView:
Snapshot of eclipse GUI tool, which shows what the layout should look like.
Attempt # 2 (LinearLayout as root):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LayoutForPreview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:screenOrientation="portrait" >
<SurfaceView
android:id="#+id/surfaceViewBarcodeScanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:scaleType="fitXY"
android:layout_weight="1" />
<LinearLayout
android:id="#+id/linearLayoutToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_left" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.68"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_center_toolbar" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_right_landscape_button" />
</LinearLayout>
</LinearLayout>
Attempt #2 , Result , Device snapshot :
Issues:
1) Toolbar appears on top rather than on bottom
2) Camera preview freezes & app crashes. For some reason LinearLayout causes the Camera error 1001 , and I think its related to preview sizes.
Attempt #3 (Add toolbar programmatically to either Relative or Linear layout in activity onCreate).
LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LayoutForPreview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:screenOrientation="portrait" >
<SurfaceView
android:id="#+id/surfaceViewBarcodeScanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:scaleType="fitXY"
android:layout_weight="1" />
</LinearLayout>
Toolbar defined in separate XML added later via Java code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- LinearLayout that contains toolbar that is divided into 3 sections horizontally , layout_below="#+id/BarcodeScannerFrame-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayoutToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_left" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.68"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_center_toolbar" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_right_landscape_button" />
</LinearLayout>
onCreate,onResume,other relevant code snippets of my activity only, the LinearLayout toolbar is added to Linear or Relative layout at runtime:
//import not included
public class ScanVinFromBarcodeActivity extends Activity {
// camera object that is used globally in this activity and also passed
// reference to PreviewSurface inner class
private Camera globalCamera;
private int cameraId = 0;
// bitmap that would created after picture is taken and converted from
// camera bytes
private Bitmap bmpOfTheImageFromCamera = null;
// global flag whether a camera has been detected
private boolean isThereACamera = false;
// layout for this activity
private LinearLayout RelativeLayoutBarcodeScanner= null;
// CameraPreview extends SurfaceView displays preview of images from the
// Camera
private CameraPreview newCameraPreview = null;
// used to inflate the xml layout
private SurfaceView surfaceViewBarcodeScanner = null;
private boolean cameraPreviewing = false;
// this continueToAutomaticallyDecode flag is initially set to TRUE, but
// will be set to FALSE on the first successful decode OR when a crucial
// method in the code process fails or throws an exception
private volatile boolean continueToAutomaticallyDecode = true;
// global flag used to indicate picture taking & decoding is in progress
private volatile boolean takingPictureAndDecodeInprogress = false;
// Bitmap options for bitmap creation from camera picture
BitmapFactory.Options options = null;
// used for samsung galaxy s devices only
private Matrix rotationMatrix90CounterClockWise = null;
// Reader is class from zxing used to decode barcodes
Reader reader = null;
// DecodeHintType hashtable is used to provide help to the zxing Reader
// class
Hashtable<DecodeHintType, Object> decodeHints = null;
//
private boolean onTouchEvent = true;
//
private OrientationEventListener orientationEventListener = null;
// 1 means the screen is PORTRAIT and 2 means screen is LANDSCAPE
private int latestScreenOrientation = 1;
//
Camera.Parameters Flash = null;
//
private String globalVIN = null;
//
private Handler handler = null;
//
private LinearLayout barcodeVinScannerToolbar = null;
public boolean isContinueToAutomaticallyDecode() {
return continueToAutomaticallyDecode;
}
public void setContinueToAutomaticallyDecode(
boolean continueToAutomaticallyDecode) {
this.continueToAutomaticallyDecode = continueToAutomaticallyDecode;
}
public boolean isTakingPictureAndDecodeInprogress() {
return takingPictureAndDecodeInprogress;
}
public void setTakingPictureAndDecodeInprogress(
boolean takingPictureAndDecodeInprogress) {
this.takingPictureAndDecodeInprogress = takingPictureAndDecodeInprogress;
}
/*
* This method , finds FEATURE_CAMERA, opens the camera, set parameters ,
* add CameraPreview to layout, set camera surface holder, start preview
*/
#SuppressLint("InlinedApi")
private void initializeGlobalCamera() {
try {
if (!getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
Toast.makeText(this, "No camera on this device",
Toast.LENGTH_LONG).show();
} else { // check for front camera ,and get the ID
cameraId = findFrontFacingCamera();
if (cameraId < 0) {
Toast.makeText(this, "No front facing camera found.",
Toast.LENGTH_LONG).show();
} else {
Log.d("ClassScanViewBarcodeActivity",
"camera was found , ID: " + cameraId);
// camera was found , set global camera flag to true
isThereACamera = true;
// OPEN
globalCamera = getGlobalCamera(cameraId);
// parameters auto focus
globalCamera.getParameters().setFocusMode(
Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
// set picture format to JPEG, everytime makesure JPEg
globalCamera.getParameters().setPictureFormat(
ImageFormat.JPEG);
autoFocusSetupForBarcode(globalCamera);
/*
* START early setup variables & setting used in
* jpegCallback in order to optimize the jpegCallback code
*/
options = new BitmapFactory.Options();
// option set for down sampling he captured image taken from
// the camera in order to MemoryOutOfBounds exception
options.inSampleSize = 4;
// image quality rather than speed in order to achieve early
// barcode detection & decode
options.inPreferQualityOverSpeed = false;
// Samsung galaxy S only , rotate to correct orientation
// ,and capture only the image within the guidance rectangle
rotationMatrix90CounterClockWise = new Matrix();
rotationMatrix90CounterClockWise.postRotate(90);
// early variable used by zxing to decode method
decodeHints = new Hashtable<DecodeHintType, Object>();
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
decodeHints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
decodeHints.put(DecodeHintType.ASSUME_CODE_39_CHECK_DIGIT,
Boolean.TRUE);
reader = new MultiFormatReader();
turnOnFlashlight(globalCamera);
// pass surfaceView to CameraPreview
newCameraPreview = new CameraPreview(this, globalCamera) {
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("ClassScanViewBarcodeActivity",
" onTouchEvent(MotionEvent event) ");
onTouchEvent = true;
globalCamera
.autoFocus(autoFocusCallbackForAutomaticScan);
return super.onTouchEvent(event);
}
};
// pass CameraPreview to Layout
RelativeLayoutBarcodeScanner.addView(newCameraPreview);
// give reference SurfaceView to camera object
globalCamera.setPreviewDisplay(surfaceViewBarcodeScanner
.getHolder());
// PREVIEW
if (cameraPreviewing != true) {
globalCamera.startPreview();
}
Log.d("ClassScanViewBarcodeActivity",
"camera opened & previewing");
}
}// end else ,check for front camera
}// end try
catch (Exception exc) {
// in case of exception release resources & cleanup
if (globalCamera != null) {
globalCamera.stopPreview();
cameraPreviewing = false;
globalCamera.setPreviewCallback(null);
globalCamera.release();
globalCamera = null;
options = null;
rotationMatrix90CounterClockWise = null;
reader = null;
}
Log.d("ClassScanViewBarcodeActivity initializeGlobalCamera() exception:",
exc.getMessage());
exc.printStackTrace();
}// end catch
}// end ini
/* this method detect whether the camera flashlight a.k.a torch feature is available to be turned on then turns on the light*/
public void turnOnFlashlight(Camera camera) {
Log.d("ClassScanViewBarcodeActivity",
"turnOnFlashlight(Camera camera )");
boolean flag = false;
Context context = null;
if (camera != null) {
Log.d("ClassScanViewBarcodeActivity",
"turnOnFlashlight() , FEATURE_CAMERA_FLASH: " + flag);
context = RelativeLayoutBarcodeScanner.getContext();
if (context != null) {
flag = context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH);
if (flag) {
Flash = camera.getParameters();
Flash.setFlashMode("torch");
camera.setParameters(Flash);
}// end if camera feature is available
else {
Log.d("ClassScanViewBarcodeActivity",
"turnOnFlashlight() , FEATURE_CAMERA_FLASH: "
+ flag);
}
}// end if context not null
}// end camera not null
}// end turnOnFlashlight(Camera camera )
// onCreate, instantiates layouts & surfaceView used for video preview
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode_vin_scanner);
Log.d("ClassScanViewBarcodeActivity", "onCreate ");
// create surfaceView for previewing of camera image
RelativeLayoutBarcodeScanner = (LinearLayout) findViewById(R.id.LayoutForPreview);
surfaceViewBarcodeScanner = (SurfaceView) findViewById(R.id.surfaceViewBarcodeScanner);
barcodeVinScannerToolbar = (LinearLayout) findViewById(R.id.linearLayoutToolbar);
initializeGlobalCamera();
//*****TOOLBAR IS ADDED*****
if (RelativeLayoutBarcodeScanner!=null && barcodeVinScannerToolbar!=null)
{
//surfaceViewBarcodeScanner.addChildrenForAccessibility(list);
RelativeLayoutBarcodeScanner.addView(barcodeVinScannerToolbar);
}
// instantiate orientationEventListener
orientationEventListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_NORMAL) {
#Override
public void onOrientationChanged(int arg0) {
/*
latestScreenOrientation = ScreenUtility
.getScreenOrientation(RelativeLayoutBarcodeScanner.getContext());
Log.d("ClassScanViewBarcodeActivity",
"latestScreenOrientation: " + latestScreenOrientation);
if (orientationEventListener.canDetectOrientation()) {
orientationEventListener.enable();
Log.d("ClassScanViewBarcodeActivity",
"enabled orientationEventListener: "
+ String.valueOf(orientationEventListener
.canDetectOrientation()));
} else {
Log.d("ClassScanViewBarcodeActivity",
"enabled orientationEventListener: "
+ String.valueOf(orientationEventListener
.canDetectOrientation()));
}
*/
}
};
handler = new Handler();
}// end onCreate
#Override
protected void onResume() {
Log.d("ClassScanViewBarcodeActivity, onResume() globalCamera:",
String.valueOf(globalCamera));
initializeGlobalCamera();
//*****TOOLBAR IS ADDED*****
if (RelativeLayoutBarcodeScanner!=null && barcodeVinScannerToolbar!=null)
{
//surfaceViewBarcodeScanner.addChildrenForAccessibility(list);
RelativeLayoutBarcodeScanner.addView(barcodeVinScannerToolbar);
}
if (orientationEventListener != null) {
orientationEventListener.enable();
}
super.onResume();
}
#Override
protected void onStop() {
if (globalCamera != null) {
globalCamera.stopPreview();
cameraPreviewing = false;
globalCamera.setPreviewCallback(null);
globalCamera.release();
globalCamera = null;
}
if (orientationEventListener != null) {
orientationEventListener.disable();
}
super.onStop();
}
#Override
protected void onPause() {
if (globalCamera != null) {
globalCamera.stopPreview();
cameraPreviewing = false;
globalCamera.setPreviewCallback(null);
globalCamera.release();
globalCamera = null;
options = null;
rotationMatrix90CounterClockWise = null;
reader = null;
}
if (orientationEventListener != null) {
orientationEventListener.disable();
}
super.onPause();
}// end onPause()
//other irrelevant code was not included
}//end activity
Attempt #3, result, screen freezes , no particular error or exception reported:
Error log:
**If the eclipse GUI tool shows the desired look , than why is the toolbar still missing??? I would like to have the toolbar in the bottom , preferably using RelativeLayout without any crashes or screen freezes. Any help will be appreciated.
I'm using Samsung Galaxy S3 for testing.
Thanks.**
You wanted something like this?
the layout I used is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="#+id/linearLayoutToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:src="#drawable/ic_launcher"
/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:src="#drawable/ic_launcher"
/>
<ImageView
android:id="#+id/imageView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:src="#drawable/ic_launcher"
/>
</LinearLayout>
<SurfaceView
android:id="#+id/surfaceViewBarcodeScanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/linearLayoutToolbar"
/>
</RelativeLayout>
Please note that, since I didn't have your images in my res/drawable folder, I replaced them with ic_launcher.
Also note that I used an ldpi device running Froyo (API level 2.2). This is why the image is smaller than yours and the UI differs slighthly (...). Anyway, it works at every resolution and with every os version.
Add this to your LinearLayout containing imageView
android:layout_alignParentBottom="true"
What you really want is not to use a Relativelayout but instead use a Linearlayout as your root. Then set the surfaceview weight to 1, something like:
<SurfaceView
android:id="#+id/surfaceViewBarcodeScanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/scanner_bottom_left" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/scanner_bottom_center_toolbar" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/scanner_bottom_right_landscape_button" />
</LinearLayout>
You forgot to tell the surfaceView to be above the linearLayout. Your code will need to look similar to this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/linearLayoutToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/scanner_bottom_left" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/scanner_bottom_center_toolbar" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/scanner_bottom_right_landscape_button" />
</LinearLayout>
<SurfaceView
android:id="#+id/surfaceViewBarcodeScanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/linearLayoutToolbar" />
</RelativeLayout>
Why do you use RelativeLayout as a root layout, I think it is much simplier to use LinearLayou with layout_weight parameter in one of children view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:layout_weight="1"
android:id="#+id/surfaceViewBarcodeScanner"
android:layout_width="match_parent"
android:layout_height="0dp" />
<!-- LinearLayout that contains toolbar that is divided into 3 sections horizontally -->
<LinearLayout
android:id="#+id/linearLayoutToolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/scanner_bottom_left" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/scanner_bottom_center_toolbar" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/scanner_bottom_right_landscape_button" />
</LinearLayout>
</LinearLayout>
try changing the layout to this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LayoutForPreview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:screenOrientation="portrait" >
<SurfaceView
android:id="#+id/surfaceViewBarcodeScanner"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="top"
android:scaleType="fitXY"
android:layout_weight="1" />
<LinearLayout
android:id="#+id/linearLayoutToolbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:orientation="horizontal"
android:layout_gravity="bottom" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_left" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.68"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_center_toolbar" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_right_landscape_button" />
</LinearLayout>
I see a few mistakes in your initial XML
1. In the surfaceview you use:
android:layout_above="#+id/linearLayoutToolbar"
You should use #+id when you create an Id but when you want to make a reference you should only do #id like this:
android:layout_above="#id/linearLayoutToolbar"
2. You can't do a reference to another object that isn't created yet! So you should put the surfaceview below de linearlayout like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LayoutForPreview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:screenOrientation="portrait" >
<LinearLayout
android:id="#+id/linearLayoutToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_left" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.68"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_center_toolbar" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_right_landscape_button" />
</LinearLayout>
<SurfaceView
android:id="#+id/surfaceViewBarcodeScanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/linearLayoutToolbar"
android:layout_gravity="top"
android:scaleType="fitXY" />
</RelativeLayout>
3. You should add weight to your 3 ImageViews like this:
android:layout_weight="1"
Some small changes in your initial layout will lead to remove the error. You need to use android:layout_height="0dp" , defining 0dp will help your surfaceview to fit in your screen.
<SurfaceView
android:id="#+id/surfaceViewBarcodeScanner"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="#+id/linearLayoutToolbar"
/>
And I've not found use of below attribute in SurfaceView.
android:layout_gravity="top"
android:scaleType="fitXY"
Checkout this layout designs, how android:layout_height="0dp" works, and how android:layout_width="0dp" works.
I have used that kind of layouts in many apps, and here is how should be your layout. No tricks, no praying for luck, this is just the clean, happy, and always-working way to achieve it
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:screenOrientation="portrait" >
<SurfaceView
android:id="#+id/surfaceViewBarcodeScanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayoutToolbar"
android:layout_alignParentTop="true" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayoutToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_left" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.68"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_center_toolbar" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/scanner_bottom_right_landscape_button" />
</LinearLayout>
</RelativeLayout>
here you have a preview on one of my apps.
The posted code is adapted to yours so a copy/paste will do it. please note that its everything in a simple file, so you have to change slightly your activity
Related
I am working on an android project, in which I need to set ImageView
background (ascending icon and descending icon) multiple times. to do
this I am doing it with a clicking variable like:
sortBookedOnLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pickUpImage.setBackgroundResource(R.drawable.ascending);
vouchredImage.setBackgroundResource(R.drawable.ascending);
cancledImage.setBackgroundResource(R.drawable.ascending);
if(Constant.bookedOn == 1) {
Log.d(TAG, "FliteronClick:1 ");
bookedonImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ascending_active));
// int ascending_active_b = R.drawable.ascending_active;
// bookedonImage.setBackgroundResource(ascending_active_b);
// bookedonImage.setImageDrawable(mContext.getResources().getDrawable( R.drawable.ascending_active));
// bookedonImage.setBackground(mContext.getResources().getDrawable(R.drawable.ascending_active));
// ivPickUpTxt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ascending_active, 0);
Constant.bookedOn = 2;
}else if(Constant.bookedOn == 2){
Log.d(TAG, "FliteronClick:2 ");
bookedonImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.decending_active));
// bookedonImage.setImageResource(android.R.color.transparent);
// int decending_active_b = R.drawable.decending_active;
// bookedonImage.setBackgroundResource(decending_active_b);
// bookedonImage.setImageDrawable(mContext.getResources().getDrawable( R.drawable.decending_active));
// ivPickUpTxt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.decending_active, 0);
Constant.bookedOn = 1;
}
}
}
});
And My Xml layout is just like:
<LinearLayout
android:id="#+id/sortBookedOnLayout"
android:orientation="horizontal"
android:background="#color/white"
android:weightSum="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_weight="1.8"
android:layout_width="0dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/bookedOntxtLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/ivPickUpTxt"
style="#style/editTextTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/edittext_background"
android:drawableLeft="#drawable/cal2"
android:drawablePadding="10dp"
android:hint="#string/booked_on"
android:inputType="text"
android:maxLines="1"
android:padding="10dp"
android:textColor="#000000"
android:textCursorDrawable="#drawable/cursor_drawable"
android:textSize="#dimen/filter_text_size"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_weight=".2"
android:layout_width="0dp"
android:layout_height="match_parent">
<ImageView
android:id="#+id/booedOnImage"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ascending"
/>
</LinearLayout>
</LinearLayout>
Screenshot of my layout.
in this image i am going to click on BookedOn layout.
And my targetSdkVersion 25
I have tried it multiple ways as I commented but image icon is not
reflecting on imageView. any help will be appreciable.Thanks.
Try this:
qImageView.setBackgroundResource(R.drawable.thumbs_down);
and add android:scaleType:fitxy
<ImageView
android:id="#+id/booedOnImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitxy"
android:background="#drawable/ascending"/>
(or) Try programmatically
imgview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Here's a thread that talks about the differences between the two methods.
I Understand your problem...You are not set the width of the LinearLayout
<LinearLayout
android:layout_weight=".2"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:id="#+id/booedOnImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/abc_btn_rating_star_off_mtrl_alpha"
/>
</LinearLayout>
I have a list with Swipe Actions on it using IOnTouchListener, as dragging occurs I am changing the Left of a LinearLayout to reveal a second LinearLayout behind it. That layout has two buttons, one is hidden. If the user does a "quick" swipe then I move the Left of the top view to a specific value and fire some code. This code changes the invisible button to be visible. When the ViewState of that button changes it makes the top view's Left snap back to 0 instead of staying where I place it. Any idea why it would do this or how to work around it.
The XML looks like... (But will change slightly based on the answer to another question I posted)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill_vertical"
android:background="#color/black"
android:id="#+id/Swipe"
>
<LinearLayout
android:id="#+id/LeftContentView"
android:layout_width="175dp"
android:layout_height="match_parent"
android:background="#color/yellow"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
>
<Button
android:id="#+id/ApproveButton"
android:layout_width="0dp"
android:layout_weight=".72"
android:layout_height="match_parent"
android:background="#2796C3"
android:text="Approve"
android:layout_alignParentLeft="true"
/>
<Button
android:id="#+id/ApproveUndoButton"
android:layout_width="0dp"
android:layout_weight=".28"
android:layout_height="match_parent"
android:background="#215681"
android:text="Undo"
android:layout_toRightOf="#id/ApproveButton"
/>
</LinearLayout>
<LinearLayout
android:layout_alignParentRight="true"
android:id="#+id/RightContentView"
android:layout_width="175dp"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="horizontal"
>
<Button
android:id="#+id/DenyButton"
android:layout_width="0dp"
android:layout_weight=".72"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="Deny"
/>
<Button
android:id="#+id/DenyUndoButton"
android:layout_width="0dp"
android:layout_weight=".28"
android:layout_height="match_parent"
android:background="#860000"
android:text="Undo"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/TopContentView"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1F1F1F">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="match_parent" >
<ImageView
android:id="#+id/UnreadImage"
android:layout_height="match_parent"
android:layout_width="7dp"
android:src="#drawable/vertical_blue_bar"
android:background="#2796C3"/>
<LinearLayout
android:id="#+id/ListText"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:padding="12dp">
<TextView
android:id="#+id/Text_Line1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dip"
/>
<TextView
android:id="#+id/Text_Line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dip"
/>
<TextView
android:id="#+id/Text_Line3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="11dip"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
I am not using Java, I am using Xamarin with C#. Here are the relevant pieces of code...
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
OriginalTouchX = Math.Ceiling(e.RawX);
index = e.ActionIndex;
pointerId = e.GetPointerId(index);
VelocityTracker.AddMovement(e);
break;
case case MotionEventActions.Move:
VelocityTracker.AddMovement(e);
ChangeX(newX);
VelocityTracker.ComputeCurrentVelocity(1);
float velocity = Math.Abs(VelocityTracker.GetXVelocity(pointerId));
if (velocity > SlowDrag && Math.Abs(distanceMoved) > (DragThreshold / 3))
{
QuickApprove();
}
break;
}
}
void QuickApprove()
{
ToggleUndoButton(true, true);
WaitingForUndo = true;
ChangeX(DragThreshold);
this.ApproveTimer.Start(true);
}
private void ToggleUndoButton(bool ShowUndo, bool LeftSide)
{
this.ApproveUndoButton.Visibility = ViewStates.Visible;
this.ApproveButton.Text = "Approving...";
}
private void ChangeX(int newX)
{
int width = this.TopContentView.Right - this.TopContentView.Left;
this.TopContentView.Left = newX;
this.TopContentView.Right = this.TopContentView.Left + width;
}
If in the ToggleUndoButton method I comment out the line
this.ApproveUndoButton.Visibility = ViewStates.Visible;
Everything works fine. The Left of TopContentView changes to be equal to my DragThreshold, the text changes to Approving... the timer starts, It stays this way for 2 seconds, then the code in my timer tick fires and the TopContentView is moved back to a Left of 0. If I leave in the visibility change then the TopContentView is immediately moved back to 0 instead of waiting until the timer is done.
I am using Vuforia AR sdk and want to create a button on the camera preview on the screen.
I cannot figure out where and how to add the button.
I have edit the camera_overlay_udt.xml like this.. In my layout design i have placed back button and listview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/header"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#android:color/transparent"
android:src="#drawable/back" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Swipart"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/arcstarButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:background="#android:color/transparent"
android:src="#drawable/star_button" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/favListingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/headerLayout"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ListView
android:id="#+id/favlist"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="7dp"
android:cacheColorHint="#00000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="#color/overlay_bottom_bar_background"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1" >
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/overlay_bottom_bar_separators" />
<ImageButton
android:id="#+id/camera_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:contentDescription="#string/content_desc_camera_button"
android:onClick="onCameraClick"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:src="#drawable/camera_button_background" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#id/bottom_bar"
android:background="#color/overlay_bottom_bar_separators" />
</RelativeLayout>
after that please Edit that ImageTargets.java class
private void addOverlayView(boolean initLayout) {
// Inflates the Overlay Layout to be displayed above the Camera View
LayoutInflater inflater = LayoutInflater.from(this);
mUILayouts = (RelativeLayout) inflater.inflate(
R.layout.camera_overlay_udt, null, false);
mUILayouts.setVisibility(View.VISIBLE);
// If this is the first time that the application runs then the
// uiLayout background is set to BLACK color, will be set to
// transparent once the SDK is initialized and camera ready to draw
if (initLayout) {
mUILayouts.setBackgroundColor(Color.TRANSPARENT);
}
// Adds the inflated layout to the view
addContentView(mUILayouts, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Gets a reference to the bottom navigation bar
mBottomBar = mUILayouts.findViewById(R.id.bottom_bar);
// Gets a reference to the Camera button
mCameraButton = mUILayouts.findViewById(R.id.camera_button);
mCameraButton.setVisibility(View.GONE);
favButton = (ImageButton) mUILayouts.findViewById(R.id.arcstarButton);
listview = (ListView) mUILayouts.findViewById(R.id.favlist);
backButton = (ImageButton) mUILayouts.findViewById(R.id.backButton);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
finish();
}
});
listview.setVisibility(View.GONE);
galleryList = SendFile.getFavourites();
if (galleryList != null) {
gridviewAdapter = new GridviewAdapter(ImageTargets.this);
listview.setAdapter(gridviewAdapter);
}
favButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (galleryList != null && galleryList.size() > 0) {
if (listview.getVisibility() == View.GONE) {
listview.setVisibility(View.VISIBLE);
} else {
listview.setVisibility(View.GONE);
}
} else {
Toast.makeText(ImageTargets.this, "Favourites not fond",
Toast.LENGTH_LONG).show();
}
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> paramAdapterView,
View paramView, int positon, long paramLong) {
SendFile.setFavourite(galleryList.get(positon));
Intent intent = new Intent(ImageTargets.this,
LoadingScreen.class);
Bundle bundle = new Bundle();
bundle.putInt("x", x_Axis);
bundle.putInt("y", y_Axis);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
});
showDialogHandler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Server Response: " + aResponse, Toast.LENGTH_SHORT)
.show();
showAlertDialog(aResponse);
} else {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Not Got Response From Server.", Toast.LENGTH_SHORT)
.show();
}
};
};
loadingDialogHandler.captureButtonContainer = mUILayouts
.findViewById(R.id.camera_button);
mUILayouts.bringToFront();
}
They showing there layouts using handlers
Start you camera preview in a normal way. Place a layout on top of it with transparent background like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff000000"
android:layout_height="match_parent">
<ImageView
android:id="#+id/start_image_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:layout_weight="1"
android:src="#drawable/scan_image"/>
</RelativeLayout>
In java file, you can add this layout like this:
private View mStartupView;
mStartupView = getLayoutInflater().inflate(
R.layout.startup_screen, null);
// Add it to the content view:
addContentView(mStartupView, new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
This way you will get to see your button on top of camera preview. Hope it helps
You can add buttons in cameraoverlay layout which is in layout folder and you can initialize buttons in initAR function which is in mainactivity.
Step 1: Add the button in the camera_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
style="#android:style/Widget.ProgressBar"
android:id="#+id/loading_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="51dp"
android:text="Button" />
</RelativeLayout>
Step 2: Edit the ImageTargets.java class
private static final String LOGTAG = "ImageTargets";
private Button b1;
Step 3: Modify the initApplicationAR() function of ImageTargets.java class
private void initApplicationAR()
{
// Create OpenGL ES view:
int depthSize = 16;
int stencilSize = 0;
boolean translucent = Vuforia.requiresAlpha();
mGlView = new SampleApplicationGLView(this);
mGlView.init(translucent, depthSize, stencilSize);
mRenderer = new ImageTargetRenderer(this, vuforiaAppSession);
mRenderer.setTextures(mTextures);
mGlView.setRenderer(mRenderer);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setVisibility(View.GONE);
}
});
}
Now lay back and watch your button disappear on a click!
Although it's a long time since the post.. yet I found one article.. wherein you can have the desired thing..
Ref: https://medium.com/nosort/adding-views-on-top-of-unityplayer-in-unityplayeractivity-e76240799c82
Solution:
Step1: Make a custom layout XML file (vuforia_widget_screen.xml). For example, button has been added.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout">
<FrameLayout
android:id="#+id/unity_player_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#null"
android:text="#string/welcome" />
</FrameLayout>
Step 2: Make following changes in the UnityPlayerActivity.java
Replace "setContentView(mUnityPlayer);" with
setContentView(R.layout.vuforia_widget_screen);
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.addView(mUnityPlayer.getView());
-> For anyone, who will face the issue in future. :)
I have problem with my qrreader.
I need scan only on center screen.
I need square where I have to put qr code before code is scaning.
I try added margin in FrameLayout, but I have blck screen on frame..
It is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
>
</FrameLayout>
<TextView
android:id="#+id/scanText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Scanning..."
android:layout_marginTop="20dp"
android:textColor="#android:color/white"
android:textSize="21sp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/prv" />
<Button
android:id="#+id/ScanButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:visibility="invisible"
android:text="Scan" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button"
android:onClick="turnFlash"
/>
</RelativeLayout>
And my code to scan:
PreviewCallback previewCb = new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
int result = scanner.scanImage(barcode);
if (result != 0 && !is_checking) {
is_checking = true;
String text="";
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
text = sym.getData();
}
if(text.substring(0, 3).matches("htt|www")){
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
scanText.setText(text);
v.vibrate(500);
try{
if(text.substring(0, 3).equalsIgnoreCase("www")){
scanText.setText("http://"+scanText.getText().toString());
}
startWebsite(scanText.getText().toString());
}catch(Exception e){
}
}
is_checking = false;
}
}
};
I am using Zbar libary.
Take a look at my app JustScan (can be found at code.google.com as eclipse project), where I used same sample code as you did and modified it as you need.
The idea is to override the method draw(Canvas canvas) of the CameraPreview-class, where you can draw your rectangle for the scan area. To ensure that your overridden method is called you have to call a setWillNotDraw(false) in dhe method surfaceCreated(...).
To crop the preview to your scan area crop the barcode image by using image method setCrop(...) before you pass it to scanImage() in the method onPreviewFrame(...) in the previewCallback function.
Sorry for this very short explanation, but I can't list the complete code here. Hope it helps anyway...
i got a problem with setContentView which throws me a null pointer.
In my mainFrame i got a small SurfaceView which takes about 50% of the screen.
I wanted to implement a method which makes that SurfaceView go Full Screen if you long clicked it. I did it like this:
final FrameLayout frame = (FrameLayout) findViewById(R.id.frame_layout);
frame.setOnLongClickListener(new OnLongClickListener() {
boolean clicked = false;
#Override
public boolean onLongClick(View v) {
if (clicked){
clicked = false;
(MyActivity.this).setContentView(R.layout.main);
} else {
clicked = true;
(MyActivity.this).setContentView(R.layout.fullScreen);
}
return true;
}
});
Can someone help me how to fix this problem?
Greetings
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical"
android:weightSum="1"
android:background="#drawable/background_new" >
<TextView
android:text="#string/measures"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/text_measures"
android:textColor="#ff444444"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
/>
<ImageButton
android:contentDescription="#string/desc_edge"
android:layout_toRightOf="#id/text_measures"
android:id="#+id/edges_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/edges_black"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="30dp"
android:onClick="edge_handler"
android:background="#null"
/>
<FrameLayout
android:background="#xml/border"
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginBottom="35dp"
android:layout_marginRight="200dp"
android:layout_marginTop="80dp"
android:layout_below="#id/text_measures"
>
</RelativeLayout>
I know it looks like a FrameLayout but dont get fooled it was done by someone else and it is actually a SurfaceView.
Just change the layout parameters of the SurfaceView onLong click of frame. No need to setContentView again.
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
surfaceView.setLayoutParams(lp);