Android - Kotlin - How to overcome a minimal width limit of a button?
I am trying to manipulate a width of an android button with Kotlin using minSdkVersion 19 or higher (test phone has Android 8.0.0, API 26). When I try to make a button smaller I see that it's width couldn't pass some threshold approximately above 200px.
That's how I create and manipulate a button:
val button = Button(this)
button.width = btn_side // btn_side = 175
constraintLayout.addView(button)
What I've already tryed. Different layouts: TableLayout, ConstraintLayout. I've tryed to set textSize to zero in case if it interfered with button width. Or to replace any button text with null string. I've tryed to apply zero or 10px minWidth to a button. I've played with setPadding button method. I've tryed to assign width via layoutParams property. None of which helped.
What am I missing?
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameFieldActivity"
android:id="@+id/constraintLayout"
>
* UPD *
Someone deleted an answer with possibly useful hint: there might be a way to use different button constructors. Might be there is a way to pass desired button width through AttributeSet.
android button kotlin size
add a comment |
I am trying to manipulate a width of an android button with Kotlin using minSdkVersion 19 or higher (test phone has Android 8.0.0, API 26). When I try to make a button smaller I see that it's width couldn't pass some threshold approximately above 200px.
That's how I create and manipulate a button:
val button = Button(this)
button.width = btn_side // btn_side = 175
constraintLayout.addView(button)
What I've already tryed. Different layouts: TableLayout, ConstraintLayout. I've tryed to set textSize to zero in case if it interfered with button width. Or to replace any button text with null string. I've tryed to apply zero or 10px minWidth to a button. I've played with setPadding button method. I've tryed to assign width via layoutParams property. None of which helped.
What am I missing?
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameFieldActivity"
android:id="@+id/constraintLayout"
>
* UPD *
Someone deleted an answer with possibly useful hint: there might be a way to use different button constructors. Might be there is a way to pass desired button width through AttributeSet.
android button kotlin size
Maybe you set minwidth or the button may be constrained by its parent. Layout xml file could tell us.
– navylover
Nov 20 '18 at 2:25
Can you show us your xml layout? Is it necessarily for you to create button from code?
– Andrey Busik
Nov 20 '18 at 2:26
I've added xml of Activity to the main post. Yes, it is necessary for me to create a button from code: I want to make a logic game with a square grid of buttons. The higher difficulty - the more buttons I want to place on the screen.
– Roman Voronov
Nov 20 '18 at 2:41
add a comment |
I am trying to manipulate a width of an android button with Kotlin using minSdkVersion 19 or higher (test phone has Android 8.0.0, API 26). When I try to make a button smaller I see that it's width couldn't pass some threshold approximately above 200px.
That's how I create and manipulate a button:
val button = Button(this)
button.width = btn_side // btn_side = 175
constraintLayout.addView(button)
What I've already tryed. Different layouts: TableLayout, ConstraintLayout. I've tryed to set textSize to zero in case if it interfered with button width. Or to replace any button text with null string. I've tryed to apply zero or 10px minWidth to a button. I've played with setPadding button method. I've tryed to assign width via layoutParams property. None of which helped.
What am I missing?
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameFieldActivity"
android:id="@+id/constraintLayout"
>
* UPD *
Someone deleted an answer with possibly useful hint: there might be a way to use different button constructors. Might be there is a way to pass desired button width through AttributeSet.
android button kotlin size
I am trying to manipulate a width of an android button with Kotlin using minSdkVersion 19 or higher (test phone has Android 8.0.0, API 26). When I try to make a button smaller I see that it's width couldn't pass some threshold approximately above 200px.
That's how I create and manipulate a button:
val button = Button(this)
button.width = btn_side // btn_side = 175
constraintLayout.addView(button)
What I've already tryed. Different layouts: TableLayout, ConstraintLayout. I've tryed to set textSize to zero in case if it interfered with button width. Or to replace any button text with null string. I've tryed to apply zero or 10px minWidth to a button. I've played with setPadding button method. I've tryed to assign width via layoutParams property. None of which helped.
What am I missing?
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameFieldActivity"
android:id="@+id/constraintLayout"
>
* UPD *
Someone deleted an answer with possibly useful hint: there might be a way to use different button constructors. Might be there is a way to pass desired button width through AttributeSet.
android button kotlin size
android button kotlin size
edited Nov 20 '18 at 3:15
Roman Voronov
asked Nov 20 '18 at 2:15
Roman VoronovRoman Voronov
54
54
Maybe you set minwidth or the button may be constrained by its parent. Layout xml file could tell us.
– navylover
Nov 20 '18 at 2:25
Can you show us your xml layout? Is it necessarily for you to create button from code?
– Andrey Busik
Nov 20 '18 at 2:26
I've added xml of Activity to the main post. Yes, it is necessary for me to create a button from code: I want to make a logic game with a square grid of buttons. The higher difficulty - the more buttons I want to place on the screen.
– Roman Voronov
Nov 20 '18 at 2:41
add a comment |
Maybe you set minwidth or the button may be constrained by its parent. Layout xml file could tell us.
– navylover
Nov 20 '18 at 2:25
Can you show us your xml layout? Is it necessarily for you to create button from code?
– Andrey Busik
Nov 20 '18 at 2:26
I've added xml of Activity to the main post. Yes, it is necessary for me to create a button from code: I want to make a logic game with a square grid of buttons. The higher difficulty - the more buttons I want to place on the screen.
– Roman Voronov
Nov 20 '18 at 2:41
Maybe you set minwidth or the button may be constrained by its parent. Layout xml file could tell us.
– navylover
Nov 20 '18 at 2:25
Maybe you set minwidth or the button may be constrained by its parent. Layout xml file could tell us.
– navylover
Nov 20 '18 at 2:25
Can you show us your xml layout? Is it necessarily for you to create button from code?
– Andrey Busik
Nov 20 '18 at 2:26
Can you show us your xml layout? Is it necessarily for you to create button from code?
– Andrey Busik
Nov 20 '18 at 2:26
I've added xml of Activity to the main post. Yes, it is necessary for me to create a button from code: I want to make a logic game with a square grid of buttons. The higher difficulty - the more buttons I want to place on the screen.
– Roman Voronov
Nov 20 '18 at 2:41
I've added xml of Activity to the main post. Yes, it is necessary for me to create a button from code: I want to make a logic game with a square grid of buttons. The higher difficulty - the more buttons I want to place on the screen.
– Roman Voronov
Nov 20 '18 at 2:41
add a comment |
1 Answer
1
active
oldest
votes
Short answer:
val button = Button(this)
val layoutParams = ViewGroup.LayoutParams(
50, // you can set initial width here
ViewGroup.LayoutParams.WRAP_CONTENT
)
constraintLayout.addView(button, layoutParams)
Some details:
// create a button
val button = Button(this)
// crate a layout params you want this button to be added to ViewGroup with
val layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
// add a button to ViewGroup with layout params
constraintLayout.addView(button, layoutParams)
// set initial width
button.layoutParams.width = 50
button.width = 50
// increase width of button for 10 with each click
button.setOnClickListener {
button.layoutParams.width += 10
button.width += 10
}
Thanks, but it didn't help. I've placed setOnClickListener part stright after addView method. Android Studio wrote "it: View!" after "{" bracket. This method didn't work with height inside "{}" brackets either.
– Roman Voronov
Nov 20 '18 at 3:32
Have you clicked on the button?:)
– Andrey Busik
Nov 20 '18 at 3:34
Uh... Actually I didn't :D Thanks! Now new width applies after button is clicked. Is there a way to apply those conditions without tapping manually?
– Roman Voronov
Nov 20 '18 at 3:38
Yes, I've updated my answer
– Andrey Busik
Nov 20 '18 at 3:45
1
UPD: as I see, width must be changed AFTER addView is applied. Othervise app crashes or renders inappropriate width (and so does height).
– Roman Voronov
Nov 20 '18 at 3:53
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385270%2fandroid-kotlin-how-to-overcome-a-minimal-width-limit-of-a-button%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Short answer:
val button = Button(this)
val layoutParams = ViewGroup.LayoutParams(
50, // you can set initial width here
ViewGroup.LayoutParams.WRAP_CONTENT
)
constraintLayout.addView(button, layoutParams)
Some details:
// create a button
val button = Button(this)
// crate a layout params you want this button to be added to ViewGroup with
val layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
// add a button to ViewGroup with layout params
constraintLayout.addView(button, layoutParams)
// set initial width
button.layoutParams.width = 50
button.width = 50
// increase width of button for 10 with each click
button.setOnClickListener {
button.layoutParams.width += 10
button.width += 10
}
Thanks, but it didn't help. I've placed setOnClickListener part stright after addView method. Android Studio wrote "it: View!" after "{" bracket. This method didn't work with height inside "{}" brackets either.
– Roman Voronov
Nov 20 '18 at 3:32
Have you clicked on the button?:)
– Andrey Busik
Nov 20 '18 at 3:34
Uh... Actually I didn't :D Thanks! Now new width applies after button is clicked. Is there a way to apply those conditions without tapping manually?
– Roman Voronov
Nov 20 '18 at 3:38
Yes, I've updated my answer
– Andrey Busik
Nov 20 '18 at 3:45
1
UPD: as I see, width must be changed AFTER addView is applied. Othervise app crashes or renders inappropriate width (and so does height).
– Roman Voronov
Nov 20 '18 at 3:53
|
show 1 more comment
Short answer:
val button = Button(this)
val layoutParams = ViewGroup.LayoutParams(
50, // you can set initial width here
ViewGroup.LayoutParams.WRAP_CONTENT
)
constraintLayout.addView(button, layoutParams)
Some details:
// create a button
val button = Button(this)
// crate a layout params you want this button to be added to ViewGroup with
val layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
// add a button to ViewGroup with layout params
constraintLayout.addView(button, layoutParams)
// set initial width
button.layoutParams.width = 50
button.width = 50
// increase width of button for 10 with each click
button.setOnClickListener {
button.layoutParams.width += 10
button.width += 10
}
Thanks, but it didn't help. I've placed setOnClickListener part stright after addView method. Android Studio wrote "it: View!" after "{" bracket. This method didn't work with height inside "{}" brackets either.
– Roman Voronov
Nov 20 '18 at 3:32
Have you clicked on the button?:)
– Andrey Busik
Nov 20 '18 at 3:34
Uh... Actually I didn't :D Thanks! Now new width applies after button is clicked. Is there a way to apply those conditions without tapping manually?
– Roman Voronov
Nov 20 '18 at 3:38
Yes, I've updated my answer
– Andrey Busik
Nov 20 '18 at 3:45
1
UPD: as I see, width must be changed AFTER addView is applied. Othervise app crashes or renders inappropriate width (and so does height).
– Roman Voronov
Nov 20 '18 at 3:53
|
show 1 more comment
Short answer:
val button = Button(this)
val layoutParams = ViewGroup.LayoutParams(
50, // you can set initial width here
ViewGroup.LayoutParams.WRAP_CONTENT
)
constraintLayout.addView(button, layoutParams)
Some details:
// create a button
val button = Button(this)
// crate a layout params you want this button to be added to ViewGroup with
val layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
// add a button to ViewGroup with layout params
constraintLayout.addView(button, layoutParams)
// set initial width
button.layoutParams.width = 50
button.width = 50
// increase width of button for 10 with each click
button.setOnClickListener {
button.layoutParams.width += 10
button.width += 10
}
Short answer:
val button = Button(this)
val layoutParams = ViewGroup.LayoutParams(
50, // you can set initial width here
ViewGroup.LayoutParams.WRAP_CONTENT
)
constraintLayout.addView(button, layoutParams)
Some details:
// create a button
val button = Button(this)
// crate a layout params you want this button to be added to ViewGroup with
val layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
// add a button to ViewGroup with layout params
constraintLayout.addView(button, layoutParams)
// set initial width
button.layoutParams.width = 50
button.width = 50
// increase width of button for 10 with each click
button.setOnClickListener {
button.layoutParams.width += 10
button.width += 10
}
edited Nov 20 '18 at 3:51
answered Nov 20 '18 at 3:22
Andrey BusikAndrey Busik
6113
6113
Thanks, but it didn't help. I've placed setOnClickListener part stright after addView method. Android Studio wrote "it: View!" after "{" bracket. This method didn't work with height inside "{}" brackets either.
– Roman Voronov
Nov 20 '18 at 3:32
Have you clicked on the button?:)
– Andrey Busik
Nov 20 '18 at 3:34
Uh... Actually I didn't :D Thanks! Now new width applies after button is clicked. Is there a way to apply those conditions without tapping manually?
– Roman Voronov
Nov 20 '18 at 3:38
Yes, I've updated my answer
– Andrey Busik
Nov 20 '18 at 3:45
1
UPD: as I see, width must be changed AFTER addView is applied. Othervise app crashes or renders inappropriate width (and so does height).
– Roman Voronov
Nov 20 '18 at 3:53
|
show 1 more comment
Thanks, but it didn't help. I've placed setOnClickListener part stright after addView method. Android Studio wrote "it: View!" after "{" bracket. This method didn't work with height inside "{}" brackets either.
– Roman Voronov
Nov 20 '18 at 3:32
Have you clicked on the button?:)
– Andrey Busik
Nov 20 '18 at 3:34
Uh... Actually I didn't :D Thanks! Now new width applies after button is clicked. Is there a way to apply those conditions without tapping manually?
– Roman Voronov
Nov 20 '18 at 3:38
Yes, I've updated my answer
– Andrey Busik
Nov 20 '18 at 3:45
1
UPD: as I see, width must be changed AFTER addView is applied. Othervise app crashes or renders inappropriate width (and so does height).
– Roman Voronov
Nov 20 '18 at 3:53
Thanks, but it didn't help. I've placed setOnClickListener part stright after addView method. Android Studio wrote "it: View!" after "{" bracket. This method didn't work with height inside "{}" brackets either.
– Roman Voronov
Nov 20 '18 at 3:32
Thanks, but it didn't help. I've placed setOnClickListener part stright after addView method. Android Studio wrote "it: View!" after "{" bracket. This method didn't work with height inside "{}" brackets either.
– Roman Voronov
Nov 20 '18 at 3:32
Have you clicked on the button?:)
– Andrey Busik
Nov 20 '18 at 3:34
Have you clicked on the button?:)
– Andrey Busik
Nov 20 '18 at 3:34
Uh... Actually I didn't :D Thanks! Now new width applies after button is clicked. Is there a way to apply those conditions without tapping manually?
– Roman Voronov
Nov 20 '18 at 3:38
Uh... Actually I didn't :D Thanks! Now new width applies after button is clicked. Is there a way to apply those conditions without tapping manually?
– Roman Voronov
Nov 20 '18 at 3:38
Yes, I've updated my answer
– Andrey Busik
Nov 20 '18 at 3:45
Yes, I've updated my answer
– Andrey Busik
Nov 20 '18 at 3:45
1
1
UPD: as I see, width must be changed AFTER addView is applied. Othervise app crashes or renders inappropriate width (and so does height).
– Roman Voronov
Nov 20 '18 at 3:53
UPD: as I see, width must be changed AFTER addView is applied. Othervise app crashes or renders inappropriate width (and so does height).
– Roman Voronov
Nov 20 '18 at 3:53
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385270%2fandroid-kotlin-how-to-overcome-a-minimal-width-limit-of-a-button%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Maybe you set minwidth or the button may be constrained by its parent. Layout xml file could tell us.
– navylover
Nov 20 '18 at 2:25
Can you show us your xml layout? Is it necessarily for you to create button from code?
– Andrey Busik
Nov 20 '18 at 2:26
I've added xml of Activity to the main post. Yes, it is necessary for me to create a button from code: I want to make a logic game with a square grid of buttons. The higher difficulty - the more buttons I want to place on the screen.
– Roman Voronov
Nov 20 '18 at 2:41