Question

I have this imageView that I want to be Invisible at first... Then when I click a button (calculateButton), the imageView will be Visible.

Here's my ImageView:

<ImageView
     android:id="@+id/imageView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignRight="@+id/resultLabel"
     android:layout_below="@+id/resultLabel"
     android:src="@drawable/image" />

And here's my code for the calculateButton:

     public void calculateClickHandler(View view) {


    if (view.getId() == R.id.calculateButton) {

    EditText weightText = (EditText) findViewById(R.id.weightText);
    EditText heightText = (EditText)findViewById(R.id.heightText);
    TextView resultText = (TextView)findViewById(R.id.resultLabel);


    int weight = (int) Float.parseFloat(weightText.getText().toString());
    int height = (int) Float.parseFloat(heightText.getText().toString());
            int bmiValue = calculateBMI(weight, height);



 String bmiInterpretation = interpretBMI(bmiValue);

 resultText.setText("Your BMI is:" + " " + bmiValue  + " " +  bmiInterpretation); }

    }

  private int calculateBMI (int weight, int height) {

    return (int)  weight * 703 / (height * height) ;

        }
   private String interpretBMI(int bmiValue) {

            if (bmiValue <= 16.0 && bmiValue <= 18.5) {
                return "Underweight";   
            } else if (bmiValue > 18.5 && bmiValue <= 25 ){
                    return "Normal (Healthy)";
            } else if (bmiValue > 25 && bmiValue <= 30  ) {
                return "Overweight";
            } else {
                return "Obese"; }
            }

               }
Was it helpful?

Solution

Make image view invisible first and inside onclick make it visible.

So put this line in image-view of XML

android:visibility="invisible"

so your full code look something like this.

<ImageView
     android:id="@+id/imageView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignRight="@+id/resultLabel"
     android:layout_below="@+id/resultLabel"
     android:visibility="invisible"
     android:src="@drawable/image" />

and then make it visible in button click event.

    imageview.setVisibility(View.visible);   

Remember here imageview is your object on ImageView. so it looks like

 ImageView imageview =(ImageView) findViewById(R.id.imageView1);
 imageview.setVisibility(View.visible);

So put it like this.

public void calculateClickHandler(View view) {


    if (view.getId() == R.id.calculateButton) {

    EditText weightText = (EditText) findViewById(R.id.weightText);
    EditText heightText = (EditText)findViewById(R.id.heightText);
    TextView resultText = (TextView)findViewById(R.id.resultLabel);

ImageView imageview =(ImageView) findViewById(R.id.imageView1);
         imageview.setVisibility(View.visible);

         .......
         .......


if (bmiValue <= 16.0 && bmiValue <= 18.5){

imageview1.setVisibility(View.visible);
imageview2.setVisibility(View.invisible);
imageview3.setVisibility(View.invisible);
imageview4.setVisibility(View.invisible);

}else if (your second condition){
imageview1.setVisibility(View.invisible);
imageview2.setVisibility(View.visible);
imageview3.setVisibility(View.invisible);
imageview4.setVisibility(View.invisible);

}else if (your third condition){
imageview1.setVisibility(View.invisible);
imageview2.setVisibility(View.invisible);
imageview3.setVisibility(View.visible);
imageview4.setVisibility(View.invisible);

}else
{
imageview1.setVisibility(View.invisible);
imageview2.setVisibility(View.invisible);
imageview3.setVisibility(View.invisible);
imageview4.setVisibility(View.visible);
}

Here just replace your image-view with your ID and try. Hope this will help you.

OTHER TIPS

You can set visibility

android:visibility="invisible"

Then on button click

public void calculateClickHandler(View view) {

 ImageView iv =(ImageView) findViewById(R.id.imageView1);
 iv.setVisibility(View.VISIBLE);
}

http://developer.android.com/reference/android/view/View.html#attr_android:visibility

Avoid initializing imageview every time on button click

ImageView iv;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm_manager);
    iv =(ImageView) findViewById(R.id.imageView1);
}

OnCreate() set imageview INVISIBLE like imageview.setVisibility(View.INVISIBLE);

and button onClick() VISIBLE it like imageview.setVisibility(View.VISIBLE);

try it out

visibility of imageview is invisible ......

<ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignRight="@+id/resultLabel"
 android:layout_below="@+id/resultLabel"
 android:src="@drawable/image"
 android:visibility="invisible" />

and in your code..,at buton click put this line

imageview.setVisibility(View.visible);

You should probably define the image as invisible in your layout XML... android:visibility="invisible"

You can modify the visibility of a view with view.setVisibility(x);, where x is View.INVISIBLE, View.VISIBLE, or View.GONE. write this code in onCreate() method.

Button calculateButton = (Button) findViewById(R.id.button1);
calculateButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        ImageView imageView =(ImageView) findViewById(R.id.imageView1);
        imageView.setVisibility(View.VISIBLE);

    }
});

firstly set visibilty in xml gone

 android:visibility="gone"

now from java code set visibility visible on onclick..

 imageView.setVisibility(View.VISIBLE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top