0

I want to be able to click the "SizeClicker" button and change the size of the "Clicker" button. I am new to Java and Android Studios. I am working on an app. I want to be able to change the "Clicker" to whatever size I need be. If I am doing this 100% wrong could you help me change the size (The width and the height).

import android.app.Activity;
import android.os.Debug;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

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

Button Clicker = (Button) findViewById(R.id.Clicker);
Button Sizer = (Button) findViewById(R.id.SizeClicker);

Sizer.setOnClickListener(new Button.OnClickListener(){

    @Override
    public void onClick(View arg0) {
        ViewGroup.LayoutParams params = Clicker.getLayoutParams();
        params.height++;
        params.width++;
        Clicker.setLayoutParams(params);
    }});
}

The XML:

    <Button
    style="?android:attr/buttonStyleSmall"
    android:text="@string/SizeClickerText"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:id="@+id/SizeClicker"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@drawable/buttonshapesize"
    android:layout_above="@+id/Clicker"
    android:layout_toEndOf="@+id/Clicker" />

    <Button
    android:text="@string/ClickerButton"
    android:textColor="#FFFFFF"
    android:textSize="24sp"
    android:id="@+id/Clicker"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:background="@drawable/buttonshape"
    android:layout_alignParentBottom="true"
    android:layout_alignStart="@+id/ClickerDisplayText"
    android:layout_marginBottom="50dp"
    android:clickable="false" />
cory sparks
  • 21
  • 11

1 Answers1

0

inside your onClick try

button.setLayoutParams(new LinearLayout.LayoutParams(10, 100));

http://android-coding.blogspot.in/2011/05/resize-button-programmatically-using.html

If your button is inside RelativeLayout, you should use RelativeLayout.LayoutParams

Programmatically change the width of the Button in android

Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88