set text to textview if an image has been selected from gallery - android

I have a textview: Click to add an image behind an imageview.
when user click, it will open the gallery. If user select an image it will be displayed in the imageview.
I want to set the textview text to blank if an image has been selected.
I tried inside imageview on click:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
ImageViewText.setText("");
the problem here is the textview will be blank in the moment user click on the imageview. What I want is to reset it if user really selected an image.
Any ideas?

As you are using startActivityForResult(), you will get an image path imageUri from Intent in onActivityResult() method if an image selected successfully.
Add condition to Check RESULT_OK and REQUEST_CODE. If both true then set image to imageView and set textView value blank("") or change visibility to GONE or INVISIBLE.
Try this:
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
// Set image
imageView.setImageBitmap(selectedImage);
// Set text
textView.setText("");
// Alternative
// textView.setVisibility(View.GONE); or
// textView.setVisibility(View.INVISIBLE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
Hope this will help~

Since you're using startActivityForResult, you should have an onActivityResult method overridden. You can check there if the user has actually selected something which is where you can call setText("") or preferably setVisibility(View.GONE)

Related

Android - pass several parameters with putExtras between two activities

Pass brush size and brush shape between MainActivity and ChangeBrush.
Main:
static final int ACTIVITY_BRUSH_SIZE_REQUEST_CODE = 2;
static final int ACTIVITY_BRUSH_SHAPE_REQUEST_CODE = 3;
public void onClickBrush(View view) {
Intent intent = new Intent(MainActivity.this, ChangeBrush.class);
startActivityForResult(intent, ACTIVITY_BRUSH_SIZE_REQUEST_CODE);
startActivityForResult(intent, ACTIVITY_BRUSH_SHAPE_REQUEST_CODE);
}
ChangeBrush:
public void onClickChangeBrushSize(View view) {
String size = view.getTag().toString().;
// return the brush size to main activity
Bundle bundle = new Bundle();
bundle.putString("size", size);
Intent intent = new Intent();
intent.putExtras(bundle);
setResult(Activity.RESULT_OK, intent);
finish();
}
public void onClickChangeBrushShape(View view) {
String shape = view.getTag().toString();
// return the brush shape to main activity
Bundle bundle = new Bundle();
bundle.putString("shape", shape);
Intent intent = new Intent();
intent.putExtras(bundle);
setResult(Activity.RESULT_OK, intent);
finish();
}
SelectBrush activity layout:
When user click the size button and shape button in ChangeBrush activity, they could pass size and shape to MainActivity. In MainActivity, I use onActivityResult to set the brush size and shape.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
String color = bundle.getString("color");
String size = bundle.getString("size");
String shape = bundle.getString("shape");
switch(requestCode) {
case ACTIVITY_COLOR_REQUEST_CODE:
fingerPainterView.setColour(Color.parseColor(color)); // set new color value
case ACTIVITY_BRUSH_SIZE_REQUEST_CODE:
Log.d("FingerPainter", "Main activity change brush size to " + size);
case ACTIVITY_BRUSH_SHAPE_REQUEST_CODE:
Log.d("FingerPainter", "Main activity change brush shape to " + shape);
}
} else if(resultCode == RESULT_CANCELED) {
Log.d("FingerPainter", "MainActivity canceled");
}
}
This will log the return value:
D/FingerPainter: Main activity change brush shape to null
D/FingerPainter: Main activity change brush size to null
D/FingerPainter: Main activity change brush shape to SQUARE
It seems the return value could correspond to the right intent. I don't know why it log the shape information twice. Is there anything wrong with finish()? I want the user select both size and shape, and then return to the main activity.
If the method is wrong, what should I do to pass these two values?
Two problems I see:
1: You are calling startActivityForResult twice. You can pass extras using intent.putExtra(...).
2: You don't have a break in your switch statement:
switch(requestCode) {
case ACTIVITY_COLOR_REQUEST_CODE:
fingerPainterView.setColour(Color.parseColor(color)); // set new color value
break;
case ACTIVITY_BRUSH_SIZE_REQUEST_CODE:
Log.d("FingerPainter", "Main activity change brush size to " + size);
break;
case ACTIVITY_BRUSH_SHAPE_REQUEST_CODE:
Log.d("FingerPainter", "Main activity change brush shape to " + shape);
break;
}
If you are trying to update multiple things from a single Intent, I think you need to save off the current data that already exists in the Activity.
public void onClickBrush(View view) {
Intent intent = new Intent(MainActivity.this, ChangeBrush.class);
// intent.putExtra("shape", currentShape); // for example
// then, only start one Activity, with a generic request code, not one Activity twice with two codes
In other words, you start one Activity capable of changing all the data (even though you only update a single value at a time). So, save the current values when you start the next Activity, pass those back as unchanged while you update the new values. Then you shouldn't get null values.
For example, for the shape button.
public void onClickChangeBrushShape(View view) {
String shape = view.getTag().toString();
// return the brush shape to main activity
Bundle bundle = new Bundle();
bundle.putString("shape", shape); // Set the new shape
bundle.putString("color", color); // I assume you've saved this
bundle.putString("size", size); // and this...
// You could put string extras into the intent directly, but whatever
Intent intent = new Intent();
intent.putExtras(bundle);
setResult(Activity.RESULT_OK, intent);
finish();
}
Another option is using SharedPreferences to store all the values and forget about Intents and Bundles.

Android Change ImageButton image

I write an travel APP.There are 20 QR Code in every spot.
When tourist use QR Code scanner scan the QR Code,the ImageButton's image have to change into another image.
The problem on this line : spot1.setImageResource(R.drawable.hotspot1);
If I delete this line, there is no problem.
I don't know how to fix it.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (0 == requestCode && null != data && data.getExtras() != null) {
String result = data.getExtras().getString("la.droid.qr.result");
int spotnum=Integer.valueOf(result);
switch(spotnum){
case 1:
ImageButton spot1=(ImageButton)findViewById(R.id.imageButton1);
spot1.setImageResource(R.drawable.hotspot1);
setContentView(R.layout.hotspot1);
break;
case 2:
setContentView(R.layout.hotspot2);
break;
}
}
}
Here is my Logcat:http://i.stack.imgur.com/6y2UQ.png
You can not initialize any Views from xml before calling setContentView :
setContentView(R.layout.hotspot1);
ImageButton spot1=(ImageButton)findViewById(R.id.imageButton1);
spot1.setImageResource(R.drawable.hotspot1);
You are not suppose to call setContentView after setting view resource. call set contentView once in onCreate without setting any view from xml.
You can then change the layout contents, but do not call setContentView again.
You are using a image view, but not calling setContentView before it. This makes the ImageView as null, hence the error.
Try following above suggestions,and it will go. Happy Coding.

