how to display image on imageview dynamically from drawalble folder - android

public class Serchresult extends Activity implements OnClickListener {
ImageView imageView1;
String Status;
String Reason;
TextView status;
TextView reason;
ImageView statusicon;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.act_serchresult);
Intent intent = getIntent();
Status = intent.getExtras().getString("Status");
Reason = intent.getExtras().getString("Reason");
imageView1 = (ImageView) findViewById(R.id.searchstatus_imgBack);
imageView1.setOnClickListener(this);
status = (TextView) findViewById(R.id.status);
reason = (TextView) findViewById(R.id.reason);
statusicon = (ImageView) findViewById(R.id.imageView1);
reason.setText(Reason.replace("null", ""));
if (reason.equals("ACCEPTED")) {
AQuery aq = new AQuery(getApplicationContext());
statusicon.setImageResource(R.drawable.accept_icon);
} else if (reason.equals("REJECTED")) {
AQuery aq = new AQuery(getApplicationContext());
statusicon.setImageResource(R.drawable.reject_icon);
}
else {
// reason.setCompoundDrawables(null, null, null, null);
statusicon.setImageResource(0);
}
status.setText(Status.replace("null", ""));
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.searchstatus_imgBack) {
finish();
}
}
}
here is my code i want display image from drawable folder accept and reject icon .i am getting ACCEPTED and REJECTED from intent on the basis of this response i want to display image in image view i have apply condition but image is not visible please help me where am doing wrong

Your code seems working. But still you can try following code to display image. Make sure your if-else conditions are working
Bitmap defBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.accept_icon);
statusicon.setImageBitmap(defBitmap);

Maybe the Bitmap is too big for the heap, try use BitmapFactory.Options to reduce the size.
BitmapFactory.Options op = new BitmapFactory.Options();
op.inSampleSize = 4;
statusicon.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.accept_icon, op));

Related

I need to intent ImageView resource file that is originally passed from another activity

