I was wondering if someone can advise what the best way to do the following: I'm creating an application where I'm asking the user a question and the user either answers the question or proceeds to the next question with fade in and fade out animation using animator instead of anim. As of now, I have one activity and in the activity I would load the layout that I have created in XML and then remove the view and load a new view.
I'm not sure if this is the best way to present multiple views in one Activity and prevent the need to use multiple activities. The reason why I'm doing this is because I have several objects that I'm storing data into based on the category of the question (whether is math, science, social studies, etc).
Here is a picture if it helps visualize:
If there is a better way to do this, please let me know. The only issue I'm having with this is that the activity A java class is growing in code because I have to handle everything there despite having the classes of the objects defined in other java files. Thank you.
I think you can build layout like this
RelativeLayout(see the structure) Set layout1 android:visibility="visible" and layout2 android:visibility="invisible" at first in .xml
layout1 = findViewById(R.id.layout1);
layout1.setVisibility(View.VISIBLE);
layout2 = findViewById(R.id.layout2);
layout2.setVisibility(View.GONE);
Button buttonNext = (Button)findViewById(R.id.buttonNext);
buttonNext.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);
viewTransAnimation();
}
});
And set your animation in viewTransAnimation(), example:
private void viewTransAnimation() {
layout2.setScaleX(0.1f);
layout2.setPivotX(layout2.getX() + layout2.getLeft() );
layout2.animate()
.scaleX(1)
.setDuration(500)
.setInterpolator(new AccelerateInterpolator())
.start();
}
Then it will change to layout2 after you click it. (use some flag make the animation only work once for your requirement.)
You can also replace 2 view with 2 RelativeLayout and do it in the same way.
Related
I have a MainActivity.java file with a single layout. I created a new layout that is simply called layout2, and want to call it somehow in the MainActivity.java file, to switch between the 2 layouts with a btn click, but without any .java class file (like in Activities: .java file goes with his own single layout).
For short:
An Activity open (just) a new layout file.
Thanks for any answers.
ImageButton ibtt;
ImageButton ib = (ImageButton)findViewById(R.id.ibtt);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.setContentView(R.layout.secondlayout);
}
});
Not entirely sure what you are trying to do but a Viewflipper sounds something you might like. Define a viewflipper with as many children as you want in the xml. The children can be separate layouts each. Use the<include> tag. Plenty of examples with viewflipper on StackOverflow. I prefer using a statekeeper along with one always.
EDIT: Here's an example How to use view flipper with three layouts?
On my activity's creation I have set an AlphaAnimation in order to perform some visual effects. Now I have set a LayoutAnimationListener and overriden the onAnimationEnd() method. The Animation is executed correctly and the callback to onAnimationEnd() is working as well. Within the onAnimationEnd() method I want to perform another animation on two child objects of the Activity's layout. These are two EditTexts that are defined in XML with the property android:visibility="gone". For starters, what I want to do is to have them set as VISIBLE.
I've tried getting a reference from their parent, change their setVisibility(View.VISIBLE); and in the end invalidate the parent view.
This attempt does not seem to work. You will find the code attached below:
#Override
public void onAnimationEnd(Animation animation) {
Log.d(TAG, "onAnimationEnd()");
// TODO move icon up, make edittext's appear.
RelativeLayout layout = (RelativeLayout) View.inflate(
getApplicationContext(), R.layout.splash_dialog_layout,
null);
LinearLayout linear = (LinearLayout) layout.getChildAt(0);
Log.d(TAG, "Children" + linear.getChildCount());
email = (EditText) linear.findViewById(R.id.splash_Email);
email.setVisibility(View.VISIBLE);
pass = (EditText) linear.findViewById(R.id.splash_password);
pass.setVisibility(View.VISIBLE);
linear.invalidate();
layout.invalidate();
Splash_Activity.layout.invalidate();
}
};
I've tried to removeAllViews from a parent and add them one by one and invalidate again but that doesn't seem to work either.
There is probably a misunderstanding on my behalf as to how view inflation operates, shouldn't this work?
Thanks for your time.
Inflating a layout creates a new view hierarchy, which means the views you're obtaining are actually different from the ones displayed on screen even though they have the same IDs.
You can obtain on-screen views in several ways, but the easiest would probably be to query the containing Activity.
Activity.this.findViewById(R.id.splash_Email);
I have a very strange problem with Android animations, I tried many different approaches and components, and still couldn't find any explanation.
I have a FrameLayout which is a container for views, and a Button.
This FrameLayout should display only one view at a time, and when I click on the Button, I want the FrameLayout to display another view, and start an animation on the view that is removed from the FrameLayout.
The specificity here is that I only use two views, and I want to switch between these two views.
The problem:
When I click the button multiple times really fast, and then stop clicking, the two views are shown at the same time one on top off the other, and won't disappear. The container still contains only one view though... definitely strange!
error screenshot http://i.minus.com/jDXyvUsE1LCOx.png
I was able to reproduce this with a simple example:
public class TestAnimActivity extends Activity implements OnClickListener {
private FrameLayout container;
private TextView current;
private TextView next;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
current = new TextView(this);
current.setText("View 1 YEAH");
next = new TextView(this);
next.setText(" View 2 DOH");
container = (FrameLayout) findViewById(R.id.container);
container.addView(current);
findViewById(R.id.button).setOnClickListener(this);
}
public void onClick(View v) {
Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
current.startAnimation(outAnimation);
container.addView(next);
container.removeView(current);
TextView temp = current;
current = next;
next = temp;
}
}
As you can see the animations are being started on the views while previous ones are still running, and the way I do that may somehow be the source of the problem.
If I comment the animation related code, it works perfectly:
// Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
// current.startAnimation(outAnimation);
If I stop reusing views and create new views instead, it also works perfectly:
// next = temp;
next = new TextView(this);
next.setText("View: " + new Random().nextInt());
But I don't want to create new views :-) .
It seems that the problem is related to starting animations several times on a view while adding / removing this view from its parent.
I first tried with a ViewFlipper, then a ViewAnimator, then looked up the Android source code and ended up doing this manually to reproduce the problem.
If you want the layout to be able to test by yourself, here is my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me fast" />
</LinearLayout>
Thank you for your time!
I finally found a way to fix the issue.
When we remove a view from a ViewGroup, if there is an animation running on this view, it is automatically added to the "disappearing children" list of the ViewGroup.
The issue seemed to appear when we tried to add to the ViewGroup a view that has been removed from it but was still in its disappearing children.
There's an easy way to fix that : viewGroup.clearDisappearingChildren();
Here would be the new implementation of the onClick() method :
public void onClick(View v) {
next.clearAnimation();
container.clearDisappearingChildren();
Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
current.startAnimation(outAnimation);
container.addView(next);
container.removeView(current);
TextView temp = current;
current = next;
next = temp;
}
If it works without quickly pressing the button, and works without the animation then you may be seeing how animations actually work. From my understanding what is displayed in the animation is separate from the View objects they are animating. You can try animating from the parent view instead of the ones that keep being added and removed. Or make sure to clean up the animation before removing the view, as it sounds like the view isn't able to get the onAnimationEnd method where it can remove the phantom image being displayed (setFillAfter(false) which is the default behavior of an Animation).
In my Android application, I have an activity with three layouts: A left layout, a middle layout, and a right layout.
The right layout is the main one. I want the right layout to zoom into fullscreen when I click a button. If you have the specific code,it's better.
Thanks very much!
If I got your question right:
Your button's OnClickListener should look like something like this:
OnClickListener listener = OnClickListener() {
public void onClick(View v){
findViewById(R.id.left_layout).setVisibility(View.INVISIBLE);
findViewById(R.id.center_layout).setVisibility(View.INVISIBLE);
};
This by itself will not get your right layout to fullscreen. You'll need to add something the below to your right layout for it to "auto fullscreen":
android:layout_width = "0dp";
android:layout_weight = "1";
if you don't do that, you'll have to resize the right layout from the "listener" above.
Hmm, the question is vague, but if I understand correctly, what you want to do is hide the left and center layouts when a button is clicked, so that the right layout becomes full-screen?
Without more details, it's not easy to give you a more precise answer (please post your current layout), but I would do something like:
// this code in your Activity:
// this method is bound to button onCLick (android:onClick="clickButton")
public void clickButton(View view) {
findViewById(R.id.left_layout).setVisibility(View.INVISIBLE);
findViewById(R.id.center_layout).setVisibility(View.INVISIBLE);
}
And to revert back to a 3-layout display, you do the opposite (View.VISIBLE)
want to make an Android app that starts with a main layout and when you push a button (called stateButton) that is in this layout the layout changes to a main2 layout containing another button (called boton2), and when you push this one you get back to the first main.
I want to do this in the same activity without creating or starting another one.
Here I show you part of the code:
public class NuevoshActivity extends Activity
implements SensorEventListener, OnClickListener {
private Button stateButton;
private Button boton2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.stateButton = (Button) this.findViewById(R.id.boton);
this.boton2 = (Button) this.findViewById(R.id.boton2);
stateButton.setOnClickListener(this);
boton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==stateButton) {
setContentView(R.layout.main2);
}
else if(v==boton2) {
setContentView(R.layout.main);
}
}
}
The mains only have some images, text views and the buttons.
But I've some troubles. Can't it just be as simple as that or what am I missing or what is wrong?
When you use findViewById, you are actually trying to find a view inside the layout you specified by the setContentView. So using setContentView again and again might bring problems when you are trying to check for buttons.
Instead of using a setContentView, I would add the 2 layouts for the screen as child's for a view-flipper which only shows one child at a time. And you can specify the index of which child to show. The benefit of using a view flipper is that you can easily specify a 'in' and 'out' animation for the view if you need an animation when you switch between views. This is a lot cleaner method then recalling setContentView again and again.
The FrameLayout handles this wonderfully... Use this with the <include... contstruct to load multiple other layouts, then you can switch back and forth between them by using setvisibility(View.VISIBLE); and setVisibility(View.INVISIBLE); on the individual layouts.
For example:
Main XML including two other layouts:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="#+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<include android:id="#+id/buildinvoice_step1_layout" layout="#layout/buildinvoice_step1" android:layout_width="fill_parent" android:layout_height="fill_parent"></include>
<include android:id="#+id/buildinvoice_step2_layout" android:layout_width="fill_parent" layout="#layout/buildinvoice_step2" android:layout_height="fill_parent"></include>
</FrameLayout>
Code to switch between layouts:
findViewById(R.id.buildinvoice_step1_layout).setVisibility(View.VISIBLE);
findViewById(R.id.buildinvoice_step2_layout).setVisibility(View.INVISIBLE);
You will also need to set the visibility of the individual layouts when the activity starts (or in XML) otherwise you will see them both - one on top of the other.
Your boton2 button will be NULL because the definition of the button is in main2.xml.
The only views you will be able to find are the views which are defined in main.xml.
Thanks!!! All the info was usefull to understand a lot of things and as C0deAttack commented I've got troubles with the button on the main2. What I've done is to set View.VISIBLE and View.GONE to the TextViews and Buttons that I wanted in each layout. Thank you very much.