Skip to content

Instantly share code, notes, and snippets.

@evik246
Last active April 5, 2022 19:47
Show Gist options
  • Save evik246/033f87b08a527ab26a969a23d5abeb7a to your computer and use it in GitHub Desktop.
Save evik246/033f87b08a527ab26a969a23d5abeb7a to your computer and use it in GitHub Desktop.
Увеличивает ширину кнопки
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="getWider"
android:text="Button"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.widthbutton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void getWider(View view) {
Button btn = (Button) view;
int currentWidth = btn.getWidth();
ConstraintLayout layout = findViewById(R.id.layout);
ConstraintSet set = new ConstraintSet();
set.clone(layout);
set.connect(btn.getId(), ConstraintSet.LEFT, layout.getId(), ConstraintSet.LEFT, 0);
set.connect(btn.getId(), ConstraintSet.RIGHT, layout.getId(), ConstraintSet.RIGHT, 0);
set.connect(btn.getId(), ConstraintSet.BOTTOM, layout.getId(), ConstraintSet.BOTTOM, 0);
set.connect(btn.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 0);
set.constrainWidth(btn.getId(), currentWidth + 20);
set.constrainHeight(btn.getId(), ConstraintSet.WRAP_CONTENT);
set.applyTo(layout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment