I have a button (placed in a linearlayout) with animation. After the animation is completed (simple translateAnimation) I want that animated button to be enabled. I realized that using an animation only has an effect for the button drawable, so the clickable "place" stays at the original position.
layout= (LinearLayout) findViewById (R.id.newitemtext);
layout.setBackgroundResource(R.drawable.white_box);
btn = (Button) findViewById (R.id.btn);
moveLefttoRight = new TranslateAnimation(0,0 , 0, 200);
moveLefttoRight.setDuration(1000);
moveLefttoRight.setFillAfter(true);
animbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
layout.startAnimation(moveLefttoRight);
layout.setEnabled(true);
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("TAG", "CCIK");
}
});
The btn button is in the LinearLayout layout. How could I set the position after the animation? I tried to use setPadding but with no success.
Thanks in advance!
Use View.setLayoutParams(), to update a Views params - note that it depends on what kind of layout the view is inside as to how the params can be updated. For example, if your view is in a LinearLayout, then you will only be able to adjust params that relate to a LinearLayout (gravity, weight, width, height, etc.) so you would not be able to adjust the absolute X/Y coords. You may want to switch to something like a RelativeLayout to try and move the view to the final destination based on the location of other views.
Related
I have to create a view like this -
There will be three items shown with a show more option
On Click of show more the list will grow to 10 items which will be scrollable .
What could be the optimal way to implement this kind of view ?
Use HorizontalScrollView
On the first population of the scrollview detect the click on the 4th position and on the 4th item click reload the data and call adapter.notifyDataSetChanged()
fro this purpose you can Add 4 element at first time.and detect which Item is clicked by user if this is on 3rd position then you can add more element using this code snippets
HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView1);
LinearLayout topLinearLayout = new LinearLayout(this);
// topLinearLayout.setLayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT);
topLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 15; i++){
final ImageView imageView = new ImageView (this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.ic_launcher);
topLinearLayout.addView(imageView);
imageView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Log.e("Tag",""+imageView.getTag());
}
});
}
scrollView.addView(topLinearLayout);
hope so it will work for you.
enjoy your coding time:)
Use RecyclerView instead.
set the LinearLayoutManager's orientation HORIZONTAL, like this:
recyclerView.setLayoutManager(new LinearLayoutManager(context, HORIZONTAL, false);
Good luck.
I have done enough searching and finally I am asking this question.
I am converting gallery of imageviews into Viewpager backed up by PagerAdapter. I was able to achieve this:-
In onpageselected I am getting the position which I am using to get the red border.
Problem:-
Only the left most imageview is returned in onpageselected(). The rightmost imageview can never come to left and thus cannot be selected. Further on touching an imageview onpageselected() does not get called. It only gets called when you swipe it.
Questions:-
How to centre the selected imageview?
How to get the imageview position on touching it?
Thanks to NathanZ he gave me some direction. Finally I had to set a OnClickListener() on the ImageView not the ViewPager.
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
MyImageView grpView = new MyImageView(ctxt);
final int temp=position;
grpView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
pos=temp;//This is the value what I am interested in
}
});
grpView.setLayoutParams(new LayoutParams((int)(109*density), (int)(109*density)));
myViewPager.addView(grpView);
return grpView;
}
I have a ToggleButton. I want to have more place for click on this button. If I add layout_width, layout_height etc. the image looks bad. I also tried using android:padding but it did not help me.
It is needed for the convenience of users.
Easy solution : If you have image for the button, then create transparent area around it (i.e. for touch area).
The grey area can be transparent to increase the touch area.
Or use TouchDelegate
You can also increase touch area by setting touch delegates Android Developer Training Blog Post
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the parent view
View parentView = findViewById(R.id.parent_layout);
parentView.post(new Runnable() {
// Post in the parent's message queue to make sure the parent
// lays out its children before you call getHitRect()
#Override
public void run() {
// The bounds for the delegate view (an ImageButton
// in this example)
Rect delegateArea = new Rect();
ImageButton myButton = (ImageButton) findViewById(R.id.button);
myButton.setEnabled(true);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,
"Touch occurred within ImageButton touch region.",
Toast.LENGTH_SHORT).show();
}
});
// The hit rectangle for the ImageButton
myButton.getHitRect(delegateArea);
// Extend the touch area of the ImageButton beyond its bounds
// on the right and bottom.
delegateArea.right += 100;
delegateArea.bottom += 100;
// Instantiate a TouchDelegate.
// "delegateArea" is the bounds in local coordinates of
// the containing view to be mapped to the delegate view.
// "myButton" is the child view that should receive motion
// events.
TouchDelegate touchDelegate = new TouchDelegate(delegateArea,
myButton);
// Sets the TouchDelegate on the parent view, such that touches
// within the touch delegate bounds are routed to the child.
if (View.class.isInstance(myButton.getParent())) {
((View) myButton.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}
}
Use TouchDelegate for your ToggleButton as ammar26 have commented you.
Or
Try this:
Make one parent layout like LinearLayout or RelativeLayout that cover the ToggleButton. And now put margin to that Parent layout.
Now, on click of that Parent layout do action for the toggle button.
Hope it will help you to increase touch area for your view.
Happy Coding.
Instead of putting the touch event in button put it in the layout containg the only the button..and fix the size of the layout as ur wish
increase the values of android:padding:
<SeekBar android:id="#+id/seek" android:layout_width="0dip"
android:layout_height="wrap_content" android:layout_weight="1"
android:paddingTop="5dp" android:paddingBottom="5dp"
android:progressDrawable="#drawable/green_scrubber_progress_horizontal_holo_light"
android:thumb="#drawable/thumb" />
Take the margin (place of padding) of your Button from its parent layout and then perform opration on your Layout
mLayout.setonTouchListener(View.onTouchListener{
// here your working code
});
I have a Linear layout (say A - at top right) inside a FrameLayout. There are some set of new linear layouts created from the code at center bottom & added to the same FrameLayout(say l1,l2,l3...l10). Width & Height of all the layouts (Layout A & 10 new layouts) are same. Hence at center bottom location layout l10 is being shown as its the latest layout added.Remaining layouts will not be shown as they will be behind l10 . Now I want to translate all the 10 layouts from Layout B to Layout A with TranslateAnimation which I am able to do without any issues.
When the first layout(l10) translates from its position to Layout A , it covers the layout A area and i10 will be shown on Layout A now (which is what expected) and l9 will be shown at bottom center location. BUT when the next layout(l9) translates layout A , it is going below l10 . Instead it should cover l10 and l9 should be shown on Layout A area now. If anyone has faced this issue before and has any solutions for the same , please share it.
(Have clear screenshots but not able to attach as I am a new member
Thanks in advance.
**CODE**
int numberOfCardsLeft = 10;
FrameLayout fl=(FrameLayout)findViewById(R.id.cardsFrameMainLayout);
ArrayList<LinearLayout> animationCardLayouts= new ArrayList<LinearLayout>(10);//used for animation
for(int i=0;i<10;i++) //create 10 layouts & add to FrameLayout
{
cpuPlayers.add(temp.get(i));
tempCardLayout = new LinearLayout(this);
layoutParams=new FrameLayout.LayoutParams(cardLayoutWidth,cardLayoutHeight);
layoutParams.gravity=Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM;
//layoutParams.leftMargin=0;
tempCardLayout.setLayoutParams(layoutParams);
tempCardLayout.setWeightSum(10);
tempCardLayout.setOrientation(LinearLayout.VERTICAL);
tempCardLayout.setGravity(Gravity.CENTER);
//adding different childs & different background to it
animationCardLayouts.add(tempCardLayout);
fl.addView(tempCardLayout);
}
cardAnimation = new TranslateAnimation("<to fit area of Layout a>");
cardAnimation.setFillAfter(true);
cardAnimation.setDuration(900);
cardAnimation.setAnimationListener(cardAnimationListener);
animationCardLayouts.get(numberOfCardsLeft-1).startAnimation(cardAnimation);
cardAnimationListener= new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
numberOfCardsLeft--;
if(numberOfCardsLeft!=0)
{
tempCardAnimation = new TranslateAnimation("<to fit area of Layout a>");
tempCardAnimation.setFillAfter(true);
tempCardAnimation.setDuration(900);
tempCardAnimation.setAnimationListener(cardAnimationListener);
animationCardLayouts.get(numberOfCardsLeft-1).startAnimation(tempCardAnimation);
}
}
};
I have added a bunch of images to a ViewFlipper and now I am performing an onClick event in the flipper. For this I would like to know the current child position so that I can perform some operations in an array. Is there anyway to find out the position of the current child.
Use this to get the current Child position:
flipper.getDisplayedChild();
And this to set child number to view:
flipper.setDisplayedChild(8);
In addflipperimages(ViewFlipper flipper) method you are adding bunch of images to ViewFlipper for that you are creating imageview, set tag to imageview, set imageview clickable true then write onclick method to imageview. go through the fallowing code it may works for you
here ids[] is an array of image ids
private void addFlipperImages(ViewFlipper flipper) {
int imageCount = ids.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
for (int count = 0; count <imageCount; count++) {
ImageView imageView = new ImageView(this);
Bitmap imbm = BitmapFactory.decodeResource(this.getResources(),ids[count]);
imageView.setImageBitmap(imbm);
imageView.setLayoutParams(params);
imageView.setTag(count);
imageView.setClickable(true);
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int id=(Integer) v.getTag();
Toast.makeText(ImageSliderVertical.this, id+"", Toast.LENGTH_LONG).show();
}
});
flipper.addView(imageView);
flipper.setTag(count);
}
}
Make use of indexOfChild().
Check this Post
How can I programmatically display a ViewFlipper's second child?
May this works for you.
I used this flipper.indexOfChild(flipper.getCurrentView())