Sequence of Activities - Interactive book - java

I'm trying to make an interactive book, where every page contains 2 images and 2 buttons.
One image is animated on user touch (that's working fine)
The buttons are "Back" and "Next", that's working fine too, but my problem is I have a sequence of 10 classes name one after another:
Class01
Class02
Class03...
(Every class has a different animation)
So in class01, next button is always calling the next activity
public void Next(View v) {
Intent next = new Intent(this, next_page);
startActivity(next);
finish();
and closing the current one, which is Force Closing the app quite regularly.
I'm new to android and I think my logic is pretty useless.
How can I implement this sequence?

you can make it using View Flipper to make it more interactive and add animations.

In your case, finish(); isn't necessary, remove it. And you will have:
Next button
public void Next(View v) {
Intent next = new Intent(this, next_page);
startActivity(next);
}
Back button
public void Back(View v) {
this.finish();
}
Don't forget, users can press "Back Button" on their device. This may interest you:
public void onBackPressed() {
// do something if the button back is pressed.
super.onBackPressed();
}
I think #MohammedSaleem and #mvnpavan are right when they say you should to use ViewFlipper, more adapted in your case. You will declare in your Manifest just one Activity for all your layouts. See this answer (https://stackoverflow.com/a/3545954/2668136) which says:
ViewFlipper can be used if you want to periodically change the views.
Say like an automated flipping book of some sort.
To make a ViewFlipper, you must to read this tutorial:
Creating Image Slideshow Using ViewFlipper
And to see a simple example in this SO answer:
How To Use ViewFlipper With Three Layouts?
Finally, the Google Reference Documentation:
Public Class ViewFlipper
Hope this help.

Related

Is there a way to include a Multi-transition within ViewPager2?

Am using an array list that holds 5 images which change after an interval of 5seconds on the viewpager2 using handler and runnable,the images change perfectly well from the 1st image to the 5th image and restarts the process again using slide transition, the problem i have is changing the animation as it restarts running through the item images it looks terrible because it has to slide back to the first time as it transits more faster through the items backwards , i want to create it in a disappear(last item) and reappear (1st item) fast transition just like when popup ads change images and restart the slide transition again to the last item in a fragment, below is the piece of code.
viewpager3.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
sliderHandler.removeCallbacks(sliderRunnable);
sliderHandler.postDelayed(sliderRunnable, 5000);
}
});
sliderRunnable =new Runnable() {
#Override
public void run() {
if(viewpager3.getCurrentItem()<image2List.size()-1)
{ viewpager3.setCurrentItem(viewpager3.getCurrentItem()+1);
}else {
viewpager3.setPageTransformer();
viewpager3.setCurrentItem(0);
}
}
};
Not sure why you are using Viewpager2 for this and have to automate the changing of views yourself and you would really also need to implement an infinite View pager to get a more seamless transition (though at a small risk if running out of pages)
You could try setting viewpager3.setCurrentItem(0,false); to jump to the start without any animation.
But much better would be to use the more appropriate AdapterViewFlipper class.
This has automatic changing of the item based on a timer built in to it (no runnables needed).
It recyclers and is dynamic like viewpager2, the only downside is that page(view) is not as nicely encapsulated in a Fragment but as you are just showing images then that should not be a problem.
Example of how to use https://abhiandroid.com/ui/adapterviewflipper
You can add in and out animations to slide in and out to get a similar look to the Viewpager2 animations
If you look at another examples of the similar class for more static content it shows the slide in/out animations https://abhiandroid.com/ui/viewflipper

Button in one activity to do the same as button in another activity

