0

How can I loop this process? I want to shrink the size of a textview with each button click. It will work for one button click, but will not shrink for each click.

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button button2;
    TextView foxHealth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button2 = findViewById(R.id.button2);
        foxHealth = findViewById(R.id.foxHealth);
        int foxStartHealth = 150;
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewGroup.LayoutParams params = foxHealth.getLayoutParams();
                params.width = foxStartHealth - 5;
                foxHealth.setLayoutParams(params);
            }
        });
    }
}
  • 1
    Danny welcome in this community. This post gets little attention and I want to give you a few tips. Change your tags, which are too generic (e.g. Android etc.) and also work on your question and description (more context). It would be also helpful to add more background to your example (e.g. a bit of the data behind it). – crisscross Feb 25 '21 at 21:46

2 Answers2

1

params.width = foxHealth.getMeasuredWidth() - 5;

//Using the textview foxHealth instead of the integer foxStartHealth

0
int foxStartHealth = 150;
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ViewGroup.LayoutParams params = foxHealth.getLayoutParams();
            params.width = foxStartHealth - 5; //you always restart at 150 here
            foxHealth.setLayoutParams(params);
        }
    });

You always shrink from that original value (foxStartHealth = 150) instead of the newest value.

params.width = params.width - 5;
sigma1510
  • 1,165
  • 1
  • 11
  • 26
  • Thanks. I tried using params.width - 5 but now when I click the button, nothing happens. Should I change foxHealth in the setLayoutParams(params) too? – Danny Villa Feb 25 '21 at 19:16
  • You can try to create a new LayoutParam with the wanted values, yes: foxHealth.setLayoutParams(new LayoutParams(params.width - 5, params.height)) – sigma1510 Feb 25 '21 at 21:42
  • It is asking me to import a class for the new LayoutParams (which shows in red). I've tried each class with no success. – Danny Villa Feb 26 '21 at 02:57
  • Sorry, foxHealth.setLayoutParams(new ViewGroup.LayoutParams(...)) – sigma1510 Feb 26 '21 at 06:03
  • If that still doesn't work, can you share some more information (xml files, logs) in your question? There may be another issue elsewhere. – sigma1510 Feb 26 '21 at 06:13
  • I updated the question here https://stackoverflow.com/q/66390395/15252372 – Danny Villa Feb 26 '21 at 20:31