i am trying to change the image in a imageview, but when i set the variable in oncreate it gives me a yellow line making it so i cannot change my image.
in this app i am checking the Internet connection. basically, if the Internet connection is available i want to show a certain image; if not i want to show a image for that.
heres my code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imagev = (ImageView) findViewById(R.id.imageView1);
Button btnStatus = (Button) findViewById(R.id.btn_check);
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
/**
* Check Internet status button click event
* */
btnStatus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection",
"You have internet connection", true);
imagev.setImageDrawable(getResources().getDrawable(R.drawable.withinternet));
} else {
// Internet connection is not present
// Ask user to connect to Internet
showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection",
"You don't have internet connection.", false);
imagev.setImageDrawable(getResources().getDrawable(R.drawable.nointernet));
}
}
});
}
Declare the image view as a class variable i.e., imageV should be declared before onCreate(as a class variable) rather than inside it. It will solve your problem..