android : How to select multiple Images from gallery and then add into horizontal scrollable Multiple imageView

help me to solve this question in android :
android : How to select multiple Images from gallery and then add into horizontal scroll able Multiple imageView ...!
You can use this library for selecting multiple images
Just call the intent using
private void pickImages(){
final Intent pickIntent = new Intent(this, PickerActivity.class);
pickIntent.putExtra(PickerActivity.LIMIT_KEY, 6); // Set a limit , you can skip that if you want no limit
startActivityForResult(pickIntent, PickerActivity.PICK_REQUEST); //Open gallery
}
And in your onActivityResult,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(resultCode, requestCode, data);
if (requestCode == PickerActivity.PICK_REQUEST && resultCode == RESULT_OK) {
//No problemo
final String[] paths = data.getStringArrayExtra(PickerActivity.PICKED_IMAGES_KEY);//Paths for chosen images (Organized)
//Do what you want with paths
For displaying them horizontally u can use this link that shows implementation of horizontal listview using a custom library
in your xml use
<com.devsmart.android.ui.HorizontialListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ddd"
/>
and set adapter using
HorizontialListView listview = (HorizontialListView) findViewById(R.id.listview);
listview.setAdapter(myAdapter);

How can i show the image on full screen when i click any item of image in the gridview?

I have a list of T-shirt Image which is displayed in grid view.Now what i have to do is when i click any item of the image in the gridview,display the image in full screen.
one way to achieve this is :
make intent that display images from gallery: for that code is :
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, TFRequestCodes.GALLERY);
after that override the method onActivityResult():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
in that method you can get images ids and whatever you want to fetch :
and you can also display in it in your activity:

Android programming help

I'm working on an app where i have textview's in one layout and a button that sends you to a second layout with Edittext's. Every edittext is for an textview. How can i replace text in a textview with the text in edittext with a button in the second layout?
you mean like this ??
in the method onCreate() :
btn.setOnClickListener(this);
txtView = (TextView)findViewById(R.id.mytxtView);
editTxt = (EditText) findViewById(R.id.myeditText);
and then , ovverride the onClick method like this :
#Override
public void onClick(View v ) {
txtView.setText(editText.getText());
}
textview textview = (textview)findViewById(R.layout.nameoftextview);
edittext edittext = (edittext)findViewById(R.layout.nameofedittext);
textview.settext(edittext.text());
First of all, you will have to pass the edittext value to the first activity through intent.
Eg:
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("edittext_value", edittext.getText().toString());
startActivity(i);
Then inside your first activity, you will have to fetch this data as:
String value;
Bundle extras = this.getIntent().getExtras();
if (extras != null) {
value = extras.getString("edittext_value");
textview.setText(value);
}
Hope this may help you.
From what I understand is that you want your second activity (let's call it Activity2) to pass text back to the first one (Activity1). To do that, you have to (some code comes from :
Change the way you open Activity2 to
Intent EditIntent = new Intent(this, Activity2.class);
//Other stuff you may want to do with intent
startActivityForResult(EditIntent , 0);
Add override to you Activity1
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
if (data.hasExtra("myText")) {
//get your data with data.getExtras().getString("myText")
}
}
}
Change what button on your Activity2 does
{
Intent returnData= new Intent();
returnData.putExtra("myText", /*Text from your EditText*/);
if (getParent() == null) { //This part was taken from StackOverflow by Ilya Taranov
setResult(Activity.RESULT_OK, returnData);
} else {
getParent().setResult(Activity.RESULT_OK, returnData);
}
finish();
}
This should return text from EditText from Activity2 to Activity1. Code was not tested
create a variable for the textview to access it like
Textview txt = (Textview) finviewByid........;
implement the following code on button click listener
txt.setText(edittext.getText().toString());

Categories

Resources