Android App tutorial: How to Use Android Wrap_Content For Wrapping Contents

How to Use Android Wrap_Content For Wrapping Contents Within The View

Android wrap_content, How to Use Android Wrap_Content For Wrapping Contents Within The View

The Android wrap_content, as the name suggests, sets a view’s size to wrap_content which will only expand enough to contain the values. In simple words, the view wrapped with wrap_content will just be big enough to enclose its contents. For instance, if you’re declaring a button name as “Get Started” and setting its height and width as wrap_content, then it’ll create a button with Get Started name size, and won’t occupy more than the content area.

Although, it’s also possible to implement wrap_content on Textview in which, it occupies space based on the length of the string. However, in today’s Android app tutorial, we’ll developing a simple demo explaining how to implement Android wrap_content in a button.

Let’s Get Started

Create a new project under file menu, modify the project details and choose location demo for project.

Android wrap_content, How to Use Android Wrap_Content For Wrapping Contents Within The View

Choose Mini support SDK to use this project.

Android wrap_content, How to Use Android Wrap_Content For Wrapping Contents Within The View

In the next tab, select add No Activity.

Android wrap_content, How to Use Android Wrap_Content For Wrapping Contents Within The View

Lastly, create XML and use custom view button in it.

Android wrap_content, How to Use Android Wrap_Content For Wrapping Contents Within The View

Now, Create class for MainActivity.

public class MainActivity extends AppCompatActivity{

    private Button buttonSample;

    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        buttonSample = (Button) findViewById(R.id.btnButtonSample);

        buttonSample.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                generateConfirmationDialog(buttonSample.getText().toString());

            }

        });

    }

    private void generateConfirmationDialog(String text) {

        final Dialog dialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);

        // Setting the title and layout for the dialog

        dialog.setContentView(R.layout.dialog_confirmation);

        final TextView tvYes = (TextView) dialog.findViewById(R.id.tvYes);

        TextView tvNo = (TextView) dialog.findViewById(R.id.tvNo);

        final EditText edtText = (EditText) dialog.findViewById(R.id.edtText);

        edtText.setText(text);

        dialog.setCancelable(true);

        dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT,

                LinearLayout.LayoutParams.WRAP_CONTENT);

        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

        dialog.getWindow().setGravity(Gravity.CENTER);

        dialog.show();

        tvYes.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                dialog.dismiss();

                buttonSample.setText(edtText.getText().toString());

            }

        });

        tvNo.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                dialog.dismiss();

            }

        });

    }

}

Manage Click Event in Main Class

// Find button by findviewbyid

buttonSample.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

generateConfirmationDialog(buttonSample.getText().toString());

}

});

Now when you run the demo, it’ll look like this:

Android wrap_content, How to Use Android Wrap_Content For Wrapping Contents Within The View

The dialog will open EditText with same context as button. Write some content and click YES and the button’s width will get changed.

Android wrap_content, How to Use Android Wrap_Content For Wrapping Contents Within The View

Android wrap_content, How to Use Android Wrap_Content For Wrapping Contents Within The View

Android wrap_content, How to Use Android Wrap_Content For Wrapping Contents Within The View

And Done!

Small and Simple. This, however, is just a small part of Android application development to help you understand the basics, before you move to the broad concepts. And if you’ve already started with your native Android app development, wrap_content may come in handy to create appealing user interface. But in case of need of technical assistance for building other aspects, you may contact an Android app development company to help you successfully create your Android app.

Grab a free copy of wrapping content demo from Github.

 
 

Want to Develop Android App From Scratch? Contact Us Now

Get your free consultation now

Leave a Reply

Your email address will not be published. Required fields are marked *

Android wrap_content, How to Use Android Wrap_Content For Wrapping Contents Within The View Android wrap_content, How to Use Android Wrap_Content For Wrapping Contents Within The View
Get Your Free Quote