textviews background change its size when i click it - java

Whenever I click the textview background gets bigger and don't even fit in textviews borders
before:
before click
after:
after click
the textview I use:
<TextView
android:id="#+id/Card1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/placeholder"
android:gravity="bottom"
android:text="#string/placeholder"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="italic"
app:layout_constraintDimensionRatio="0.678"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/Card2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline3"></TextView>
and heres the code on click
card1.setOnClickListener(view -> {if (!card1Poped[0]){
ValueAnimator animation = ValueAnimator.ofFloat(0f, 100f);
animation.setDuration(500);
animation.setInterpolator(new OvershootInterpolator());
animation.start();
animation.addUpdateListener(updatedAnimation -> {
float animatedValue = (float) updatedAnimation.getAnimatedValue();
card1.setTranslationY(-animatedValue);
card2.setTranslationY(0);
card3.setTranslationY(0);
card4.setTranslationY(0);
card5.setTranslationY(0);
card1Poped[0] = true;
card2Poped[0] = false;
card3Poped[0] = false;
card4Poped[0] = false;
card5Poped[0] = false;
});
}else if (!CardViewerPoped[0]) {
CardViewer.setVisibility(View.VISIBLE);
CardViewer.setAlpha(0F);
CardViewer.setImageDrawable(card1.getBackground());
ValueAnimator alphaAnim = ValueAnimator.ofFloat(0F, 1F);
alphaAnim.setDuration(250);
alphaAnim.setInterpolator(new LinearInterpolator());
alphaAnim.start();
alphaAnim.addUpdateListener(valueAnimator -> {
float alphaValue = (float) valueAnimator.getAnimatedValue();
CardViewer.setAlpha(alphaValue);
});
CardViewerPoped[0] = true;
}
});
the problem is with "else if" part, there it basically gives its background to an ImageView, and that image view starts to fade in, I DO NOTHING with that textview but it changes and idk why
I tried like .invalidate cuz its only thing I can think of that can help but it doesn't.

Related

Rotate ImageView

I have roulette (ImageView) with count of scrolls. When I tap on the button once roulette starts to round on 270 degree. When I tap on the button again, roulette must speeds up and round more. How to realise speed up of scrolling after start first animation and decrement count of scrolling after full end of animation?
I am having trouble understanding exactly what you want to do as you did not provide any code to reference that shows an error, but this should do what you are asking in terms of allowing your image to rotate faster as you continually click a "rotate" button.
I have used ObjectAnimator
activity_main.xml:
<ImageView
android:id="#+id/imageView_roulette"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#mipmap/ic_launcher"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Rotation"
app:layout_constraintTop_toBottomOf="#+id/imageView_roulette"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/buttonStop"
app:layout_constraintRight_toRightOf="parent"
/>
<Button
android:id="#+id/buttonStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
app:layout_constraintTop_toBottomOf="#+id/imageView_roulette"
app:layout_constraintLeft_toRightOf="#+id/button"
app:layout_constraintRight_toRightOf="parent"
/>
</android.support.constraint.ConstraintLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
Boolean rotating = false;
Integer durationOfSpin = 3000; //default 3000
ImageView image;
ObjectAnimator animation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonStart = (Button) findViewById(R.id.button);
Button buttonStop = (Button) findViewById(R.id.buttonStop);
image = (ImageView) findViewById(R.id.imageView_roulette);
buttonStart.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if(!rotating) {
rotating = true;
animation = ObjectAnimator.ofFloat(
image, "rotation", 0, 360);
animation.setDuration(durationOfSpin);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(Animation.INFINITE);
animation.start();
}else{
if(durationOfSpin - 300 > 0) {
durationOfSpin -= 300; //make slightly faster each subsequent click
}
animation.setDuration(durationOfSpin);
}
}
});
buttonStop.setOnClickListener(new Button.OnClickListener() {
//just here so you can cancel your rotation
public void onClick(View v) {
if(rotating) {
rotating = false;
durationOfSpin = 3000; //set back to default
animation.cancel();
}
}
});
}
}

How to fade ImageView after drag/drop is done?

I have one ImageView (drag and drop) and one button that fades the Image. drag and drop works fine and fade button initially works fine to. The issue arises when the image is dragged and THEN fade button is pressed.
it seems that it only fades the original place the image was(before drag and drop). I would like to fade the actual image after its been dragged. Here is a picture explaining :enter image description here
my main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="app.com.example.muhammad.downloadmanagerexample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/Hello"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/images"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fade"
android:id="#+id/buttonFade"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.imageView);
//downloadFile(path);
image.setOnTouchListener(this);
Button fadeButton = (Button) findViewById(R.id.buttonFade);
fadeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(1000);
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.REVERSE);
image.startAnimation(animation);
//appIcon.animate().alpha(0.0f).setDuration(1000);
//appIcon.animate().alpha(1.0f).setDuration(1000);
}
});
}
PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
PointF StartPT = new PointF(); // Record Start Position of 'img'
#Override
public boolean onTouch(View v, MotionEvent event) {
int eid = event.getAction();
switch (eid)
{
case MotionEvent.ACTION_MOVE :
PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
image.setX((int)(StartPT.x+mv.x));
image.setY((int)(StartPT.y+mv.y));
StartPT = new PointF( image.getX(), image.getY() );
break;
case MotionEvent.ACTION_DOWN :
DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF( image.getX(), image.getY() );
break;
case MotionEvent.ACTION_UP :
// Nothing have to do
break;
default :
break;
}
return true;
}
}
I have been searching about this topic and couldn't find an excat solution so thought about asking it. Thank you in advance. really appreciate this site and all of the people who contribute to it :)
you actually create new image every time.You are not dealing with one image only.You do not have same instance of image.when you fade you are deleting image.And when you drop image after dragging then you recreate image.If you find my answer appropriate then upvote me.Thanks

