Camera activity return make button behaviour lost - java

I'm a total beginner in Android and I tried to make an app that exploit the camera.
In this application a FloatingActionButton is the button that triggered the camera (code below).
This button is hide if the bottomsheet is expand and show if it's collapse.
All of it work perfectly the first time I use the button.
Indeed, once the picture is taken and the mainactivity is back, the button doesn't work anymore and furthermore is behavior is "lost" too (which means that the FloatingActionButton is always shown).
I don't understand what is the source of this bug.
You'll find next to this post 2 screen, one before the button is clicked and one after and the code called in my fragment that make the onclick listener of the button :
The button is hidden when the bottomsheet is expanded (before camera was used):
The button is shown, even if the bottomsheet is expanded (after camera was used):
#Override
public void onStart() {
super.onStart();
mMapsHandler = new MapsHandler(getActivity());
mBottomSheetHandler = new BottomSheetHandler(getActivity());
FloatingActionButton button = (FloatingActionButton) getActivity().findViewById(R.id.cameraButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent); //On change d'activite
}
});

Related

animation on layout with change activity

What I want, when I click on the login button of the first activity the yellow part slides down and the next activity opens. when I click on the signup button of the second screen(login screen) the yellow part of the second screen slides up and the first activity (sign up Activity)opens. I have used slide-down animation on the linear layout on the first screen it works but not working smoothly. Any help??
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
mSignUpButton = findViewById(R.id.btnSigUp);
linearLayout=findViewById(R.id.linearLayout1);
mGotoLoginActivityButton=findViewById(R.id.btnLoginSignUpActivity);
slideDown= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_down);
//listener for Login button
mGotoLoginActivityButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//setValidation();
Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
startActivity(intent);
linearLayout.startAnimation(slideDown);
}
});
}
you shouldn't use two separated Activities for this purpose, use one and login and create account views should be packed into Fragments. this way will be way easier to animate between two views/fragments in on Activity, but if you really must use Activity then use Transitions (probably with shared elements), not animations, as these are working in one Activity during its runtime when visible (and you are currently running new Activity, which cover old one)

Material transition FAB to activity - Activity is still present for a second when transitioning back

I am trying to get a material transition to work from a FAB in Activity A to Activity B and back. Its currently working but with a slight issue. When I click on a FAB it expands to the Activity B using material transition with no issues. But when I transition back from Activity B to Activity A, Activity B duplicates and is still present for a second during the transition and it takes a second for it to disappear fully.
Here is the guide I followed:
https://material.io/develop/android/theming/motion
Specifically under the section, "Container transform examples" and under the subsection "Transition between Activities".
Here is what the issue I am facing looks like. Hopefully these images give a clear idea. In these images I am transitioning from Activity B to Activity A. This is where the problem is
Here is Activity B
The back button is pressed and Activity B transforms back into the FAB in Activity A
Now the FAB from Activity A is visible again but Activity B is still visible
Finally Activity B disappears and Activity A is visible again.
As you can see once the back button is pressed Activity A should appear immediately but it still shows
Activity B for a second.
Here is what I have in my code which is basically just following the guide:
In Activity A in the onCreate method:
setExitSharedElementCallback(new MaterialContainerTransformSharedElementCallback());
getWindow().setSharedElementsUseOverlay(false);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
And in the onClick of the FAB in Activity A:
FloatingActionButton newTaskButton = (FloatingActionButton) findViewById(R.id.newTaskButton);
newTaskButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, newTaskButton, "shared_container");
Intent intent = new Intent(MainActivity.this, CreateNewTask.class);
startActivityForResult(intent, 1, options.toBundle());
}
});
And in the onCreate method of Activity B:
findViewById(android.R.id.content).setTransitionName("shared_container");
setEnterSharedElementCallback(new MaterialContainerTransformSharedElementCallback());
MaterialContainerTransform transform = new MaterialContainerTransform();
transform.addTarget(android.R.id.content);
transform.setAllContainerColors(MaterialColors.getColor(findViewById(android.R.id.content), R.attr.colorSurface));
transform.setFitMode(MaterialContainerTransform.FIT_MODE_AUTO);
transform.setDuration(550L);
transform.setPathMotion(new MaterialArcMotion());
transform.setInterpolator(new FastOutSlowInInterpolator());
transform.setScrimColor(Color.parseColor("#4AED93"));
getWindow().setSharedElementEnterTransition(transform);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_task);
I have enabled window transitions for the theme
<!-- enable window content transitions -->
<item name="android:windowActivityTransitions">true</item>
I thought this might be an issue with the scrim color or container color but these dont affect
this particular issue. Can you guys advise?

Android Studio - Looping animation when button is pressed

