I wanted to move some linear layout using TranslateAnimation. I have 2 problems. My base SDK is Android 2.2.
Even when the animation is finished, the touchable area in the linear layout was not moved at all.
The screen flashed for a couple of frames right after the animation was finished.
At first, I didn't use AnimationListener and LinearLayout.layout(). When I fnished the animation using the following code, the position of the view was indeed changed. But it seemed that the touchable area was not moved with the view during animation. As a result, when I tried to clicked any of the buttons on the view after animation, nothing happened. If I clicked the original area of the buttons (the original area before the animation took place), the on_click_listener was triggerred.
Then i deleted this line of code,
tmpAnimation.setFillAfter(true);
and tried AnimationListener and LinearLayout.layout(). It did help and sovled the 1st problem.
But there came the 2 problem. After the animation, some of my linear layouts would flash for a couple of frames and then back to order.
I've tried midLinearlayout.requestLayout(), it doesn't work.I tried implemented Animation.AnimationListener and override onAnimationEnd like someone said,but it doesn't work either.
TranslateAnimation tmpAnimation = new TranslateAnimation(midLinearlayout.getLeft(),midLinearlayout.getLeft(),midLinearlayout.getTop(),midLinearlayout.getTop()+100);
//tmpAnimation.setFillAfter(true);
tmpAnimation.setDuration(2000);
tmpAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
//To change body of implemented methods use File | Settings | File Templates.
}
#Override
public void onAnimationEnd(Animation animation) {
Log.v("onflingTest","top="+midLinearlayout.getTop()+" left="+midLinearlayout.getLeft()+" right" + midLinearlayout.getRight());
midLinearlayout.layout(midLinearlayout.getLeft(), midLinearlayout.getTop()+100, midLinearlayout.getLeft() + midLinearlayout.getMeasuredWidth(), midLinearlayout.getTop()+ 100 + midLinearlayout.getMeasuredHeight());
}
#Override
public void onAnimationRepeat(Animation animation) {
//To change body of implemented methods use File | Settings | File Templates.
}
});
I've solve this by the code below:
linearlayout.clearAnimation();
see the link:
EditText stucks after animation and alive back on scrolling......?
I solved the issue with the help from post View.GONE in animation complete
The problem is after layout B completes the animation, i missed to make the view state as View.GONE. Adding View.GONE brought back the controls.
Related
OK,i have ScrollView with multiply recyclerview-s.When user click on some item on my list that item expand itself with some more info. The problem is that although it does expand correctly, my root view(in my case ScrollView) does not follow that expansion automatically ,so i need to scroll manually to see expanded item.So i used scrollView.fullScroll(View.FOCUS_DOWN) in onClick in my recyclerview.And it works fine but after this line of code is triggered every other click on some other recycelerView it scroll down although that recyclerview does not have fullScroll().
I tried to use scrollTo(),smoothScrollTo etc.I dont use ExpandableRecyclerView but cachapa/ExpandableLayout and in this thread developer said that it is rather the parent View "issue".Also tried solution from the same thread but problem persist.
So my question is how to stop triggering fullScroll(View.FOCUS_DOWN) when i click on other recuclerviews items.
my code:
` animaton.setOnClickListener(v -> {
notifyDataSetChanged();
//some stuff
scrollView.post(new Runnable() {
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
});`
I'm struggling to get my android app to show and hide imageView objects programmatically. Actually I'm struggling to get expected behavior from imageView objects full stop.
Following the answers to this question,
Here's what I'm testing with:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageView warn = (ImageView)findViewById(R.id.gpsStatusWarning);
warn.setImageResource(R.drawable.gps_error);
warn.getLayoutParams().height = 64;
warn.getLayoutParams().width = 64;
}
}
The above code is called in the parent activity's OnCreate method, and it does exactly what I expect: It changes the image of the object to be the one designated, and it sets the height and width of said object. However, what I can't seem to do is to set the object as INVISIBLE or GONE. I just can't make it vanish at all, in fact. I've tried both:
warn.setVisibility(View.INVISIBLE);
warn.setVisibility(View.GONE);
But the image is still visible. I've even tried changing it in the XML to
android:visibility="gone"
But even that hasn't helped. The image is still visible.
What am I doing wrong? Am I missing a call to some update method? Does setting the image resource force the image to be drawn?
Try :
warn.setImageResource(0);
Or : warn.setImageResource(android.R.color.transparent);
This is how you set the visibility of ImageView objects on Android programatically:
yourImage.setVisibility(View.VISIBLE);
yourImage.setVisibility(View.INVISIBLE);
yourImage.setVisibility(View.GONE);
You can also set the initial states of ImageView objects in XML layout files like this:
visibility="visible"
visibility="gone"
visibility="invisible"
You can follow the official documentation about ImageView controls to try it on yourself on this link below. Learn how to set the visibility state of a view.
imageView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
So I am working on an Android application using Android 4.0 Library.
One of the activities of this application is made up of a RelativeLayout that has an image background and a toggle button.
The background image of the layout must change when the user toggles the button.
So it must be changed from inside the activity.java class:
if (toggleButton.isChecked()){
// Change the background of the activity to image 2 (for example)
}
else{ // when toggle button is off
// Change it back to image 1
}
Please help me with this. Thank you :)
You use the method, setBackground in the View class:
if (toggleButton.isChecked()){
// Change the background of the activity to image 2 (for example)
View myView = this.findViewById(yourViewId);
myView.setBackgroundResource(yourImage);
}
else{ // when toggle button is off
// Change it back to image 1
// Change the background of the activity to image 2 (for example)
View myView = this.findViewById(yourViewId);
myView.setBackgroundResource(yourOtherImage);
}
I'm writing a very simple android app. When the user touches the surface and moves their finger around, pictures are drawn below their fingers. When their finger is raised, this stops.
I want the action bar to hide when the finger is down and reappear when it is raised.
The ActionBar is overlayed. I thought using ActionBar.hide() and .show() in my view's onTouchEvent would work fine, but I've hit some problems.
I've tried a few things. First I tried passing the ActionBar created in the Activity into the view, but this didn't work as the view has to be created before the ActionBar is "got", so I would just be passing in a null value.
I then tried to make a static ActionBar which I can call from my view, but then I get this error: "Only the original thread that created a view hierarchy can touch its views.”
Anyone know of a way around this?
Take a look at one of the examples that is part of the jfeinstein10's Sliding Menu library
In it, in the BirdActivity.java, you will see how the ActionBar is shown and hidden after 2 seconds. You will naturally have to adapt the code yourself. But this should get you started:
Excerpt of the code from Line No. 53 - 58:
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getSupportActionBar().show();
hideActionBarDelayed(mHandler);
}
});
The hideActionBarDelayed() from Line No. 80 - 86:
private void hideActionBarDelayed(Handler handler) {
handler.postDelayed(new Runnable() {
public void run() {
getSupportActionBar().hide();
}
}, 2000);
}
UPDATE
For the lack of code, most of the problem has been assumed. It may be wrong simply for the lack of data from the OP. That being said, if sorting out the "Only the original thread that created a view hierarchy can touch its views." problem will fix it, perhaps this may do it for you:
Runnable run = new Runnable() {
#Override
public void run() {
// RUN YOUR CODE TO HIDE / SHOW THE ACTIONBAR HERE
}
}; YOUR_ACTIVITY.this.runOnUiThread(run); // REPLACE WITH getActivity().runOnUiThread(run); IF THIS IS A FRAGMENT
NOTE: In the example, the activity in question uses ActionBarSherlock and extends SherlockActivity and therefore, the use of getSupportActionBar(). If you are not using ABS, you will have to use getActionBar() instead of the former.
I am trying to display Loading text only when its loading.
The problem is im setting it to visible and straight after to invisible once finished loading. But text never has a chance to update. Is there a way to force refresh the screen, or maybe there is another way to do this?
Thanks
This is my code
SearchBtn = (Button) findViewById(R.id.SearchButton);
SearchBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
//this is never seen because its set straight after to invisible
LoadingText.setVisibility(View.VISIBLE);
SearchFor(EditSearchField.getText().toString()); // all loading done here
LoadingText.setVisibility(View.INVISIBLE);
HideKeyboard();
}
});
You need to do the actual load in a separate thread. You'd be best off using an AsyncTask for this. Take a look at http://www.vogella.com/articles/AndroidPerformance/article.html