I am trying to deveolp an Android application. I am learning Android so there I go.
My aplication is similar to this one: http://www.tutorialspoint.com/android/android_camera.htm ;
Except I have two activities.. One is called Report.java and the other one is called Form.java.
In Report.java, I have a button, when I click the button, I want to open the camera and take a picture. After I took the picture, I want to start the second activity (Form.java) and my image to be saved in an ImageView.
How can I do that? Thank you in advance.
Report.java
package com.example.harta;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Report extends Activity implements View.OnClickListener{
ImageView iv;
Button report;
final static int cameraData = 0;
Bitmap bmp;
Intent i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
initialize();
report.setOnClickListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
report = (Button) findViewById(R.id.bReport);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bReport:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
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");
Intent i = new Intent(Report.this, Formular.class);
startActivity(i);
}
}
}
Form.java
package com.example.harta;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
public class Formular extends Activity{
Button button;
ImageView iv;
int cameraResults;
final static int cameraData = 0;
Bitmap bmp;
TextView tvAdress, tvAmpers, tvCommunication, tvCoordinates;
TextView tvName, tvInformations, tvDataIntro, tvPhone, tvLatitude;
TextView tvLongitude, tvInstalledPower, tvContor, tvNetwork;
EditText etAdress, etAmpers, etCommunication, etCoordinates;
EditText etName, etInformations, etDataIntro, etPhone, etLatitude;
EditText etLongitude, etInstalledPower, etContor, etNetwork;
Spinner spinNetwork;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.formular);
initialize();
}
private void initialize() {
// TODO Auto-generated method stub
tvAdress = (TextView) findViewById(R.id.tvAdresa);
tvAmpers = (TextView) findViewById(R.id.tvAmperaj);
tvCommunication = (TextView) findViewById(R.id.tvComunicatie);
tvCoordinates = (TextView) findViewById(R.id.tvCoord);
tvName = (TextView) findViewById(R.id.tvDenumire);
tvInformations = (TextView) findViewById(R.id.tvInfo);
tvDataIntro = (TextView) findViewById(R.id.tvIntroducereDate);
tvPhone = (TextView) findViewById(R.id.tvIPtelefon);
tvLatitude = (TextView) findViewById(R.id.tvLat);
tvLongitude = (TextView) findViewById(R.id.tvLong);
tvInstalledPower = (TextView) findViewById(R.id.tvPutereInstalata);
tvContor = (TextView) findViewById(R.id.tvSerieContor);
tvNetwork = (TextView) findViewById(R.id.tvTipulRetelei);
etAdress = (EditText) findViewById(R.id.etAdresa);
etAmpers = (EditText) findViewById(R.id.etAmperaj);
etCommunication = (EditText) findViewById(R.id.etComunicatie);
etName = (EditText) findViewById(R.id.etDenumire);
etPhone = (EditText) findViewById(R.id.etIPtelefon);
etInstalledPower = (EditText) findViewById(R.id.etPutereInstalata);
etContor = (EditText) findViewById(R.id.etSerieContor);
spinNetwork = (Spinner) findViewById(R.id.spinTipulRetelei);
button = (Button) findViewById(R.id.bTrimitere);
iv = (ImageView) findViewById(R.id.ivPic);
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Related
I am trying to get the input from the EditText fields and store it into an SQLite database. But the Fields are null even after I enter text into it.
I did a similar thing earlier and it ended up just fine but I am confused what is causing the problem here.
package com.example.mridul.eventmanager;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CreateEvent extends AppCompatActivity {
EditText edit,edit2,edit3,edit4;
TextView t1;
ImageView img;
Button bpost;
public static final int PICK_IMAGE=1;
Uri imageuri;
Event in;
DatabaseHelper dc = new DatabaseHelper(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event);
edit = (EditText) findViewById(R.id.editText7);
edit2 = (EditText) findViewById(R.id.editText8);
edit3 = (EditText) findViewById(R.id.editText9);
edit4 = (EditText) findViewById(R.id.editText10);
t1 = (TextView) findViewById(R.id.textView6);
img = (ImageView) findViewById(R.id.imageView);
bpost = (Button) findViewById(R.id.button3);
t1.setOnClickListener(new View.OnClickListener(){
public void onClick(View vim){
operationGallery();
}
});
bpost.setOnClickListener(new View.OnClickListener(){
public void onClick(View vPost){
in.sEventName(edit.getText().toString()); //Problem here
in.sEventDesc(edit2.getText().toString());
in.sEventVenue(edit3.getText().toString());
in.sType(edit4.getText().toString());
in.sPic(imageuri.toString());
dc.addEvent(in);
Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_SHORT).show();
Intent gBack = new Intent(CreateEvent.this, LoginActivity.class);
CreateEvent.this.startActivity(gBack);
}
});
} public void operationGallery(){
Intent im = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(im , PICK_IMAGE);
}
public void onActivityResult(int requestcode, int resultcode, Intent data){
super.onActivityResult(requestcode, resultcode, data);
if((resultcode == RESULT_OK)&&(requestcode == PICK_IMAGE)){
imageuri = data.getData();
img.setImageURI(imageuri);
}
}
}
The following is the error which I am getting after executing the code
You have just forgot to initiate Event object "in"
Your Event object in is uninitialized.
In my code, I have a class for a recipe and then another class for each step. The recipe class holds an arraylist of the steps in that recipe.
I have 3 processes being called one after the other:
MainActivity calls AddEditRecipe using startActivityResult
within AddEditRecipe I have another startActivityResult which calls AddStep
within AddStep I am adding a new instance of step to the list in the recipe.
It successfully adds the object to the list before finish() is called, but when I check the size of the list when I have gone to the 2nd activity it has reverted back to 0, even though I've managed to create buttons with the data I collected from the AddStep functions.
Basically I don't know what to do make it so it everything correctly persists.
Also a heads up, I am still fairly new too java so there's probably lots of other things wrong, any random advice is greatly appreciated.
MainActivity
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button newRecipe;
Button editRecipe;
Button deleteRecipe;
LinearLayout recipeLayout;
List<NewRecipe> listOfRecipes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listOfRecipes = new ArrayList<>();
recipeLayout = (LinearLayout) findViewById(R.id.RecipeListLayout);
newRecipe = (Button) findViewById(R.id.NewRecipeButton);
newRecipe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, AddRecipePopUp.class);
startActivityForResult(i,998);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode==999 && resultCode == RESULT_OK){
NewRecipe rec = (NewRecipe)data.getParcelableExtra("curr_recipe");
Log.d("Number of Steps",Integer.toString(rec.listOfSteps.size()));
UpdateScreen(rec);
}
if(requestCode== 998 && resultCode == RESULT_OK){
NewRecipe recipe =(NewRecipe)data.getParcelableExtra("curr_recipe");
listOfRecipes.add(recipe);
Intent i = new Intent(MainActivity.this,AddEditRecipe.class);
i.putExtra("curr_recipe", recipe);
startActivityForResult(i,999);
}
}
void UpdateScreen(final NewRecipe recipe){
Button button = new Button(this);
button.setText(recipe.name);
button.setHeight(120);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NewRecipe recipe1 = recipe;
Button thisBut = (Button)view;
String butName = thisBut.getText().toString().toLowerCase();
for(int j = 0;j<listOfRecipes.size();j++){
String curr = listOfRecipes.get(j).name.toString().toLowerCase();
Log.d("Button ",thisBut.getText().toString());
Log.d("Current List ",listOfRecipes.get(j).name.toString().toLowerCase());
Log.d("Check bool statement", Boolean.toString(butName ==curr));
}
//recipe1 = listOfRecipes.get(0);
Intent i = new Intent(MainActivity.this, AddEditRecipe.class);
i.putExtra("curr_recipe",recipe1);
startActivityForResult(i,997);
}
});
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
recipeLayout.addView(button,lp);
}
}
AddEditRecipe
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class AddEditRecipe extends Activity {
Button home;
Button add;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_edit_recipe);
Intent i = getIntent();
final NewRecipe this_recipe = i.getParcelableExtra("curr_recipe");
Log.d("Number of Steps",Integer.toString(this_recipe.listOfSteps.size()));
LinearLayout recipeLayout = (LinearLayout) findViewById(R.id.RecipeListLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int j = 0; j<this_recipe.listOfSteps.size();j++){
Button button = new Button(this);
button.setText(this_recipe.listOfSteps.get(j).processName);
button.setHeight(120);
recipeLayout.addView(button,lp);
}
home = (Button) findViewById(R.id.HomeButton);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(AddEditRecipe.this, MainActivity.class);
i.putExtra("curr_recipe", this_recipe);
setResult(RESULT_OK,i);
Log.d("Number of Steps",Integer.toString(this_recipe.listOfSteps.size()));
finish();
}
});
add = (Button) findViewById(R.id.AddStepButton);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(AddEditRecipe.this, AddStep.class);
i.putExtra("curr_recipe", this_recipe);
startActivityForResult(i,999);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode==999 && resultCode == RESULT_OK){
NewRecipe curr_rec = (NewRecipe)data.getParcelableExtra("curr_recipe");
Button button = new Button(this);
button.setText(curr_rec.listOfSteps.get(curr_rec.listOfSteps.size()-1).processName + " " + curr_rec.listOfSteps.get(curr_rec.listOfSteps.size()-1).seconds );
button.setHeight(120);
LinearLayout lay = (LinearLayout) findViewById(R.id.RecipeListLayout) ;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lay.addView(button,lp);
}
}
}
AddStep
import android.app.Activity;
import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AddStep extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_step);
Intent i = getIntent();
final NewRecipe this_recipe = (NewRecipe) i.getParcelableExtra("curr_recipe");
Button finish = (Button) findViewById(R.id.AddStepButton);
finish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText name = (EditText) findViewById(R.id.nameEditText);
EditText hours = (EditText) findViewById(R.id.HoursEditText);
EditText minutes = (EditText) findViewById(R.id.MinutesEditText);
EditText seconds = (EditText) findViewById(R.id.SecondsEditText);
this_recipe.AddStep((ConvertToSeconds(Integer.parseInt(hours.getText().toString()),Integer.parseInt(minutes.getText().toString()),Integer.parseInt(seconds.getText().toString()))),name.getText().toString());
Intent j = new Intent(AddStep.this, AddEditRecipe.class);
j.putExtra("curr_recipe", this_recipe);
Log.d("Number of Steps",Integer.toString(this_recipe.listOfSteps.size()));
setResult(RESULT_OK,j);
finish();
}
});
}
int ConvertToSeconds(int h, int m, int s){
int ans = 0;
for(int i = 0; i<h;i++){
ans+=3600;
}
for(int i = 0; i<m;i++){
ans+=60;
}
for(int i = 0; i<s;i++){
ans++;
}
return ans;
}
}
I am trying to develop simple feedback application. When user enters invalid data it should show error. After error is detected all fields should be clear and I should stay on same activity What should I do?Here's my code:
package com.example.feedback;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Feedback extends Activity {
String s;
boolean fill=true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feedback);
final Button bt1 = (Button) findViewById(R.id.bt1);
final EditText tv2 = (EditText) findViewById(R.id.tv2);
final EditText tv1 = (EditText) findViewById(R.id.tv1);
final EditText tv3 = (EditText) findViewById(R.id.tv3);
final EditText tv4 = (EditText) findViewById(R.id.tv4);
final RadioGroup rg = (RadioGroup) findViewById(R.id.rg1);
bt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
s = tv1.getText().toString();
check();
s = tv2.getText().toString();
check();
s = tv3.getText().toString();
check();
s = tv4.getText().toString();
check();
if (rg.getCheckedRadioButtonId() == -1){
Toast.makeText(Feedback.this,"Error",Toast.LENGTH_LONG).show();
}
Toast.makeText(Feedback.this,"Press again to Submit",Toast.LENGTH_LONG).show();
bt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(Feedback.this,Feedback2.class));
// TODO Auto-generated method stub
}
});
// TODO Auto-generated method stub
}
private void check() {
if(s.matches("")){
Toast.makeText(Feedback.this,"Error",Toast.LENGTH_LONG).show();
}
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.feedback, menu);
return true;
}
}
this is the line:
startActivity(new Intent(Feedback.this,Feedback2.class));
after the click, change activity.
I suggest you to check login success in onActivityResult method and if it's true than start new activity
I know it's a pretty amateur question , but i am really getting frustrated:
I am not getting an error but its just not working.(Ignore the bundle part)package com.example.project1;import android.app.Activity;
here's the java file
package com.example.project1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Tictactoe2 extends Activity implements OnClickListener{
EditText et1 , et2;
String str1 , str2;
Button b1;
TextView tv2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ttt);
et1 = (EditText) findViewById (R.id.et1);
et2 = (EditText) findViewById (R.id.et2);
tv2 = (TextView) findViewById (R.id.tv2);
str1 = et1.getText().toString();
str2 = et2.getText().toString();
b1 = (Button) findViewById (R.id.b1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.b1:
tv2.setText(str1);
/*Bundle bun = new Bundle();
bun.putString("key", str1);
bun.putString("key1", str2);;
Intent intent = new Intent(tictactoe2.this , tictactoe.class);
intent.putExtras(bun);
startActivity(intent);*/
break;
}}
}
I guess you got NPE. You need to remove this lines
str1 = et1.getText().toString();
str2 = et2.getText().toString();
It's because you're trying to getText() from EditText and there is no Text in it. So i would suggest you move this lines under some Listener events where you get values after you set value in your EditText.
You implement like
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.b1:
str1 = et1.getText().toString();
str2 = et2.getText().toString();
tv2.setText(str1);
}}
package com.example.project1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Tictactoe2 extends Activity implements OnClickListener{
EditText et1 , et2;
String str1 , str2;
Button b1;
TextView tv2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ttt);
et1 = (EditText) findViewById (R.id.et1);
et2 = (EditText) findViewById (R.id.et2);
tv2 = (TextView) findViewById (R.id.tv2);
b1 = (Button) findViewById (R.id.b1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.b1:
str1 = et1.getText().toString();
str2 = et2.getText().toString();
tv2.setText(str1);
/*Bundle bun = new Bundle();
bun.putString("key", str1);
bun.putString("key1", str2);;
Intent intent = new Intent(tictactoe2.this , tictactoe.class);
intent.putExtras(bun);
startActivity(intent);*/
break;
}}
}
you should place your "str1" String and textview and edittext code in your onClick.
like this:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.b1:
et1 = (EditText) findViewById (R.id.et1);
tv2 = (TextView) findViewById (R.id.tv2);
str1 = et1.getText().toString();
tv2.setText(str1);
/*Bundle bun = new Bundle();
bun.putString("key", str1);
bun.putString("key1", str2);;
Intent intent = new Intent(tictactoe2.this , tictactoe.class);
intent.putExtras(bun);
startActivity(intent);*/
break;
}}
I am writting an application for educational purposes in which user takes a picture from camera and sends it via email. I wrote almost all the code except the code whi But I don't know how to do it as I am learning android. Here is my code, What should I add in this code:
package com.umer.practice2;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class SendPhoto extends Activity implements View.OnClickListener{
TextView tv1,tv2;
EditText e1,e2;
ImageView pi;
ImageButton pb;
Button pb1,pb2;
Intent photo;
Bitmap bmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sendphoto);
pb1.setOnClickListener(this);
pb2.setOnClickListener(this);
pb.setOnClickListener(this);
initializeView();
}
private void initializeView() {
// TODO Auto-generated method stub
tv1= (TextView) findViewById(R.id.pt1);
tv2= (TextView) findViewById(R.id.pt2);
e1= (EditText) findViewById(R.id.pe1);
e2= (EditText) findViewById(R.id.pe2);
pi= (ImageView) findViewById(R.id.p1);
pb= (ImageButton) findViewById(R.id.pib2);
pb1= (Button) findViewById(R.id.pbut1);
pb2= (Button) findViewById(R.id.pbut2);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.pbut1:
String email[]={e2.getText().toString()};
Intent emailIntent= new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, email);
emailIntent.setType("image/jpeg");
startActivityForResult(emailIntent, 1);
break;
case R.id.pib2:
photo= new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photo,2);
break;
case R.id.pbut2:
try {
getApplicationContext().setWallpaper(bmp);
} 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);
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
pi.setImageBitmap(bmp);
}
}
In you onActivityResult method you should save the bitmap into a file on your SD-Card, next you should attach a file to the email intent.
how to save image to a file on sd-card
How to save a bitmap into phone's gallery?
how to attach file to an email intent:
Trying to attach a file from SD Card to email