I am new to Android. In my application, in ActivityOne I want to allow the user to take a picture using a button. The picture is then stored in the gallery and displayed in an ImageView in ActivityTwo.
My question is: How can I add a button to ActivityTwo that allows the user to retake the picture if they don't like it when displayed in the ImageView. Basically, the button will do the same as the one in ActivityTwo. Do I need to write the same code for the button?
I have tried to create a separate that implements View.OnClickListener, add the functionality in the onClick method, and in each activity create an instance of that class attached to the button:
onClick(View v) {
int id = v.getId();
if (id == R.id.myButton) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
myfile = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
v.startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
However, this causes errors as the class is not an activity.
Any help is appreciated.
You can write the same logic for this second button, but as Naveen mentioned it would be easier to put the button on the first activity, leading to the camera. When the user takes a picture just add an imageview to the layout above the button using linearlayout on the first activity. Then the picture is displayed and the user has the option to retake it.
The best option is to let the user take the picture in the activity where it is needed. But if there is a need for you to use it in multiple places, you should extract the code triggered by the button in the first activity(The code that takes the picture) into a separate class. then you can call it from anywhere when needed.
#Subzero-273k Follow this.
In your activity make two different layouts. One, which shows your first button, remember to hide the other layout by setting the visibility.
Once user clicked the button1 which calls someOnClick(), hide first layout and enable another layout, which shows you the imageView and button2, onclick of button2 still calls your old function someOnClick().
How about this? As you dint mention what else you are doing in two different activities, I assume you just show activity one for single button.

Prevent ImageButton from creating a new activity

I have three activities, each with different layouts..
All three layouts have one thing in common (three ImageButtons) to switch between the activities. In the onClick of the Imagebuttons, I use an intent to open the activity.
Here is the problem:
The ActivityA plays a song, now when I click play, the song starts, if I decide to switch to activityB, it opens. From Activity B, if I wanted to go back to ActivityA by clicking the corresponding ImageButton, it creates a new instance of activityA with its default layout (No song is played) but I can still hear the song.
Meaning that a new instance of ActivityA was created, I dont want it to be like this, I want it that when I click the corresponding ImageButton, it goes to the previous activity in the same state as I left it before I switched to ActivityB.
I thought this might solve the problem, but I dont think it would considering I have three Activities (might have used it if it was just two activities)
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
I would suggest using a tab navigation or navigation spinner instead of creating new activities.
But if you really want to use separate activities, you could try putting this in the activity tags of your manifest file:
android:launchMode="singleTop"
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
I got it to work, using
android:launchMode="singleTask"
in the android manifest.
android:launchMode="singleTop"
didnt work as Mark Bukeima suggested above, but he did push me in the right direction to know I was to use the android:launchMode="singleTask".
It might be cause you start your activity using starActivity() instead of startActivityForResult() to switch from ActivityA to ActivityB.
Here's how you should do it :
/* In ActivityA */
public void onClick(View view) {
Intent intentToB = new Intent(this, ActivityB.class);
startActivityForResult(intentToB, 0); // Will start B whithout killing A
}

Switching between activities automatically

I'm new to Android programming, so I'm probably missing something trivial here.
The goal is to create a prototype of a digital signage app. Right now I've created three activities; MainActivity has a method which switches to the second activity after a certain period of time. The same method is then called from the second activity to third, and from third back to main.
There are two problems though: first, is it ok to create new Intent every time the app switches between the activities? As I mentioned, I started learning Android recently, so please don't be mad if this is a super dumb question.
Second: the app launches itself after I've hit home button in my emulator even though I'm calling finish(); after the startActivity(intent);
Here is the method in the MainActivity:
public void switchActivities(final Class<?> classObject) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), classObject);
startActivity(intent);
finish();
}
}, 1000);
}
The activities Second and Third extend MainActivity and call the switchActivities method:
switchActivities(Third.class); (from Second to Third).
Thanks in advance!
UPDATED:
I added public boolean isRunning = true; to my MainActivity and
if(isRunning) startActivity(intent); to the switchActivities method;
I also added a method
#Override
protected void onPause(){
super.onPause();
isRunning = false;
}
as advised here.
Although finish(); should have cleared the method stack, the app didn't close after pressing Home button but went to previous activity instead, so I added this line to every activity in the AndroidManifest file:
android:noHistory="true"
as advised here.
Sorry for not upvoting helpful answers, I don't have enough reputation yet.
Yeah, it is the proper way of switching activities by creating new intents. I am little confused about your second questions. if you are saying that as you are using postDelayed method which gets triggered after some interval, is being called even you have finished your activity or pressed home button, you can handle this by creating a boolean variable isRunning in your activity which gets false when onPause or onDestroy gets called. Then you in your postdelayed method, you can check the flag and then proceed as required.
The Intent used in navigation between the Activities , so it's cool to using it , no problem
The second problem because the Activity object still in your memory after you hit Home button , then the Handler will continue its work and start the activity after the certain period you determined in your code
For your first question: it's not a problem that you're creating a new Intent every time
The second one is a bit trickier. When you press the home button the current activity stays in memory, so it can start the new one even though you think it's not running. The easiest way to handle this to check weather the activity is finishing, and if so, you don't show the next one.
if (!isFinishing()) startActivity(intent);
This if statement prevents your activity to start a new one when it's in the background.

Best way to create a lot of new layouts

I'm currently making a local news app and the main layout has 10 image buttons (more to be added in the future) and I was wondering what would be the best way to get each of these to open a separate layout with an individual text view without making 10 seperate classes and maybe even without making 10 separate layouts. Right now my MainActivty class handles the first button from the layout main_activity which opens a new layout named issue.XML.
Thanks in advanced.
You can create a layout called "newsLayout" that has just one text view and sets it from intent and make the onClick listener of the buttons to start a new activity and passing the desired text (the news) to the intent
Here is some code to help:
newsLayout.java
TextView text = findViewById(R.id.text);
text.setText(getIntent().getCharArrayExtra("TEXT"));
mainLayout.java
public void openNews(View view)
{
Intent intent = new Intent(this, newsLayout.class);
intent.putExtra("TEXT", newsText);
startActivity(intent);
}
Where 'newsText' is the text you want to be shown
I may have wrote some lines wrong because im answering from mobile and i dont remember the exact words, if there's anything you dont understand tell me :)

Categories