Position text in Textviews and Edittexts with an image as background?

Asked

Viewed 1,303 times

1

I created a simple app that has only one Textview and a Edittext:

inserir a descrição da imagem aqui

As you can see changed the background of both, so far no problem:

inserir a descrição da imagem aqui

The problem appears when I insert texts, because they are poorly positioned in the layout both Textview and Edittext:

inserir a descrição da imagem aqui

Even in different ways...

Anyway, I’ve tried some ways to solve this, including selecting other alignments, but nothing of the behavior changes:

inserir a descrição da imagem aqui

I would like you to tell me how to make the text boxes act in a similar way to what I will display:

inserir a descrição da imagem aqui

I made a Nine-patch with the image present in the application:

inserir a descrição da imagem aqui

However already in the preview of the layout I was surprised because it appeared bugada:

inserir a descrição da imagem aqui

In the final result on android Emulator, was different, but equally bad:

inserir a descrição da imagem aqui

The original image has the following dimensions => 714x627... I made a rule of 3 because I wanted it to be 100 wide.

I put in the layout_width 100 and in the layout_heigth 88.

The value equivalent to 100 in height, It was 87 and a broken, because I made rule 3, I approached to 88. Even so, you shouldn’t present something so bizarre...

The solution was decrease the image size and import again to android studio, I took advantage to put 2 pixels extra width and height so there is no hack.

  • I should not have edited the question with the solution, but put as an answer.

2 answers

3


The way to achieve this behavior is the image of background be a Nine-patch bitmap.

A Nine-patch is a bitmap that Android will automatically resize to include the content of View to which it was attributed background.

It is a normal PNG (the image used as background) with an extra 1-pixel-thick edge where the adjustable zone is defined and the area where the content is placed.

inserir a descrição da imagem aqui
Source: Canvas and Drawables - Nine-patch.

  • The margin delimitations and Talz is even easy to do, but the image distorts completely

  • I decreased the size for what I set in textview and it turned out. obg!!!

  • Can you put the image in your question? I would like to try to make it not distort.

1

You will have to give a padding of, for example, 10 DP in editText.

android:padding="10dp"

To center, just use in xm ltatambém a:

android:gravity"center"

To break line into a certain number of characters, you will need to do this via Java and add an n to the string to break the line. This reply will help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.