1

I want to hide Some ImageButton while making image description visible. However else if statement is not working as intended.

OnCreate:

    ImageView thumbnail;
    ImageButton addImage ,removeImage;
    EditText description;

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_write);

            Log.d(TAG, "onCreate: Started");

            thumbnail = findViewById(R.id.thumbnail);
            addImage = findViewById(R.id.addImage);
            removeImage = findViewById(R.id.removeImage);
            description = findViewById(R.id.description);


            setupToolbar();
            setupBodyEditor();
            addThumbnail();
        }

addThumnail:

  private void addThumbnail() {


    if ( thumbnail.getDrawable()== null)
    {
        addImage.setVisibility(View.VISIBLE);
        removeImage.setVisibility(View.GONE);
        description.setVisibility(View.GONE);
    }
    else
    {
        addImage.setVisibility(View.GONE);
        removeImage.setVisibility(View.VISIBLE);
        description.setVisibility(View.VISIBLE);
    }

    thumbnail.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            removeImage.setVisibility(View.VISIBLE);
            addImage.setVisibility(View.GONE);
        }
    });

    addImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"),5);
        }
    });

    removeImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            thumbnail.setImageDrawable(null);
        }
    });

}

onActivityResult:

 if (requestCode== 5  && resultCode == Activity.RESULT_OK && data != null && data.getData() != null)
        {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                thumbnail.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(mContext, "Something went wrong", Toast.LENGTH_LONG).show();
            }
        }
 else if (resultCode == RESULT_CANCELED){
            Toast.makeText(mContext, "No Image Selected",Toast.LENGTH_LONG).show();
        }

Tried naming variables differently but nothing worked.

Even if Thumbnail ImageView contains an image, it still shows addImage button and description is also not made visible.

  • I think you don't need an `else if` with the check if it is `!= null`, you already know it is not null and, thus, you can just use `else { ... }`. But that might not be the reason for your error. – deHaar Aug 16 '19 at 14:38
  • I tried that too but gives same result. – Tajan Thind Aug 16 '19 at 14:39
  • Thought so... Then the reason is one or more of the methods triggered not working as desired. Is `Thumbnail` a class? Does `Thumbnail` have a `static` method `getDrawable()`? If yes, you might have trouble with capitalized variable names. – deHaar Aug 16 '19 at 14:41
  • Thumbnail is an imageview. – Tajan Thind Aug 16 '19 at 14:47
  • 3
    Unrelated: learn about java naming conventions. Variable names go camelCase in Java. Your variable names look like ClassNames, and that is really confusing. – GhostCat Aug 16 '19 at 14:49
  • I changed all variable names to lower case but it is still behaving same. – Tajan Thind Aug 16 '19 at 14:55
  • @TajanThind changing variable names won't have an effect on the program, it just makes it easier to read. –  Aug 16 '19 at 14:59

2 Answers2

0

you need to use setImageDrawable in your code. ImageView is always null.

setImageDrawable(getResources().getDrawable(R.drawable.icon));
  • https://stackoverflow.com/questions/12001793/android-imageview-setimagebitmap-vs-setimagedrawable – second Aug 16 '19 at 16:50
0

As far as I can tell you do not change the buttons visibilty after the image has been added (immediately). Only when the onCreate method is invoked the changes will be applied.

According to the Activity Lifecylce this is only done if your Activity is launched (initially or after the process has been killed).

To fix your issue you will have to trigger the change of the visibility for the addImage, removeImage & description elements together with the setting of your thumbnail image (from inside your onActivityResult method).

second
  • 4,069
  • 2
  • 9
  • 24