How to Use Android Wrap_Content For Wrapping Contents Within View

How to Use Android Wrap_Content For Wrapping Contents Within The View

0
Shares
How to Use Android Wrap_Content For Wrapping Contents Within The View
Rate this post

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.

sswrapconentdemo1

Choose Mini support SDK to use this project.

sswrapconentdemo2

In the next tab, select add No Activity.

sswrapconentdemo3

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

sswrapcontentdemo4

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:

sswrapcontentdemo5

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

sswrapcontentdemo6

sswrapcontentdemo7

sswrapcontentdemo8

And Done!

Small and Simple. This, however, is just a small part of Android app development to help you understand the basics, before you move to the broad concepts. And if you’ve already started building your native Android app, 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 build your Android app.

Grab a free copy of wrapping content demo from Github.

 
0
Shares
 

Want to Develop Android App From Scratch? Contact Us Now

Get your free consultation now