How to declare wrap content to a dynamic button in code

Here is my code

Button myButton = new Button(this);
myButton.setText("Press Me");
myButton.setTextColor(Color.WHITE);


LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
layout.addView(myButton);

How do I add wrapcontent to this button?

  • How to differentiate the connected bluetooth device in android programmatically?
  • Heart Beat Detection with Camera
  • Fastest and most efficient way to pre-populate database in Android
  • Redraw/refresh Itemizedoverlay? (android/google maps api)
  • Black screen when returning to video playback activity in Android
  • Import eclipse project to Android studio - error “this project refers to Eclipse workspace…”
  • Related posts:

    Android Studio 3.0 Canary 1: Kotlin tests or Java tests referring to Kotlin classes fail
    Where do you file bugs for Android/iOS Google Products or Services (AppInvites, Maps, etc&hellip...
    Android: download file from server and show the download progress in the notification bar using Asyn...
    Android studio. Module not specified after import
    ERROR: dump failed because no AndroidManifest.xml found
    Android: Theme Holo previous version
  • Playing multiple songs with MediaPlayer at the same time: only one is really playing
  • import xml into another xml
  • How do you reduce space between floating editText hint and editText box in Android?
  • ConsumerProguardFiles vs ProguardFiles
  • How to draw a view on top of everything?
  • Android: AdMob not working
  • 2 Solutions collect form web for “How to declare wrap content to a dynamic button in code”

    Use this line (I used FILL_PARENT to demonstrate)

    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
    

    set the wrap content for button

    myButtonsetLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    

    or for layout

    layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.