Set ImageView's visibility (programmatically) before setContentView - Android

143 Views Asked by At

I don't like editing elements' XML attributes, so I prefer doing it programmatically. Is there any alternative to setting an ImageView's visibility to INVISIBLE and changing it back to VISIBLE in the Java code? Is there any way I can do both programmatically, i.e., set the ImageView's visibility to INVISIBLE and make it visible again using Java code? I guess it won't work the usual way as such code can be used only after setContentView(). For example, I might want to process the image and then display it rather than display it and then process it, etc. So, if I want to do something like that, which can be achieved only programmatically, and only after setContentView(), how should I go about it?

1

There are 1 best solutions below

2
Nader On

No, It's not possible. Instead, you can use the myLayout.xml file for default values. then, after setContentView() you can change that as you want using:

imageView.setVisibility(View.VISIBLE);

It is not possible because setContentView() binds the class with their layout and run it to give the default values, then you can override the default values using the code above.


One way to play around that is using LayoutInflater like the code below:

View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.myLayout, null,false);

But you will end up with the same result (with a lot more of code), and yet you need to bind the class with their layout and set their content view.