Handling screen orientation changes - java

My app functions like this: an imageView that can be visible in portrait or landscape, and the user can swipe between images to change the image in the imageView. The issue is that if you swipe to select a different image and then rotate the device to landscape it shows the original image the user selected, not the current image that was being displayed in the imageView. I am wondering how I can get the id of the current image before it is rotated, and then set it for when it is in landscape. Would it be in the onPause method, or is there a onConfigurationChanged method I could use for this?

See here: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
According to the documentation, the activity is destroyed when the orientation changes. In order to save additional state information in such an event, you can override the onSaveInstanceState() method and add key-value pairs to the bundle object, like so:
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putInt("CurrentImageId", [current image id here]);
super.onSaveInstanceState(Bundle savedInstanceState);
}
Then, when the application is restored after the orientation change, you can retrieve the current image's id by overriding either the onCreate(Bundle savedInstanceState) method or the onRestoreInstanceState(Bundle savedInstanceState) method:
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int currentImageId = savedInstanceState.getInt("CurrentImageId");
//Set the current image here, after retrieving the id
}
Of course, I don't know exactly how you're identifying your images, so I can't tell you exactly how to set/retrieve the current image's id.

Try to add android:configChanges="orientation" below <activity> in your AndroidManifest.xml

Related

Loading an image inside a bottom navigation bar item icon using an URL

I'm trying to load a profile image using the firebase storage url inside an item's icon of a bottom navigation bar, here's my code:
Glide.with(getApplicationContext()).asBitmap().load(profilePicUrl)
.into(new CustomTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
Drawable profileImage = new BitmapDrawable(getResources(), resource);
bottomNav.getMenu().getItem(4).setIcon(profileImage);
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
The profilePicUrl does work, I already use it to for another image view. However when I run the app, the dimensions of the icon changes corresponding to the picture that I'm trying to load but there's no image inside, here's how it looks.
Menus do not support coloured images
You can not use a coloured image in these places and a bit more also:
In bottom navigation
In navigation drawer
In popup menu
etc...
Basically which ever view uses a menu file for its resource, cannot have a coloured image. That is why you don't see it. To test it yourself, take a colourful image in your drawable and set it as an icon for the bottom navigation. You notice that it comes the same way. This proves it
Edits
This post gives some info on how to do it
You can do it this way:
mBottomNav.setItemIconTintList(null);

Renaming Toolbar based on the bottom navigator

How do I change Toolbar Titles in different bottom navigation activities, I currently have one displayed in all which is one my manifest
android:label="#string/home"
but since I have four activities on my bottom navigator how can I rename them all on the activities, I tried adding a toolbar it added below the original, home. Tried renaming manifest particular activities doesn't work. Thanks in advance.
I tried setting titles on the activities like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collections);
setTitle(R.string.collections);
}
I want to have something like this, Image One Image two
You can use setTitle in your activity.

Change Title/Label of an Activity in Android 5 recents screen

With Android 5, Google supports multiple activities in the recents screen for a single application.
I managed to implement multiple activities in the recents screen with this official doc.
How can I change the title of the activity? Switching to recents screen always shows my app name as title.
Activity.setTitle("title");
changes the title on the toolbar, but I want it to change in the recents screen, just like Google does in Google Chrome App.
I need to change the title programatically.
In your activity you can use new method:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_layout);
setTaskDescription(new ActivityManager.TaskDescription("Activity 2"));
}
setTaskDescription sets information about activity in recents task list. In this example only title.

How to know the status of the pages in a ViewPager

I'm implementing a image gallery with a ViewPager.
Each page load a image from Internet and put it in a ImageView.
The gallery also have a share button (external to the ViewPager).
I want to disable the button when the image is loading and re-enable it when the image is visible.
How can I know the status of the current page (if the image is beeing displayed or not) from the Activity containing the ViewPager?
Thanks
You can override setPrimaryItem function
public void setPrimaryItem(android.view.ViewGroup container,
int position, Object object) {
super.setPrimaryItem(container, position, object);
//check if image is loaded here
};

How to set imageButtons to VISIBLE at onResume() method?

I have an activity with some imageButtons in it. After I click on them i use setVisible(View.INVISIBLE); to have them gone. Now, when a user enter correct answer, a popup screen pops up with some info and OK button. I need to set all my imageButtons to be invisible when that popup window close. I tried to make some method:
private void removeImages(){
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
}
and then call it onResume:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
removeImages();
}
But it does not work, it removes all my imageButtons as soon as I start that activity. How to do that after my popup windows closes, after I press OK button on that popup?
As per the Activity Lifecycle, onResume() is called before the Actviivty is in the foreground. You have a couple different options. You can use startActviityForResult() when you click an ImageButtonand check that value in onActivityResult() to set the Views how you wish. Or you could save a value in SharedPreferences to tell the Activity which Views to set invisible/visible in onResume()

Categories

Resources