Reputation: 537
I've been having some troubles to get an IF/ELSE statement working.
I have the following code:
File fileOnSD=Environment.getExternalStorageDirectory();
String storagePath = fileOnSD.getAbsolutePath();
Bitmap BckGrnd = BitmapFactory.decodeFile(storagePath + "/oranjelanbg.png");
ImageView BackGround = (ImageView)findViewById(R.id.imageView1);
BackGround.setImageBitmap(BckGrnd);
if (){
}else{
TextView text1 = (TextView) findViewById(R.id.textView1);
TextView text2 = (TextView) findViewById(R.id.textView2);
text1.setVisibility(View.VISIBLE);
text2.setVisibility(View.VISIBLE);
}
And I'm trying to achieve to following. My app downloads an image to the phone and uses this as a background. But the picture is not downloaded yet when u run the app for the first time, so there must be some text instead. The text is invisible by default and I want to make it visible when the image is still downloading and not placed yet.
What expression should I use in the IF statement to check if the image is loaded?
Upvotes: 0
Views: 389
Reputation: 109237
if (BckGrnd != null){
BackGround.setImageBitmap(BckGrnd);
}else{
TextView text1 = (TextView) findViewById(R.id.textView1);
TextView text2 = (TextView) findViewById(R.id.textView2);
text1.setVisibility(View.VISIBLE);
text2.setVisibility(View.VISIBLE);
}
Better Solution:
Use AyncTask for Downloading Image:
AsyncTask<Void, Void, Void> loadingTask = new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
TextView text1 = (TextView) findViewById(R.id.textView1);
TextView text2 = (TextView) findViewById(R.id.textView2);
text1.setVisibility(View.VISIBLE);
text2.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... params) {
// Download Image Here
}
@Override
protected void onPostExecute(Void result) {
BackGround.setImageBitmap(BckGrnd);
text1.setVisibility(View.GONE);
text2.setVisibility(View.GONE);
}
};
loadingTask.execute();
Upvotes: 3