Scale button without affect other buttons - android

I have a LinearLayout, i create(programatically) and adds buttons to it
public void initButtons(){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
new android.app.ActionBar.LayoutParams(dpiToPixels(context, 100), dpiToPixels(context,100)));
params.setMargins(dpiToPixels(context,20), 0, 0, 0);
button.setLayoutParams(params);
}
layout.addView(button, index);
When user touch(onTouchListener) any button i want to scale it:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) button
.getLayoutParams();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
lp.width = (int) (93 * Resources.getSystem()
.getDisplayMetrics().density);
lp.height = (int) (93 * Resources.getSystem()
.getDisplayMetrics().density);
button.setLayoutParams(lp);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
lp.width = (int) (100 * Resources.getSystem()
.getDisplayMetrics().density);
lp.height = (int) (100 * Resources.getSystem()
.getDisplayMetrics().density);
button.setLayoutParams(lp);
}
return false;
}
});
The problem is that when i press one button down it affects every other buttons on the LinearLayout( looks scaled and moving a little left). Anyone has any idea how to solve this?

This is bound to happen in a LinearLayout.
Try using FrameLayout. In a frameLayout, changing one button size won't affect others.
You can also use a RelativeLayout, but you shouldn't position your buttons 'relative' to others in it.

Related

Shrink/Grow layout w/ animation on DialogFragment

