I have a requirement in android application to show 5 buttons each of them will open the camera intent to allow the user to take a picture. every image is related to a certain information thus each of the images will have a description in the data base and a special id. for example, the first button is to take an image of a car. therefore all images that taken for cars will have id = 1 and a description like "Image of the car .." and so on.
also when the user take the picture I will replace the button with another button to view the image that just has been taken.
Now what i tried to do when the user clicks on the first button I put an extra to the intent with id = 1 and in onActivityResult i'll check if the extra is equal to 1 I will store the image and its id in an hash map. But unfortunately I can't use extras with implicit intent calls.
Any idea or suggestion?
use different request_code for intent from all button and based on request_code you can perform action accordingly in onActivityResult .
Related
Good day,
I am trying to add a textview preview to my app. For example, i created a news app with card view and I want to have a few lines on the first screen and then it expands the full story in a new activity onclick.
I have already set up the new activity part but I just need the preview on the first screen
You should have all the News data available on your first Activity - NewsActivity.
In that case you just
Limit number of lines of preview TextView with attribute android:maxLines="3"
Then on click on TextView you would open another activity with an Intent and inside the Intent pass whole News object that corresponds to data displayed in preview TextView. Remember that News object should be implement Serializable or Parcelable interfaces to be passed with intent.
Retrieve data from an Intent and display it in the second Activity - NewsDetailsActivity
I am new to Android. In my application, in ActivityOne I want to allow the user to take a picture using a button. The picture is then stored in the gallery and displayed in an ImageView in ActivityTwo.
My question is: How can I add a button to ActivityTwo that allows the user to retake the picture if they don't like it when displayed in the ImageView. Basically, the button will do the same as the one in ActivityTwo. Do I need to write the same code for the button?
I have tried to create a separate that implements View.OnClickListener, add the functionality in the onClick method, and in each activity create an instance of that class attached to the button:
onClick(View v) {
int id = v.getId();
if (id == R.id.myButton) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
myfile = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
v.startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
However, this causes errors as the class is not an activity.
Any help is appreciated.
You can write the same logic for this second button, but as Naveen mentioned it would be easier to put the button on the first activity, leading to the camera. When the user takes a picture just add an imageview to the layout above the button using linearlayout on the first activity. Then the picture is displayed and the user has the option to retake it.
The best option is to let the user take the picture in the activity where it is needed. But if there is a need for you to use it in multiple places, you should extract the code triggered by the button in the first activity(The code that takes the picture) into a separate class. then you can call it from anywhere when needed.
#Subzero-273k Follow this.
In your activity make two different layouts. One, which shows your first button, remember to hide the other layout by setting the visibility.
Once user clicked the button1 which calls someOnClick(), hide first layout and enable another layout, which shows you the imageView and button2, onclick of button2 still calls your old function someOnClick().
How about this? As you dint mention what else you are doing in two different activities, I assume you just show activity one for single button.
I'm having a problem where I just don't know how to save input user data and then recall it. Let's say I want to store someone's name, so I believe I would use an Edit Text box, but that's all I can really figure out. I then later want to display that information in another activity. If you could link me to something or guide me in the right direction it would meant the world to me.
Well if you want persistent storage even after the user closed the app, SQLite or sharedpreferences is for you. Otherwise you can juse use Bundles to pass around the small bit of data in intents when starting a new activity.
SQLite guide,Sharedpreferences guide. Hope these helped.
Use an EditText to allow the user to input their name. Get the value from the EditText component via the EditText's getText() method. After that you have lot of different ways you can go. Save it for later or just pass it on via an intent to the next activity.
Good luck.
The term "Storing" should have a scope. Say, you are gonna store the data for a specific code block, or at activity level, or at application level, or you want persistent(storing data in Preferences or Database, for example, storing the game score, etc) or non-persistent(temporary storage: declaring the variables in the activity) storage.
In your case, to get the Text from EditText and send it to next activity:
Declare a global variables,
EditText editText;
String data;
// write this inside onCreate, after the `setContentView()` method
editText = (EditText) findViewById(R.id....);
data = editText.getText().toString();
// say you want to pass the data to next activity on button click, so write this inside //onClick()
Intent intent = new Intent(currentActivityName.this, NextActivityName.class);
intent.putExtra("data", data);
startActivity(intent);
Up till now what we have done is, get text from EditText, and prepared for sending it to next it to next activity. Now, on button click, the data will be sent to next activity.
But on next activity, you need to catch the data, that was sent from previous activity, to use it.
So, in the next activity, write the following code inside onCreate()
Intent intent = getIntent();
String dataReceived = intent.getStringExtra("data");
Log.i("data received", dataReceived);
After doing this check if you got the log, with tag name "data received" and data you sent.
I've to develop an application that allows user to browse picture and write some information
about him, all these information will stored in a class with Person name,
After he presses NextButton the activiy should moves him to another activity with these information in the second activity he'll type another information about him,
well in the 3rd activity I should receive all these info. from ( 1st and 2nd ) activities then, showing it in the 3rd one ,,
my Questions are:
1- How can move more than one info. I write a code that moves string and other code to move picture, but I couldn't combine them with each other!
2- How can I insert the information that will be typed in the second activity to the same object of Person Class ??
Hope my Questions and my scenario is clear!!
thanks alot
Shomokh =)
-----------------------Updating----------------------------------------
// this is for String info
String FullPersonInfo = person1.toString();
Bundle basket = new Bundle();
basket.putString("key", FullPersonInfo);
Intent intent = new Intent(Form_1.this,Form_2.class);
intent.putExtras(basket);
startActivity(intent);
// I'm confusing how can I add image when i try this code it doesn't work
intent.putExtra("URI", selectedImageUri.toString() );
You can pass in extra information in the intent you are passing in to the next activity and then read the intent using intent.getExtra in the 2nd and 3rd activity. I hope that gives you better idea.
As omkar.ghaisas aluded to above you can pass extras to an Intent. However, you can only pass primitives OR any object which implements the Parcelable interface. Passing string is easy enough but the image data is trickier.
I think you have a couple of options:
Write the image path to disk and then pass the file location as a
string using the Extras in the Intent, plus any other strings you
need, e.g. other metadata
Grab the image as a byte[] array, create a class which implements
Parceleable and shuffle the byte[] around.
I think #1 is probably easier.
you can Create Parcelable object of your class that content all information whcih u want to move from one activity to another.use Bundle.putParcelable and extract all class object in another Activity.
for passing Parcelable custom or complex object between activites see:
Passing a list of objects between Activities
http://prasanta-paul.blogspot.in/2010/06/android-parcelable-example.html
Android: Passing object from one activity to another
and as your application requirement but do't pass whole image in bundle just pass a reference path and text to another activity and retrieve image from path
I have an android app which is a simple home screen. What I want is to create an option where users can select background by themselves. Currently I have made the option but it does not hold the background after I restart or press back button. It goes back to the background what I set in the xml file as android:background. Is there any way out? I heard by using shared preference it's possible. but don't know how to do it. Can any one help on this?
Assuming you have several backgrounds available for them to choose and they select one and you want to simply store the id of the background they have chosen you would do this:
SharedPreference sp = getSharedPreferences("uniqueString", 0);
Editor editor = sp.edit();
editor.putInt("userBG", 5);// this is assuming the user picked your 5th bg
editor.commit();
Then onCreate in your activity you can load the SharedPreference again and...
int userPic = sp.getInt("userBG", 0); //this will either pull a previously saved number or return 0 (or whatever number you choose to provide as the default there.
That's it.
Here's the example from google, in case my snippet isn't sufficient...
http://developer.android.com/guide/topics/data/data-storage.html
Note that if you're storing an int, you can store the drawable id of the background itself, which saves you one step of translation. You can then just pull the resource directly into the background of your parent layout.