When has the Activity finished drawing itself? - android

This maybe somewhat of a strange question but I just can`t figure it out.
Basically I have a class that extends View and overrides the onDraw method so it draws a bitmap what it is called. What I want to do is when by choosing a picture from a gallery, send it through and intent and draw that on the View.
The problem is that I can`t get the Activity to draw the bitmap when it is started. I try setting in in onCreate, onStart... nothing works.
It works if I set the bitmap on a button press and calling invalitate(), so I suspect that I try to draw before the Activity hasn`t finished drawing itself.
Any suggestions?

Well, if you instantiate the class that extends the View in your onCreate, that should work.
Take a look at this example:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
customView = new CustomView(this);
setContentView(customView);
customView.requestFocus();
}

Override method onWindowFocusChanged() in Activity.
This is where you can be sure that views have been drawn. You can start obtaining the dimension/ position of the views in this method.
For eg. customView.getLeft(), etc.

Related

Grid View rotation handling

I have slight problem. I have gridView in main activity, and on device rotation the list will always start from the top. I fonud several solutions but non of them work for gridView. How can I maintain same gridView position on device rotation using onSavedInstanceState?
Thanks in advance. :)
You first need to understand the facts because of which this is happening. This is happening because whenever your device orientation changes your onDestroy() and onCreate() lifecycle methods are called. To survive your activity state using onSaveInstanceState, you need to save your grid view scroll position when onDestroy() is called and then retrieve it in your onCreate() method. You can do this using Bundle. The code will look something like this, onSaveInstanceState() will be called by android os before your activity get destroyed \n
#Override
onSaveInstanceState(Bundle bundle){
super.onSaveInstanceState(bundle);
bundle.putInt("SCROLL_VIEW_POSITION",scroll_position);
}
//And in your on Create
onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
int scrollPosition = saveInstanceState.get("SCROLL_VIEW_POSITION",default_value);
}
Once you get your same scrollPosition before oreientation change you can use it to set scroll position of your grid view.

How to save the last view set by viewSwitcher and restore it when relaunching activity?

I have an Activity that uses a ViewSwitcher to switch between two views. The onCreate() method uses setContentview() to set the view to one of the two views.
When leaving the Activity and going back to it again later, I want the view that was shown last to be the one that is picked in the viewSwitcher (not the one in SetContentview every single time).
How would I accomplish this?
I tried something like this, but the app crashes when I try to load the Activity:
private View lastState = findViewById(R.id.activity_login_page);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(lastState);

Changing an ImageButton image programmatically

I have successfully set an image to an ImageButton using this code:
mGetClickTime.setImageResource(randomImageId);
But I'm trying to problematically change the image by calling a method which contains:
mGetClickTime.setImageResource(randomImageId);
(In which the randomImageId vraiable is different)
However the programmatic change is not working.
Do I need to remove the current image before setting a new one? If so, how do I do that?
EDIT
The problem seems to be that the above command stops working after another activity has been called and completed. After that happens the setImageResource just doesn't work.
I'm not sure why this could be. I've tried commenting out everything from the second activity apart from this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
but the problem still occurs. Why?
Give this a try:
mGetClickTime.setBackgroundResource(randomImageId);
This will work in an onClick method. That might be your issue, but not 100% sure until I see the context of the code.

Force Main view to recreate after preferences

So I have my main activity that has some views and I have an activity that has the preferences.
I want to, when the activity of the preferences is destroyed, to recreate my main activity, because some views depend on the preferences.
I don't see a way to do this.
Thanks.
Check what's changed in onResume() of your main Activity. Then, based on whatever your logic is, set the contentView to the appropriate Views or manipulate whatever Views you need to.
for me I always make all work in separated method and call it in onCreate()..
i.e:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//here call method contains views initializing, logic, etc..
initializingPro();
}
private void initializingPro(){
//....
webview=(WebView)findById(R.id.webb);
//....
}
now in you case you want to recreate those components, just call this method again initializingPro() ,, no need to destroy activity..
good luck,

setContentView gives an exception

In my android app I set
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
Then my touch screen event doesnt’ work any more.
Further Explaining,
I have a button and onClick it changes the contentView by setContentView(R.layout.choose_player);.
It works fine. But if you take the focus to the button by the trackball(making it yellow) and tap on it, it gives the exception.
java.lang.IllegalArgumentException: parameter must be a descendant of this view
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main1);
}
public void onClick(View v) {
setContentView(R.layout.main2);
}
i cant understand properly first. I think you may be using setContentView method multiple times. one is to set the layout on the view. another one inside the click event. right? that not works. use that method once to set the layout. on click event you have to do your tasks on the views inside the layout. hope it helps. please post some code snippets for the perfect answers.
you use setContentView only once in your 'onCreate' method, in general for the main window (one Activity = 1 view)
in your case you should probably change your Background..

Categories

Resources