I have a DialogFragment I'm trying to animate so that when onClick()'ed a confirmation appears underneath.
I have tried using setVisibility() with an Animator, but that isn't what I'm looking for. I want the layout to Slide In with the animation not appear after, or conversely disappear before.
I have been playing with some code from Github here https://github.com/ThePreviousOne/Example
`
handle.setOnClickListener( new View.OnClickListener() {
float startHeight;
#Override
public void onClick(View v) {
startHeight = slideDownView.getHeight();
// Adjust the slide down height immediately with touch movements.
if (down) {
LayoutParams params = slideDownView.getLayoutParams();
params.height = (int) (startHeight - 300);
slideDownView.setLayoutParams(params);
down = false;
} else {
LayoutParams params = slideDownView.getLayoutParams();
params.height = (int) (startHeight + 300);
slideDownView.setLayoutParams(params);
down = true;
}
}
});
This works but I dont know how to connect the new code to an animator so I can control the speed the fragment resizes at
You can define a custom animator that updates the height property at each frame of the animation. For example:
int startHeight = slideDownView.getHeight();
// Note that you should not hardcode "300" as that will be different pixel values on
// different devices - get the value from a dimen resource or scale by the
// device density
int endHeight = startHeight - getDistanceToAnimate();
// Create a simple int animator that animates between the starting and ending height
ValueAnimator animator = ValueAnimator.ofInt(startHeight, endHeight);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
// On each frame, update the view height
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = value;
view.setLayoutParams(layoutParams);
}
#Override
public void onAnimationEnd(Animator animation) {
// Once the animation finishes, you might have to update the view's final
// height and / or its `layout_height` attribute.
}
});
animator.setDuration(getAnimationTime());
animator.start();
Hope that helps!

View/Property transition

I have the following transition which I need to make when user scrolls the recyclerview down as shown in the image attached. The "Title A" and "Description" are inside a RelativeLayout and will collapse and their properties changed:
"Title A" from layout_centerHorizontal=true to layout_centerVertical=true
"Description" from layout_centerHorizontal=true to layout_centerVertical=true, textsize from 30sp to 16sp
Minimize the relativelayout, height from 70dp to wrap_content
When user scrolls up and hit the top, the changes will revert to original expanded form. (This is similar to the concept of collapsingtoolbar)
I've successfully implemented such that when i click on a button, the layout and views will change correctly using view.requestlayout(). But I'm not sure how to link the transitions to deltaY of onscrolled function.
Here is what I've tried:
changeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
animateChanges();
}
});
private void animateChanges() {
ViewGroup.LayoutParams layout = relativeLayout.getLayoutParams();
RelativeLayout.LayoutParams header = (RelativeLayout.LayoutParams) titleA.getLayoutParams();
RelativeLayout.LayoutParams value = (RelativeLayout.LayoutParams) description.getLayoutParams();
// if current is not expanded, we change to expand view
if (!isExpanded) {
relativeLayout.setGravity(Gravity.CENTER);
layout.height = (int) getResources().getDimension(R.dimen.height_60);
header.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
header.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
header.addRule(RelativeLayout.CENTER_VERTICAL, 0);
value.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
value.addRule(RelativeLayout.BELOW, R.id.title_a);
value.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
value.addRule(RelativeLayout.CENTER_VERTICAL, 0);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
value.addRule(RelativeLayout.ALIGN_PARENT_END, 0);
header.addRule(RelativeLayout.ALIGN_PARENT_START, 0);
}
relativeLayout.setLayoutParams(layout);
titleA.setLayoutParams(header);
description.setLayoutParams(value);
description.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_30));
titleA.requestLayout();
description.requestLayout();
relativeLayout.requestLayout();
} else {
relativeLayout.setGravity(Gravity.CENTER_VERTICAL);
layout.height = ViewGroup.LayoutParams.WRAP_CONTENT;
header.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);
header.addRule(RelativeLayout.BELOW, 0);
header.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
header.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
value.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);
value.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
value.addRule(RelativeLayout.BELOW, 0);
value.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
value.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
header.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
}
titleA.setLayoutParams(header);
description.setLayoutParams(value);
relativeLayout.setLayoutParams(layout);
description.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_16));
relativeLayout.requestLayout();
titleA.requestLayout();
description.requestLayout();
}
isExpanded = !isExpanded;
}
Thanks in advance!

When creating TextView dynamically the second TextView is not appearing

I'm creating two TextViews in my LinearLayout using the code below. I'm using them this way to catch a touch event when the press this specified area of the screen
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LinearLayout layOut = (LinearLayout)findViewById(R.id.loginView);
LinearLayout.LayoutParams loginParams = new LinearLayout.LayoutParams(520, 70);
loginParams.setMargins(120, 670, 0, 0);
TextView loginBttn = new TextView(this);
layOut.addView(loginBttn);
loginBttn.setBackgroundColor(Color.GREEN);
loginBttn.setLayoutParams(loginParams);
loginBttn.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Code touch events
}
});
LinearLayout.LayoutParams forgotParams = new LinearLayout.LayoutParams(370, 40);
forgotParams.setMargins(150, 770, 0, 0);
TextView forgotBttn = new TextView(this);
layOut.addView(forgotBttn);
forgotBttn.setBackgroundColor(Color.GREEN);
forgotBttn.setLayoutParams(forgotParams);
forgotBttn.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Code touch events
}
});
I'm using Green to locate them. When finished I'll take those lines out. The first works and appears fine but the second does not appear and I can't see why.
I think its out of screen try lower values for margin and position
eg:
LinearLayout.LayoutParams loginParams = new LinearLayout.LayoutParams(50, 50);
loginParams.setMargins(10, 10, 0, 0);
TextView loginBttn = new TextView(this);
layOut.addView(loginBttn);
loginBttn.setBackgroundColor(Color.GREEN);
loginBttn.setLayoutParams(loginParams);
LinearLayout.LayoutParams forgotParams = new LinearLayout.LayoutParams(10, 10);
forgotParams.setMargins(20, 20, 0, 0);
TextView forgotBttn = new TextView(this);
layOut.addView(forgotBttn);
forgotBttn.setBackgroundColor(Color.GREEN);
forgotBttn.setLayoutParams(forgotParams);

Detecting which views are touched within swipre over gridlayout

I am currently working on an application which should be able to detect which individual textviews in a gridlayout are touched. Goal is to set the background color of the touched views to a different color. I am creating the views like this:
for (int row = 1; row < 13; row++) {
for (int col=0; col<24; col++) {
TextView tv = new TextView(this);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(row), GridLayout.spec(col));
params.width = (int)(screenWidth / 24);
params.height = (int) (tenth_heigth * 8) / 12 ;
tv.setLayoutParams(params);
tv.setGravity(Gravity.CENTER);
tv.setBackgroundColor(Color.WHITE);
tv.setText("A");
tv.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(tv, params);
}
}
I tried to do this with an overridden onTouchEvent method within the activity, but so far I had no luck achieving my goal. What's the best way to do this?
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
// would like to set the background color of the touched view to blue
break;
case MotionEvent.ACTION_MOVE :
// would like to set the background color of the touched views to red
break;
case MotionEvent.ACTION_UP :
// set the background color to purple
break;
}
return super.onTouchEvent(event);
}
I would appreciate if someone could point me in the right direction. Thanks a lot!

I am developing one application in which i want to add image on another image where user touch.please help me

public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
int x = (int) event.getX() ;
int y = (int) event.getY();
System.out.println("xxx");
release=(ImageView)findViewById(R.id.imageView2);
RelativeLayout.LayoutParams lp =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); //Assuming you use a RelativeLayout
ImageView iv=new ImageView(getApplicationContext());
System.out.println("YYYY");
lp.setMargins(x,y,0,0);
System.out.println("aaaa");
iv.setLayoutParams(lp);
System.out.println("bbbbb");
iv.setImageDrawable(getResources().getDrawable(R.drawable.pain_icon));
System.out.println("ccccc");
((ViewGroup)v).addView(release);
}
return true;
}
You haven't added the dynamicallly created view to ur parent. first declare ur parent and add this iv (imageview in ur code) to the parent using relativeLayout.addView(iv);
i'm guessing here since you din't post the error.
R.id.imageView2 most likely has a parent and you are trying to add it to another viewgroup. thats whats causing the error. you need to create a new instance of ImageView then add it to your view group
ImageView im = new ImageView(this);
im.setImageResource(R.drawable.nameofimage);
((ViewGroup)v).addView(im);

Categories

Resources