How to set up a dimensions XML file

It is essential to have a dimensions XML file in every android project you create. To be used the same dimensions multiple times and hardcode it is not good programming practise. It is easy to forget about one when you are a beginner.

I am going to step you through how to create one and link it up to your android page. To do so I am going to use the main page which we have already created in the previous guides.

1. Go to values

 Image

 

2. Right click on values > new > Values resource file

Image 

3. A new dialog box should then show up enabling you to enter a name for the resource.  It should always be called dimens.

4. You will then be taken to you new XML page once you click OK. In there you can begin to declare the dimens which you want to have.

You do not need to set up all of your dimensions at once these can be added to as you go along.

To declare a new dimens:

Image

The same format above should be used for all dimens being used.

5. Once you have declared the dimens, you can now link it up to the app page.

To do so go to layout folder > main.xml

6. I am going to change the TextViews size of text. You use the same aspect each time.

 

This can be done two ways:

Locate the properties palette on the left hand side of the screen.  Look for a resource which says Text Size. Click on it.

Image

Within the text size box select the three dots. This will then show another dialog box which will enable you to choose the size that we have just added.

Once you have click OK. You will then see that IntelliJ has added:

Image

 

The second way to do it is:

Click on Text at the bottom of the screen. You will then be shown the XML code for page.

You may find this hard to understand at the start if you have not done XML before.

At the top of the page you will see the TextView. This is where we want to add the background.

Take a new line under the last sentence you have and write:

android:textSize=”@dimen/size”

It should now look this this:

Image

Completed version:

 Image

 

5 thoughts on “How to set up a dimensions XML file

  1. Thanks for the post, really helped me understand more dimensions. I’ve only taken up learning Android a few days ago and will be following your blog to help me along.

  2. Fantastic post, I think people should acquire a great deal from this site its really user pleasant. A great deal fantastic information on here.

  3. android how to create dimens.xml file for separate values folder,
    I facing problem,I have to show a T-shirt image on different device resolution,but could not do this,some one give me idea that create dimen.xml for Imageview in different device resolution folder of Values,but how to do this and please solve my problems

    • I’m not really sure what you mean, but I’m guessing you mean that your image is being stretched on different resolution of screens. If so you should create an image for different resolutions and place them in the correct image folders. e.g. like xxhdpi.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s