codes for filling the imageview onClick event on the main activity
if (view.equals(findViewById(R.id.madrid_imageButton))) {
Bitmap madridImage = BitmapFactory.decodeResource(getResources(),R.drawable.madrid);
Intent buyProduct_Intent = new Intent(MainActivity.this,MainActivityBuy.class);
buyProduct_Intent.putExtra("Flavor","madrid");
buyProduct_Intent.putExtra("Description",madrid_Description);
buyProduct_Intent.putExtra("Bitmap",madridImage);
startActivity(buyProduct_Intent);
} else if (view.equals(findViewById(R.id.berlin_imageButton))) {
Bitmap berlinImage = BitmapFactory.decodeResource(getResources(),R.drawable.berlin);
Intent buyProduct_Intent = new Intent(MainActivity.this,MainActivityBuy.class);
buyProduct_Intent.putExtra("Flavor","berlin");
buyProduct_Intent.putExtra("Description",berlin_Description);
buyProduct_Intent.putExtra("Bitmap",berlinImage);
startActivity(buyProduct_Intent);
I have 10 of the above checks and here is the code for the new Activity that passed after the above condition
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buy_product); // Load buy product screen
TextView productNameTextView = (TextView) findViewById(R.id.productName_textView);
TextView productDesciptionTextView = (TextView) findViewById(R.id.productDescription_textView);
ImageView productImageView = (ImageView) findViewById(R.id.productImage_imageView);
Bundle extras = getIntent().getExtras();
if (extras != null) {
productNameTextView.setText(extras.getString("Flavor"));
productDesciptionTextView.setText((extras.getString("Description")));
Bitmap sharmImage = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
productImageView.setImageBitmap(sharmImage);
}
findViewById(R.id.size_radioButton_60ml).performClick();
findViewById(R.id.nic_radioButton_3mg).performClick();
findViewById(R.id.size_radioButton_30ml).performClick();
};
from this 2nd activity I need to intent a third activity that uses some of the views values. I successed to retreive all the needed values except for the imageView it displays as empty don't know why .....
here is the code that intents the third activity that contains the issue
public void onClickAddToCart(View view) {
TextView orderDetailsTextView1 = (TextView) findViewById(R.id.productName_textView);
TextView orderDetailsTextView2 = (TextView) findViewById(R.id.totalItems_textView);
TextView orderDetailsTextView3 = (TextView) findViewById(R.id.totalCost_textView);
String orderDetailsTextView = orderDetailsTextView1.getText().toString() + " " + orderDetailsTextView2.getText().toString() + " " + orderDetailsTextView3.getText().toString();
ImageView productImageView = (ImageView) findViewById(R.id.productImage_imageView);
ImageView newImageView = new ImageView(this);
newImageView.setImageDrawable(productImageView.getDrawable());
int drawableId = getResources().getIdentifier(productImageView.getDrawable().toString(), "drawable", getPackageName());
Bitmap productImage = BitmapFactory.decodeResource(getResources(),drawableId);
Intent buyProduct_Intent = new Intent(MainActivityBuy.this, Cart.class);
buyProduct_Intent.putExtra("OrderDetails", orderDetailsTextView);
buyProduct_Intent.putExtra("Bitmap", newImageView.getDrawable().toString());
startActivity(buyProduct_Intent);
}
below are the codes of the onCreate of the third activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_cart); // Load the cart XML Screen
TextView orderDetailsTextView = (TextView) findViewById(R.id.orderDetailsTextView);
ImageView productImageView = (ImageView) findViewById(R.id.productImage_imageView);
Bundle extras = getIntent().getExtras();
if (extras != null) {
orderDetailsTextView.setText(extras.getString("OrderDetails"));
Bitmap productImage = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
productImageView.setImageBitmap(productImage);
}
}
#Override
public void onBackPressed() {
// startActivity(new Intent(MainActivityBuy.this, MainActivity.class));
finish();
}
}
By the way there is no errors or crashes .. only blank imageview at the third activity but the 1st two activities are functioning correctly and the XMLs are fine as well ..
based on the brainstorming done during the above questions and comments i came up with the below idea and it actually solved the issue :)
public void onClickAddToCart(View view) {
// Here is i concatenated three textViews values - calculations into one single string
// to pass it to the Cart activity
TextView orderDetailsTextView1 = (TextView) findViewById(R.id.productName_textView);
TextView orderDetailsTextView2 = (TextView) findViewById(R.id.totalItems_textView);
TextView orderDetailsTextView3 = (TextView) findViewById(R.id.totalCost_textView);
String orderDetailsTextView = orderDetailsTextView1.getText().toString();
orderDetailsTextView += " " + orderDetailsTextView2.getText().toString();
orderDetailsTextView += " : " + orderDetailsTextView3.getText().toString();
// in the below declaration i saved the originally intent image source to a variable
// and her i recall it to re-intent it again
Bundle extras = getIntent().getExtras();
Bitmap productImage = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
// Here is the passing method - transfering below data from this activity to the Cart activity
Intent buyProduct_Intent = new Intent(MainActivityBuy.this, Cart.class);
buyProduct_Intent.putExtra("OrderDetails", orderDetailsTextView);
buyProduct_Intent.putExtra("Bitmap", productImage);
startActivity(buyProduct_Intent);
}

Android: Using ImageView onclick to change image back and forth

Code that is not working
public void fade(View v){
ImageView pic1 = (ImageView) findViewById(R.id.pic1);
ImageView pic2 = (ImageView) findViewById(R.id.pic2);
pic1.animate().alpha(0f).setDuration(2000);
pic2.animate().alpha(1f).setDuration(2000);
}
Not quite sure what you are asking for, but if you want to change the image back and forth onClick, you could try modifying the code I wrote below.
// set a boolean value
private boolean firstImageShown = true;
public void clickFunction(View view) {
ImageView firstImage = (ImageView) findViewById(R.id.firstImage);
if ((firstImage != null) && (firstImageShown)) {
firstImage.setImageResource(R.drawable.sample_image2);
firstImageShown = false;
} else {
if (firstImage != null) firstImage.setImageResource(R.drawable.sample_image1);
firstImageShown = true;
}
}
//Suppose you have two images rose_1 and rose_2 and you want to switch back
//and forth between these on a button press.
//Set a static variable
private static int switcherPressedCount = 2;
//Map the onClick attribute of the button to a function say "switcherPressed".
//"imageView1" is the id attribute of the image which first gets loaded in the
//app, in this example, it is the attribute of rose_1.
public void switcherPressed(View view){
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
if(switcherPressedCount%2 == 0) {
imageView.setImageResource(R.drawable.rose_2);
switcherPressedCount = 3; //to avoid reaching maximum int limit.
}
else {
imageView.setImageResource(R.drawable.rose_1);
switcherPressedCount = 2; //to avoid reaching maximum int limit.
}
}

