I am trying to create an app where the user can take a photo and set it as wallpaper.Now when i click the button and get back without taking a photo and click set wallpaper i want to display an alert dialog box.I have been able to set it when the image capture is not initiated but not when initiated but no image is taken
package com.sagarapp;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Camera extends Activity implements View.OnClickListener{
ImageView img;
ImageButton takepic;
Button setdp;
Intent i;
final static int cameraData = 0;
Bitmap bmp;
final Context context = this;
boolean taken = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
initialize();
takepic.setOnClickListener(this);
setdp.setOnClickListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
img = (ImageView)findViewById(R.id.Ivpic);
takepic = (ImageButton)findViewById(R.id.Ibcapture);
setdp = (Button)findViewById(R.id.Bsetdp);
}
#SuppressWarnings("deprecation")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.Ibcapture:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
taken = true;
startActivityForResult(i,cameraData);
break;
case R.id.Bsetdp:
try {
if(taken)
getApplicationContext().setWallpaper(bmp);
else{
AlertDialog.Builder alertDialogBulider = new AlerDialog.Builder(context);
alertDialogBulider.setTitle("Warning!");
alertDialogBulider.setMessage("No Image Is Available");
alertDialogBulider.setCancelable(false);
alertDialogBulider.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBulider.create();
alertDialog.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
bmp = (Bitmap)extras.get("data");
img.setImageBitmap(bmp);
}
else
{
AlertDialog.Builder alertDialogBulider = new AlertDialog.Builder(context);
alertDialogBulider.setTitle("Warning!");
alertDialogBulider.setMessage("No Photo Is Taken");
alertDialogBulider.setCancelable(false);
alertDialogBulider.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBulider.create();
alertDialog.show();
}
}
}
Your taken boolean variable should only be set to true if you receive a RESULT_OK resultcode in onActivityResult() method. That means that the user actually took a picture and saved it. Remove it from the R.id.Ibcapture View onClick () method.
Related
package com.example.thenewjay;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Camera extends Activity implements View.OnClickListener {
ImageView iv;
Button but;
ImageButton ib;
final static int cameraData = 0;
Intent i;
Bitmap bmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initialize();
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmp = BitmapFactory.decodeStream(is);
}
private void initialize() {
iv = (ImageView) findViewById(R.id.ivReturnedPic);
ib = (ImageButton) findViewById(R.id.ibTakePic);
but = (Button) findViewById(R.id.bSetWall);
but.setOnClickListener(this);
ib.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bSetWall:
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.ibTakePic:
i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 1);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}}
}
why is the below code force closes on using built in camera in android
project?
Because you are missing setContentView in onCreate before calling initialize().
You will need to set layout for current Activity before calling findViewById to access views from current screen xml.do it as in onCreate:
...
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_name);
initialize();
....
i made three activities for a game i want to go from 1st to 2nd to 3rd and then go 1st 2nd 3rd again but i got confused with the whole intent thing tried all kind of combinations but for some reason i stuck always with a problem currently it goes from 1st to 2nd to 3rd to 1st and when it supposed to lunch 2nd it exits from the whole app
//1st activity
Intent discoverintent = new Intent(getBaseContext(), discoverstring.class);
//discoverintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(discoverintent);
//2nd activity
Intent MainIntent = new Intent(getBaseContext(), learningword.class);
//MainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainIntent.putExtra("text", text);
MainIntent.putExtra("speakeronoff", Integer.toString(speakeronoff));
MainIntent.putExtra("previous", Integer.toString(previous));
startActivity(MainIntent);
and the 3rd activity just uses finish
package self.yanfaingold.talkgame;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
#SuppressLint("NewApi") public class savewords extends Activity implements OnTouchListener {
OurView v;
//Intent secondintent;
Canvas c;
int cheight,cwidth,loop=0;
Bitmap notalk,notalkscaled,bittalk;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v=new OurView(this);
v.setOnTouchListener(this);
//Remove title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(v);
notalk=BitmapFactory.decodeResource(getResources(), R.drawable.nottalk);
// bittalk=BitmapFactory.decodeResource(getResources(), R.drawable.talkmid);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable {
Thread t=null;
SurfaceHolder holder;
boolean isItOK=false;
public OurView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder=getHolder();
}
public void run(){
while(isItOK){
if(!holder.getSurface().isValid()){
continue;
}
c=holder.lockCanvas();
cheight=c.getHeight();
cwidth=c.getWidth();
scale();
drawing(c);
holder.unlockCanvasAndPost(c);
}
}
private void scale() {
// TODO Auto-generated method stub
if(loop==0)
notalkscaled=Bitmap.createScaledBitmap(notalk,(int)(cwidth),(int)(cheight), false);
}
protected void drawing(Canvas canvas){
//canvas.drawARGB(255, 255, 255, 255);
canvas.drawBitmap(notalkscaled,0,0, null);
if(loop>500){
loop=0;
wannalearnaword();
}
loop++;
}
private void wannalearnaword() {
// TODO Auto-generated method stub
//1st activity
Intent secondintent = new Intent(getApplicationContext(), discoverstring.class);
secondintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(secondintent);
}
public void pause(){
isItOK=false;
while(true){
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
t=null;
}
public void resume(){
isItOK=true;
t=new Thread(this);
t.start();
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
}
I don't know whats going wrong with my app i followed so many tutorials, any hint/help/advise would be appreciated, I am new on android. i am getting error as in onserviceregistrationfailed function of listner.
package com.example.ravinetworkapplication;
import java.io.IOException;
import java.net.ServerSocket;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.pm.ServiceInfo;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdManager.DiscoveryListener;
import android.net.nsd.NsdServiceInfo;
import android.net.nsd.NsdManager.RegistrationListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
NsdManager mNsdManager;
Context context;
NsdManager.DiscoveryListener mDiscoveryListner=null;
NsdManager.RegistrationListener mRegistrationListener=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
mNsdManager= (NsdManager) this.getSystemService(Context.NSD_SERVICE);
final ServerSocket mServerSocket;
final ArrayList<String> listName = new ArrayList<String>();
Integer localPort = 0;
final ListView list = (ListView) findViewById(R.id.listView1);
listName.add("arunima");
listName.add("abhishek");
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this , android.R.layout.simple_list_item_1,listName);
list.setAdapter(adapter);
try {
mServerSocket = new ServerSocket(0);
localPort = mServerSocket.getLocalPort();
mServerSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mRegistrationListener = new NsdManager.RegistrationListener() {
#Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
// TODO Auto-generated method stub
}
#Override
public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
// TODO Auto-generated method stub
}
#Override
public void onServiceRegistered(NsdServiceInfo serviceInfo) {
// TODO Auto-generated method stub
Log.v("service registrd", "i am on folks");
Toast.makeText(getApplicationContext(), "Registerd", Toast.LENGTH_LONG).show();
}
#Override
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
// TODO Auto-generated method stub
Log.v("service registration fail", "can not register service kumar ravi");
}
};
mDiscoveryListner = new NsdManager.DiscoveryListener() {
#Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
// TODO Auto-generated method stub
Log.v("cannot stop service", "service discovery failed");
}
#Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
// TODO Auto-generated method stub
Log.v("can not start", "Can not start discovery kumar ravi "+errorCode);
}
#Override
public void onServiceLost(NsdServiceInfo service) {
// TODO Auto-generated method stub
}
#Override
public void onServiceFound(NsdServiceInfo service) {
// TODO Auto-generated method stub
Log.v("service found", "we found the service");
if(!service.getServiceType().equals("_myapp.tcp.")){
}
else if(service.getServiceName().equals("kumar")){
}
else {
listName.add(service.getServiceName());
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
Toast.makeText(getApplicationContext(), "Finally found the service "+service.getServiceName(), Toast.LENGTH_LONG).show();
}
}
#Override
public void onDiscoveryStopped(String serviceType) {
// TODO Auto-generated method stub
}
#Override
public void onDiscoveryStarted(String serviceType) {
// TODO Auto-generated method stub
Log.v("Service Discovery Started", "Searching of Service Discovery");
}
};
Log.v("registerd port", localPort.toString());
final NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setPort(localPort);
serviceInfo.setServiceName("kumar");
serviceInfo.setServiceType("_myapp.tcp.");
//mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
Button startBroadcast = (Button) findViewById(R.id.button1);
Button startListen = (Button) findViewById(R.id.button2);
startBroadcast.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}
});
startListen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mNsdManager.discoverServices("_myapp.tcp.", NsdManager.PROTOCOL_DNS_SD, mDiscoveryListner);
}
});
}
#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;
}
#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;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
try{
mNsdManager.unregisterService(mRegistrationListener);
mNsdManager.stopServiceDiscovery(mDiscoveryListner);
Log.v("Stopped", "Scanning stopped"); }
catch(Exception e){
Log.v("some error in unregistring service", e.toString());
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
I'm working on an app where whatevever the user speaks on the phone gets added to the listview(up to this is working fine) and on clicking that item on that list view,send that over to the parse server.
This is my class called Speech
`package com.example.projecta;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;
public class Speech extends Activity implements OnClickListener {
ListView viewlist;
static final int check = 1111;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.speech);
Parse.initialize(this, "JnmLmnvOjxvRgBMGPl4XzOzvoqPPY7KGG2cqiExL", "8ejsJCanB26YYB6Io3jKov5wcwuajcB1zjRPUyMs");
ParseObject.registerSubclass(Storedata.class);
ParseAnalytics.trackAppOpened(getIntent());
viewlist = (ListView)findViewById(R.id.speechview);
Button b = (Button)findViewById(R.id.speechb);
b.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
in.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
in.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Louder");
startActivityForResult(in, check);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == check && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
viewlist.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
}
super.onActivityResult(requestCode, resultCode, data);
}
public void createStoredata(View v) {
Storedata st = new Storedata();
st.setDescription(viewlist.toString());
st.saveEventually();
}
}
`
This is my store data class
package com.example.projecta;
import com.parse.ParseObject;
public class Storedata extends ParseObject{
public Storedata(){
}
public void setDescription(String description){
put("description", description);
}
{
}
}
I really need help with this thanks.
You need to add a listener to your listview itself to click the items
viewlist.setClickable(true);
viewlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//perform action with your item
}
});
Hy experts.
i am new to android, i am trying to generate an intent to go on next activity, i am using listview, when i click on list item, it should go to that class which item item is clicked.
here is my code.
package com.example.data_server_assi;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Menu extends ListActivity{
String[] menu = {"AddInfo","DataBaseInfo"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, menu));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
try {
Toast.makeText(Menu.this, "Test" ,Toast.LENGTH_SHORT).show();
Class menuItem = Class.forName("com.example.data_server_assi."+menu[position]);
Intent menuIntent = new Intent(Menu.this,menuItem);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}`
Put below code, You have missed one line of code:
Intent menuIntent = new Intent(Menu.this,menuItem);
startActivity(menuIntent);
OR
startActivity(new Intent(menu.this,menuItem));
You have missed starting your activity by-
startActivity(YOUR_INTENT_NAME);