Android - Kotlin - How to overcome a minimal width limit of a button?












0















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.










share|improve this question

























  • 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
















0















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.










share|improve this question

























  • 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














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












1 Answer
1






active

oldest

votes


















1














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
}





share|improve this answer


























  • 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











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
});


}
});














draft saved

draft discarded


















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









1














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
}





share|improve this answer


























  • 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
















1














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
}





share|improve this answer


























  • 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














1












1








1







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
}





share|improve this answer















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
}






share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$