How to get the id of display image in flipper?

BELOW IS MY CODE. This code is working, but I want to get the value of the display image in array to test, if the display country is correct. Please help me . I'm stuck in this activity. Thanks for all help .
public class MainActivity extends Activity implements OnClickListener {
private boolean blocked = false;
private Handler handler = new Handler();
ViewFlipper flippy;
Button show;
TextView view;
int flags[] = { R.drawable.afghan, R.drawable.albania, R.drawable.algeria };
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.flipper);
flippy = (ViewFlipper) findViewById(R.id.viewFlipper1);
show = (Button) findViewById(R.id.button1);
view = (TextView) findViewById(R.id.textView1);
for (int i = 0; i < flags.length; i++) {
setflipperimage(flags[i]);
}
}
private void setflipperimage(int i) {
// TODO Auto-generated method stub
Log.i("Set Filpper Called", i + "");
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(i);
flippy.addView(image);
}
do not call this in loop,use this on click or on touch:
i+=1;
flippy.setDisplayedChild(flags[0]);
This will get used to get the current child id
viewFlipper.getDisplayedChild();

Android how to call another Image after 3 times click on same Image?

I am developing application on Images.
In that if I click on same image the image will replace by another one that already into my Drawable
Right now onclick I am able to display only Toast Message; but I want to replace Image. I don't know How to do?
Any Help and suggestion appreciable.
you can take it images name as : img1 ,img2 ,img3.
ImageView imgV = (ImageView) layout.findViewById(R.id.img1);
imgV.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (v.isClickable()) {
i++;
String ii = new Integer(i).toString();
Log.i("Inside", ii);
if (ii.equals("3")) {
Toast.makeText(getApplicationContext(), "Call another Image ",Toast.LENGTH_SHORT).show();}
ImageView imgV = (ImageView) layout.findViewById(R.id.img1);
imgV.setClickable(true);
int counter = 0;
imgV.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
i++;
if(i == 3){
imgV.setImageResource(R.drawable.img2);
//Or you may want to use
//imgV.setBackgroundResource(R.drawable.img2);
i = 0;
}
}

Problem on loading images from url

i want to load an image from a url.i have 10 images on my server and i m calling one in every class .it works but there are some pictures that are not opening...is there any wrong on my code?i have checked the url and its ok!thanks
public class modern_clock extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sites);
ImageView im = (ImageView) findViewById(R.id.image);
Drawable drawable = LoadImageFromWeb("http://kostas-menu.gr/chania/roloi.jpg");
im.setImageDrawable(drawable);
TextView title1 = (TextView) findViewById(R.id.text_title);
title1.setText("The Clock\n" );
TextView info = (TextView) findViewById(R.id.info);
info.setText(".................\n" );
}
private Drawable LoadImageFromWeb(String url) {
// TODO Auto-generated method stub
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
EDIT:
#George:
i have added in my package the LoaderImageView,java class.i also changed my xml file from the simple imageview to
<com.example.android.LoaderImageView
android:layout_marginTop="10px"
android:id="#+id/loaderImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
image="http://developer.android.com/images/dialog_buttons.png"
/>
and in every class of my project i have added that:
final LoaderImageView image = new LoaderImageView(this, "http://kostas-menu.gr/chania/santrivani.jpg");
image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
but unfortunately with that way i m always getting the image from the url in my xml file.i have the same xml for all my classes,so how could i set the image from the java file?i tried to erase the line image:"http...." from the manifest,but this is not returning any image at all!!thanks
Ideally you should fetch the images in a seperate thread different from the UI thread.
Lazy load of images in ListView
This might help too.
use this resources. I am sure this will help you
http://www.anddev.org/novice-tutorials-f8/imageview-with-loading-spinner-t49439.html
do you use this code? Is this something that we should?
public class ImageExampleXML extends Activity {
private final String images[] = {"http://developer.android.com/images/dialog_custom.png", "http://developer.android.com/images/dialog_progress_bar.png"};
private int i = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button loadImage = (Button) findViewById(R.id.button);
final LoaderImageView image = (LoaderImageView) findViewById(R.id.loaderImageView);
loadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
image.setImageDrawable(getAnImageUrl());
}
});
}
private String getAnImageUrl() {
i++;
if(i >= images.length){
i = 0;
}
return images[i];
}
}

Categories

Resources