Popupwindow show at location, changing the gravity gives false position

I made a popup window appear when you click on text view, i followed a guide how to create popups and i work fine, just the location of the popup is not what i want, it seems that the popup make its left top corner to show at the point location, but i want the bottom left corner to be at the specified point like this,after searching i found that gravity is responsible for this see developer.android , but when I change the gravity to Gravity.BOTTOM|Gravity.LEFT the popup shows way above the text
The popup XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="#+id/popup"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:padding="7dp"
android:background="#d1a2a2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView37"
android:gravity="center"
android:textAlignment="gravity"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView41" />
</LinearLayout>
Here is the code I have
vfy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//Open popup window
if (p != null)
showPopup(MainActivity.this, p);
}
});
These are the methods
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
TextView button = (TextView) findViewById(R.id.textView2);
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
button.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
//............
private void showPopup(final Activity context, Point p) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 30;
int OFFSET_Y = 30;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
// Getting a reference to Close button, and close the popup when clicked.
TextView close = (TextView) layout.findViewById(R.id.textView37);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
}
You can use showAsDropDown() or showAtLocation() method.
Refer these links Change gravity of PopupWindow and
How to show PopupWindow at special location?

Change TextureView Size at Runtime

I am working on streaming a mp4 to a textureView for a app i am currently working on. i have the TextureView displaying the video however i need to resize it to match the screen size on rotation. After much trial and error it seems that the problem is that the TextureView cannot be made larger than the containing view. I have also tried to resize the container view but i am then unable to center the TextureView correctly on screen.
public void onOrientationChanged(int orientation) {
if(isLandscape(orientation)){
myTexture.setRotation(-90);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.width = height;
params.height = (height * 9)/16;
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
myTexture.setLayoutParams(params);
myTexture.getLayoutParams().height = (height * 9)/16;
myTexture.getLayoutParams().width = height;
rl.requestLayout();
rl.invalidate();
rl.recomputeViewAttributes(myTexture);
Log.v("View Size", "Width tex: " + myTexture.getWidth());
Log.v("View Size", "Height tex: " + myTexture.getHeight());
Log.v("View Size", "Width tex parent: " + rl.getWidth());
Log.v("View Size", "Height tex parent : " + rl.getHeight());
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
android:id="#+id/mediaParent"
android:layout_centerInParent="true"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp">
<TextureView
android:layout_width="360dp"
android:layout_height="203dp"
android:id="#+id/surface"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Try This link :
http://www.binpress.com/tutorial/video-cropping-with-texture-view/21
private void initView() {
mTextureView = (TextureView) findViewById(R.id.textureView);
// SurfaceTexture is available only after the TextureView
// is attached to a window and onAttachedToWindow() has been invoked.
// We need to use SurfaceTextureListener to be notified when the SurfaceTexture
// becomes available.
mTextureView.setSurfaceTextureListener(this);
FrameLayout rootView = (FrameLayout) findViewById(R.id.rootView);
rootView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_UP:
updateTextureViewSize((int) motionEvent.getX(), (int) motionEvent.getY());
break;
}
return true;
}
});
}
private void updateTextureViewSize(int viewWidth, int viewHeight) {
mTextureView.setLayoutParams(new FrameLayout.LayoutParams(viewWidth, viewHeight));
}

TextView ellipse disappears on setText()

Good afternoon.
I have the following TextView:
<TextView android:id="#+id/keypadwidget_number"
android:tag="#string/phone_number_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textSize="30sp"
android:ellipsize="start"
android:scrollHorizontally="true"
android:gravity="right"
android:singleLine="true"
android:textColor="#color/widget_text"
android:layout_toLeftOf="#+id/keypadwidget_btn_delete"/>
My expected result is to have the following:
...99862214
But instead, I'm getting:
Sorry about the background/text colour. Not my choice!
Each of those buttons call:
mPhoneNumberTextView.append(clickValue);
My question is; What am I doing wrong?
EDIT:
When I set the TextView's android:text property to something very long, the ellipse is set as expected. It's when mPhoneNumberTextView.append(clickValue); is run that it breaks.
Use android:layout_width="fill_parent" for your TextView
In a fit of rage, I added a drawable to the left of the text view every time it was marqueed.
private boolean isMarqueed(TextView tv){
return isMarqueed(tv.getText().toString(), mPhoneNumberTextView.getWidth(), mPhoneNumberTextView);
}
private boolean isMarqueed(String text, int textWidth, TextView tv) {
Paint testPaint = new Paint();
testPaint.set(tv.getPaint());
boolean isMarquee = true;
if (textWidth > 0) {
int availableWidth = (int) (textWidth - tv.getPaddingLeft() - tv.getPaddingRight()-testPaint.measureText(text));
if(availableWidth > 0)
isMarquee = false;
}
return isMarquee;
}
Which is a slight variation of How to check if a TextView String has been trimmed (marquee)?

Categories