I'm trying to create an app for turning a torch on and off on android and I'm getting a few errors I cant work out. The errors only seem to come up in the java activity, and seem to be something to do with the 'cam' activity I made. At the bottom of my java cam.release when I hover it says 'Add cast'.
Thanks for your help in advanced.
MainActivity.java
public class MainActivity extends Activity implements OnCheckedChangeListener {
Camera cam;
ToggleButton mTorch;
Parameters camParams;
private Context context;
AlertDialog.Builder builder;
AlertDialog alertDialog;
private final int FLASH_NOT_SUPPORTED = 0;
private final int FLASH_TORCH_NOT_SUPPORTED = 1;
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
mTorch = (ToggleButton) findViewById(R.id.toggleButton1);
} else {
showDialog(MainActivity.this,FLASH_NOT_SUPPORTED);
}
}
private void showDialog(MainActivity mainActivity, int fLASH_NOT_SUPPORTED2) {
// TODO Auto-generated method stub
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mTorch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#SuppressWarnings("deprecation")
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
try {
if (cam != null) {
cam = Camera.open();
}
camParams = cam.getParameters();
List<String> flashModes = camParams
.getSupportedFlashModes();
if (isChecked) {
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
} else {
showDialog(MainActivity.this,FLASH_NOT_SUPPORTED);
}
} else {
camParams.setFlashMode(Parameters.FLASH_MODE_OFF);
}
cam.setParameters(camParams);
cam.startPreview();
} catch (Exception e) {
e.printStackTrace();
cam.stopPreview();
cam.release();
}
}
});
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (cam == null) {
cam = Camera.open();
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (cam != null) {
cam.release();
}
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
cam.release();
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.t.torch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature
android:name="android.hardware.Camera"
android:required="true" >
</uses-feature>
<uses-feature
android:name="android.hardware.camera.FLASHLIGHT"
android:required="true" >
</uses-feature>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.t.torch.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dip"
android:text="#string/app_name"
android:textSize="25sp" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dip"
android:text="ToggleButton" >
</ToggleButton>
</LinearLayout>
Your primary problem is that cam is never being set. You're probably getting NullPointerExceptions on any line related to that variable.
The cause is:
if (cam != null) {
cam = Camera.open();
}
Which should of course be:
if (cam == null) {
cam = Camera.open();
}
Also note that cam may be null during the invocation of onStop().
Related
I am developing an Application for Android devices and I wanted to scan QR codes, but I have one problem, it just wont ask for the Camera permission, therefore the surfaceview just stays black.
My Code:
public class ScanQR extends AppCompatActivity {
private SurfaceView surfaceView;
private CameraSource cameraSource;
private TextView textView;
private BarcodeDetector barcodeDetector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_qr);
surfaceView = (SurfaceView) findViewById(R.id.scanQR);
textView = (TextView) findViewById(R.id.tvScanQr);
barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
cameraSource = new CameraSource.Builder(this, barcodeDetector).setRequestedPreviewSize(640, 480).build();
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
} try {
cameraSource.start(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
#Override
public void release() {
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
SparseArray<Barcode> qrCodes = detections.getDetectedItems();
if(qrCodes.size() > 0){
textView.post(new Runnable() {
#Override
public void run() {
Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
textView.setText(qrCodes.valueAt(0).displayValue);
}
});
}
}
});
}
Android Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.estcers.partygooo">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity" />
<activity android:name=".Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CreateParty" />
<activity android:name=".SearchUser" />
<activity android:name=".UserInfo" />
<activity android:name=".Friends" />
<activity android:name=".PendingFriends" />
<activity android:name=".Parties" />
<activity android:name=".Invitations" />
<activity android:name=".Entries" />
<activity android:name=".PendingAssistances" />
<activity android:name=".PartyInfo" />
<activity android:name=".GetQR" />
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="barcode" />
<activity android:name=".ScanQR"></activity>
</application>
XML:
<LinearLayout 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=".ScanQR"
android:orientation="vertical">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="300sp"
android:id="#+id/scanQR"
android:layout_gravity="center"
android:layout_marginTop="100sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tvScanQr"
android:text="AAAAAAAAAAAAAAAA"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="20sp"
android:gravity="center"/>
If anyone could help me with this problem I would be really grateful, I just have no clue of what is going on.
I also have the implementation:
"implementation 'com.google.android.gms:play-services-vision:15.0.2'" and google().
Thanks for any help!
When you're about to open the camera, include the following check:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(YourCameraActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
return;
}
and handle the result with:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
// close the app
Toast.makeText(CameraActivity.this, "The app requires that permission in order to work properly!", Toast.LENGTH_LONG).show();
finish();
}
}
}
and in your manifest:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I have used this code to create (trying) an app that opens its camera and starts recording videos, whether front or back, doesn't matter. But what ever i have tried i cannot seem to get it to work. When i press the button, nothing happens, no crash, no new view. I am not sure what the problem is.
package com.example.mediaworld;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
public MediaRecorder mrec = new MediaRecorder();
private Button startRecording = null;
//private button stopRecording;
File video;
private Camera mCamera;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(null, "Video starting");
startRecording = (Button)findViewById(R.id.buttonstart);
mCamera = Camera.open();
surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
menu.add(0,0,0, "StartRecording");
menu.add(0,1,0, "StopRecording");
return super.onCreateOptionsMenu(menu);
//return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
//int id = item.getItemId();
//if (id == R.id.action_settings) {
// return true;
//}
switch(item.getItemId()){
case 0: try{
startRecording();
}catch(Exception e){
String message = e.getMessage();
Log.i(null, "problem Start" + message);
mrec.release();
}
break;
case 1:
mrec.stop();
mrec.release();
mrec = null;
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
protected void startRecording() throws IOException{
mrec = new MediaRecorder();
mCamera.unlock();
mrec.setCamera(mCamera);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOutputFile("/sdcard/zzzz/3gp");
mrec.prepare();
mrec.start();
}
protected void stopRecording(){
mrec.stop();
mrec.release();
mCamera.release();
}
private void releaseMediaRecorder(){
if(mrec != null){
mrec.reset();
mrec.release();
mrec = null;
mCamera.lock();
}
}
private void releaseCamera(){
if(mCamera != null){
mCamera.release();
mCamera = null;
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
if(mCamera != null){
Parameters params = mCamera.getParameters();
mCamera.setParameters(params);
}else{
Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
finish();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera.stopPreview();
mCamera.release();
}
}
And here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mediaworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name= "android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
AND here is the xml file for main activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
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.mediaworld.MainActivity" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="#+id/buttonstart"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:text="Start Rec" />
</LinearLayout>
I think you forgot to register an "onClickListener" with your button:
startRecording.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startRecording();
}
});
I have quotes android app came with admob banner template but I don't how to integrate interstitials admob to this project. Template with database with sqlite.
Here codes for MainActivity.java
package com.XXXX.MYpackage;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import com.XXXX.MYpackage.R;
import com.XXXX.MYpackage.activity.AbstractContentActivity;
import com.XXXX.MYpackage.activity.FavouristList;
import com.XXXX.MYpackage.activity.QuoteList;
import com.XXXX.MYpackage.activity.SearchActivity;
import com.XXXX.MYpackage.activity.SettingActivity;
import com.XXXX.MYpackage.db.DataHeper;
import com.XXXX.MYpackage.db.entity.QOD;
import com.XXXX.MYpackage.utils.CustomSharePreferences;
import com.XXXX.MYpackage.utils.WriteLog;
public class MainActivity extends AbstractContentActivity{
private ImageButton btn_author;
private ImageButton btn_quotes;
private ImageButton btn_fav;
private ImageButton btn_facebook;
private ImageButton btn_search;
final static int AUTHORIZE_ACTIVITY_RESULT_CODE = 10;
private DataHeper dataHeper;
private TextView tv_body;
private PendingIntent mAlarmSender;
#Override
protected void initView() {
// TODO Auto-generated method stub
super.initView();
CustomSharePreferences sharePreferences = new
CustomSharePreferences(getApplicationContext());`
btn_author = (ImageButton)findViewById(R.id.d_authors_btn);
btn_quotes = (ImageButton)findViewById(R.id.d_quotes_btn);
btn_fav = (ImageButton)findViewById(R.id.d_favs_btn);
tv_body = (TextView)findViewById(R.id.d_qod_body);
btn_facebook = (ImageButton)findViewById(R.id.actionbar_facebook_btn);
btn_search = (ImageButton)findViewById(R.id.actionbar_search_btn);
dataHeper = new DataHeper(getApplicationContext());
btn_author.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
WriteLog.d("ThangTB", "onclick image");
Intent i = new Intent(getApplicationContext(), SettingActivity.class);
startActivity(i);
}
});
btn_quotes.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
WriteLog.d("ThangTB", "onclick image");
Intent i = new Intent(getApplicationContext(), QuoteList.class);
startActivity(i);
}
});
btn_fav.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
WriteLog.d("ThangTB", "onclick image");
Intent i = new Intent(getApplicationContext(), FavouristList.class);
startActivity(i);
}
});
btn_facebook.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
facebook();
}
});
btn_search.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intentSearch = new Intent(getApplicationContext(), SearchActivity.class);
startActivity(intentSearch);
}
});
mAlarmSender = PendingIntent.getService(MainActivity.this,
0, new Intent(MainActivity.this, AlarmService_Service.class), 0);
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, 30*1000, mAlarmSender);
}
#Override
protected int getViewLayoutId() {
// TODO Auto-generated method stub
return R.layout.dashboard;
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
new GetData().execute();
super.onResume();
}
private class GetData extends AsyncTask<Object, Object, Object>{
private QOD quoteOfDay;
public GetData() {
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
quoteOfDay = dataHeper.getQOD();
return null;
}
#Override
protected void onPostExecute(Object result) {
if (quoteOfDay!=null) {
tv_body.setText(Html.fromHtml(quoteOfDay.getBody()));
}
super.onPostExecute(result);
}
}
public void facebook() {
Uri localUri = Uri.parse(getResources().getString(R.string.facebook_fanpage));
Intent localIntent = new Intent("android.intent.action.VIEW", localUri);
startActivity(localIntent);
}
}
Here is some xml codes from admob banner (quotepreview.xml) and others xml using same code for admob--- I think only this codes for admob banner.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:orientation="vertical" android:id="#id/qp_main_parent"
android:background="#drawable/bg_default" android:clickable="true"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="vertical" android:id="#id/widget99"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:id="#id/ad_holder" style="#style/AdHolder" />
<RelativeLayout android:background="#drawable/actionbar_bg"
android:paddingRight="5.0dip" android:layout_width="fill_parent"
android:layout_height="48.0dip"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
<com.google.ads.AdViewandroid:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="k15XXX"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR,xxx2xxxx"
ads:loadAdOnCreate="true"/>
</RelativeLayout>
<ImageViewandroid:background="#drawable/dashboard_bar_shadow"android:layout_width=
"fill_parent"android:layout_height="6.0dip"/>
</LinearLayout>
<ScrollViewandroid:id="#id/qp_body_wrapper"android:clickable="true"
android:layout_width="fill_parent"android:layout_height="fill_parent"android:
layout_marginTop="10.0dip"android:layout_above="#id/widget34"android:layout_below
="#id/widget99">
<LinearLayoutandroid:orientation="vertical"android:id="#id/qp_body_
wrapper_container"android:clickable="true"android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextViewandroid:textSize="18.0dip"android:gravity="left"android:id="#id/qp_body"
android:padding="10.0dip"android:clickable="true"android:layout_width=
"fill_parent"android:layout_height="wrap_content"style="#style/QuoteViewText"/>
</LinearLayout>
</ScrollView>
<LinearLayoutandroid:gravity="center"android:orientation="horizontal"android:id=
"#id/widget34"android:layout_width="fill_parent"android:layout_height=
"wrap_content"android:layout_alignParentBottom="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<Buttonandroid:id="#id/qp_previous_btn"android:background="#drawable/
state_left_arrow"android:layout_width="48.0dip"android:layout_height=
"48.0dip"android:layout_margin="12.0dip"/>
<Buttonandroid:id="#id/qp_random_btn"android:background="#drawable/state_
refresh_btn"android:layout_width="48.0dip"android:layout_height="48.0dip"
android:layout_margin="12.0dip"/>
<Buttonandroid:id="#id/qp_favourite"android:background="#drawable/
qp_fav_btn_normal"android:layout_width="48.0dip"android:layout_height=
"48.0dip"android:layout_margin="12.0dip"/>
<Buttonandroid:id="#id/qp_next_btn"android:background="#drawable/
state_right_arrow"android:layout_width="48.0dip"android:layout_height=
"48.0dip"android:layout_margin="12.0dip"/>
</LinearLayout>
</RelativeLayout>
Here is code from AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.XXXX.MYpackage"
android:versionCode="1"
android:versionName="1" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.CustomDark" >
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"> >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.XXXX.MYpackage.Widget" android:label="#string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/widget_generic" />
</receiver>
<service android:name="com.XXXX.MYpackage.AlarmService_Service"
android:process=":remote" />
<activity android:name=".MainActivity" android:screenOrientation="portrait"></activity>
<activity android:name=".activity.AuthorList" android:screenOrientation="portrait">>
</activity>
<activity android:name=".activity.AuthorQuoteList"
android:screenOrientation="portrait"></activity>
<activity android:name=".activity.QuoteView" android:screenOrientation="portrait">
</activity>
<activity android:name=".activity.QuoteList" android:screenOrientation="portrait">
</activity>
<activity android:name=".activity.FavouristList" android:screenOrientation="portrait">
</activity>
<activity android:name=".activity.CategoryList" android:screenOrientation="portrait">
</activity>
<activity android:name=".activity.FilterList" android:screenOrientation="portrait">
</activity>
<activity android:name=".activity.CategoryQuoteList"
android:screenOrientation="portrait"></activity>
<activity android:name=".activity.SearchActivity" android:screenOrientation="portrait">
</activity>
<activity android:name=".activity.HelpActivity" android:screenOrientation="portrait">
</activity>
<activity android:name=".activity.SettingActivity"
android:screenOrientation="portrait"></activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|
screenSize|smallestScreenSize"/>
</application>
</manifest>
Please help me integrate admob interstitial. Thank you
You don't need any extra layout or Manifest config to integrate interstitials, but you will need to add a small amount of code.
See https://developers.google.com/mobile-ads-sdk/docs/admob/advanced
I'm trying to capture an image using TextureView, but the main.xml file is overridden by TextureView, and hence I cannot see the contents of the main.xml file. I'm trying to add TextureView into main.xml. Please give suggestions about how to improve my code.
Java:
public class MainActivity extends Activity implements TextureView.SurfaceTextureListener {
private Camera mCamera;
private TextureView mTextureView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextureView = (TextureView) findViewById(R.id.textureView1);
mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(this);
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mCamera = Camera.open(0);
try {
mCamera.setPreviewTexture(surface);
}
catch(IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
mCamera.stopPreview();
mCamera.release();
return true;
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
}
}
Layout resource XML:
<RelativeLayout 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: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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextureView
android:id="#+id/textureView1"
android:layout_width="350dp"
android:layout_height="350dp"
android:layout_below="#+id/textView1" />
I dont understand you question.
You say you want to add a TextureView to your activity_main.xml layout. but it is already there.
Obviously if you use setContentView(mTextureView); after setContentView(R.layout.activity_main); the entire layout will be gone. You are setting the activity content to a single view instead of the entire layout.
You are also creating a new TextureView and not using the one on the activity_main layout.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextureView = (TextureView)findViewById(R.id.textureView1);
mTextureView.setSurfaceTextureListener(this);
}
I think you should be doing something like this. I never tested this though.
It seems that you are trying to make a camera preview with the TextureView, there is a good example here, and a more advanced version here.
Check this code this is working for me.
MainActivity.java.
import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.widget.FrameLayout;
public class MainActivity extends Activity implements SurfaceTextureListener {
private TextureView myTexture;
private Camera mCamera;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTexture = new TextureView(this);
myTexture.setSurfaceTextureListener(this);
setContentView(myTexture);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#SuppressLint("NewApi")
#Override
public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1,
int arg2) {
mCamera = Camera.open();
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
myTexture.setLayoutParams(new FrameLayout.LayoutParams(
previewSize.width, previewSize.height, Gravity.CENTER));
try {
mCamera.setPreviewTexture(arg0);
} catch (IOException t) {
}
mCamera.startPreview();
myTexture.setAlpha(1.0f);
myTexture.setRotation(90.0f);
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
mCamera.stopPreview();
mCamera.release();
return true;
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
// TODO Auto-generated method stub
}
}
activity_main.xml
<RelativeLayout 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: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=".MainActivity" >
<TextureView
android:id="#+id/textureView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.textureview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.textureview.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I have followed this for making my app. When I run it on my android 2.1, the app gets stuck. Can you help me? What can I do to make my app working?
MainActivity.java
public class MainActivity extends Activity {
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://check.be?v=1";
WebView view = (WebView) this.findViewById(R.id.webView1);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<webView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="be.moviechecker.bioscoopprogramma"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<activity
android:name="be.moviechecker.bioscoopprogramma.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In your activity_main.xml file you've used 'webView' instead of using 'WebView' hence it is not getting recognized. Please change it to to WebView and check the result. It will work.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
#user3142817. Welcome. I've made below code make it work for you. Please check
public class MainActivity extends Activity {
private WebView view;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView view = (WebView) this.findViewById(R.id.webView1);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("http://www.google.com");
view.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
if (url != null && url.startsWith("http://"))
{
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
else
{
return false;
}
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK && view.canGoBack()){
view.canGoBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}