I'm working on making an animation that plays when I press I button. This animation is composed of 3 frames I have made.
As of now, when I press the button, the animation starts, but doesn't stop, it just keeps looping over and over again.
What I would like is the following:
When you press the button, the animation plays once. It doesn't play until you press the button again. HOWEVER, if you press the button while the animation is playing (AKA interrupt the animation before it finishes), the animation will restart.
Here is the code I have that plays the animation when the button is pressed:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.button);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageView bun = (ImageView)findViewById(R.id.imageView);
bun.setImageResource(R.drawable.buns);
AnimationDrawable buns = (AnimationDrawable)bun.getDrawable();
buns.start();
}
});
}
}
I'm aware that you can write:
buns.setOneShot(true)
and that will cause the animation to play one time and then stop, but this isn't the solution I'm looking for.
Thanks!
Update:
I figured out the solution to this, in case anyone else was having problems.
It seems if I add the line:
buns.setOneShot(true);
after
buns.start()
this will have the intended affect, since the animation will stop after you click it and won't start until you click it again.
Cheers!

Android - How to make buttons listen more than one time

I have an app with a home screen and a bunch of buttons on it, and therefore listeners for each. After the user clicks on one of the buttons, a new layout is brought up and that layout has a back button with a listener.
The problem is that whenever the user presses the back button, the home screen layout is brought back up but none of the listeners work for the buttons anymore.
Here is some sample code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // return to home screen
// sets up a listener for when the GCF main screen button is clicked.
GCFButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.gcf); // change to the gcf layout
Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
back.setOnClickListener(new View.OnClickListener() // put a listener on back button
{
public void onClick(View v)
{
setContentView(R.layout.main); // return to home screen
}
});
Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
{
public void onClick(View v)
{
// do stuff
}
});
}
});
}
You should not change a screen with setContentView(). Screens are changed in Android by starting a new Activity with startActivity(new Intent(...)) or with Fragments like recommended by Malimo (which is a bit more difficult to do but much nicer). You call two times setContentView() where one is destroying the other one.
in my opinion you should use fragments for your contentviews. so every fragment will be responsible for its contentview and can add listeners each time it is displayed...
http://developer.android.com/guide/components/fragments.html
I'm sure that there is a method built into Android that allows you to do this, but my first thought is recursion.
The problem is that your listeners are in the onCreate method, which means that after they are run through, they won't repeat. In the back button listener,
when you set the content view to be the home screen again, that won't set up the listeners again, that will just change the content view.
To fix that, you would have to call the onCreate method again, once the back button is clicked, because then it would run your whole code with all the listeners
from the home screen again.
I suggest putting all of the listeners in a listeners() method, and then calling that method recursively when needed. It would need to be called in onCreate(...),
as well as when the back button is clicked:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
listeners(); // set up all the listeners for the buttons
}
public void listeners()
{
setContentView(R.layout.main); // return to home screen
// sets up a listener for when the GCF main screen button is clicked.
GCFButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.gcf); // change to the gcf layout
Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
back.setOnClickListener(new View.OnClickListener() // put a listener on back button
{
public void onClick(View v)
{
listeners(); // recursively call the listeners again to 'start over'
}
});
Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
{
public void onClick(View v)
{
// do stuff
}
});
}
});
}
I would also recommend putting the back button listener in its own method, so that it can be called every time the layout is changed.

Show information based on what button that was pressed

I'm making a information app for Android and I can't figure out how to change a button depending on which button was pressed in a previous class.
I made this picture that shows how the app works:
After clicking the first button you get to a new class with 2 new buttons that sends you to the same class but the 3 buttons there is supposed to change depending on which one of the two buttons you pressed, the three buttons you get sent to sends you to the same class but there is a TextView that changes text depending on what button you pressed.
So I need to add some kind of information to the button (an intent?) so it knows what you pressed earlier.
I'm kinda new to Java and Android so I'm sorry if I explained in a weird way.
Thanks in advance
Each time you start a new Activity, but some extras in that intent telling the next activity what the buttons should display. In the next activity, read the extras from the intent you got, and programatically set your buttons' text accordingly.
Here's some psuedo code.
public class FirstActivity extends Activity{
//setup button 1 and two first, then set their onClickLiseners like so
View.OnClickListener clickListener = new View.OnClickListener(){
public void onClick(View view){
Intent newActivity = new Intent(FirstActivity.this, SecondActivity.class);
if(view == button1){
//add extra to intent indicating button1 was clicked
}
else{
//add extra to intent indicating button2 was clicked
}
startActivity(newActivity);
}
};
// other stuff in your activity
}
public class SecondActivity extends Activity{
protected void onCreate(Bundle icicle){
Intent startedBy = getIntent();
if(started by has the extra indicating button 1 was clicked){
//do button1 stuff
}
else{
//do button2 stuff
}
}
}
}

Categories

Resources