Position text in TextViews and EditTexts with an image as background?

1

I've created a simple app that has only one TextView and one EditText :

Asyoucanseechangedthebackgroundofboth,sofarnoproblem:

TheproblemappearswhenIinserttext,asitispoorlypositionedinthelayoutofbothTextViewandEditText:

Evenindifferentways...

Anyway,I'vetriedsomewaystoworkaroundthis,includingselectingotheralignments,butnoneofthebehaviorchanges:

IwouldliketobeinformedhowtomakethetextboxesworksimilarlytowhatIwilldisplay:

Imadeanine-patchwiththeimageintheapplication:

ButalreadyinthepreviewofthelayoutIfoundstrangebecauseitappearedbugada:

Inthefinalresultintheandroidemulator,itwasdifferent,butalsobad:

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

I placed it in layout_width 100 and layout_heigth 88 .

The value equivalent to 100 in height, gave 87 and a broken because I made rule of 3, approached to 88 . Even so, I should not present something so strange ...

The solution was to resize the image and re-import it into android studio, I took 2 extra width and height pixels to cut it.

    
asked by anonymous 10.02.2017 / 00:53

2 answers

3

The way to achieve this behavior is the background image being a nine-bitmap patch .

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

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


Source: Canvas and Drawables - Nine-patch.

    
10.02.2017 / 16:14
1

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

android:padding="10dp"

To centralize, just use one of the following in xm:

android:gravity"center"

To break the line to a certain number of characters, you will have to do this via Java and add a \ n in the string to break the line. This answer will help you. / p>     

10.02.2017 / 13:04