Display imageview onclick and post without imageview if empty

I am writing code for a blog app which would post just text or both text and a image below it. I have defined an editText and a imageView in layout. I want that, unless I click on gallery button, the imageview doesn’t appear.

I tried that with a intent, but adding activities is making app too heavy.

 private ImageButton newPostImgbtn;
 private ImageView newPostImage;
 private EditText newPostDesc;
 private Button newPostBtn;

 protected void onCreate(Bundle savedInstanceState) {

    newPostImage = findViewById(R.id.imageView);
    newPostImgbtn = findViewById(R.id.imageButton2);
    newPostDesc = findViewById(R.id.editText);
    newPostBtn = findViewById(R.id.button2);

I created new activity imageactivity for just adding image by putting an onclick listener on button for gallery newimagebtn, but I don’t want to do this in another activity.

public void post(View v) { Intent intent = new Intent(postActivity.this, 
imageActivity.class); startActivity(intent);}

Also, if the user clicks on the gallery button, but doesn’t choose any image, then when post button is clicked, only text appears without a empty imageview area or error because the field is empty. Please help me.

Leave a Reply

2 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

In onCreate Method add newPostImgbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getImageFromAlbum(); } }); in your activity class private void getImageFromAlbum(){ try{ Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); }catch(Exception exp){ Log.i("Error",exp.toString()); } } Add this method to your Activity Class Override protected void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); if (resultCode == RESULT_OK) { try { final Uri imageUri = data.getData(); final InputStream imageStream = getContentResolver().openInputStream(imageUri); final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); newPostImage .setImageBitmap(selectedImage); } catch (FileNotFoundException e) { e.printStackTrace(); Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show(); } }else { //image not selected so do… Read more »


First, you have to take permission from User and open gallery intent. When you found the image url u need show it with image loader library like glide. The last thing you need to do check image url is empty or not when clicked button. Let me know if you need further assistance.