align word wrap text in android button - android-layout

How can I align wraped text in a button ?
this is my button layout :
<Button
android:id="#+id/buttontest"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#color/white_botton"
android:drawableLeft="#drawable/message_button"
android:gravity="left|center_vertical"
android:text="Testing newline"
android:textSize="16sp" />
this is how it looks :
I want the new line to be aligned with the first line.
( If I try with a textview I dont have this issue )

<Button
android:id="#+id/buttontest"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#android:color/white"
android:drawableLeft="#drawable/message_button"
android:drawablePadding="5dp"
android:gravity="left|center_vertical"
android:text="Testing newline"
android:textSize="16sp" />
You can set width according to your need.
My image size isnt perfect using your image will be fine I guess

remove
android:gravity="left|center_vertical"
and Check
hope it will help you

Just add a android:drawablePadding to get some space between the icon and the text.

I guess there is something wrong somewhere else in my code, in a new clean project the alignment works. thank you for all your answers.

Related

Layout in Android Emulator looks different as compared to the layout file

I have recently started learning Android Development. I am using Android Studio and I created the XML layout file with it. But the layout is looking different on the emulator to the one I created on XML and I have no clue why it is happening.
This is the picture of my Main activity layout:
And this is the picture of my emulator:
The plain text should be below the text view but in the emulator, its position is different(I have marked the error by red).
Here is the code for the Main activity:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="208dp"
android:text="SUBMIT"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="Enter the value in kg below"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editTextNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
tools:layout_editor_absoluteX="100dp"
tools:layout_editor_absoluteY="73dp" />
I'm not sure why it is showing that way in the emulator versus the preview, but the reason that the EditText field isn't where you want it to be is because of how you have set the position of the editTextNumber field:
tools:layout_editor_absoluteX="100dp"
tools:layout_editor_absoluteY="73dp"
You are telling Android to position the field 100 pixels in and 73 pixels down from the top of the screen. Most than likely this occurred because you accidentally dragged the field slightly in the editor window, which specifies the exact pixel placement.
Instead, since you are using a ConstraintLayout, you'll want to set the location of the EditText in relation to other fields on your layout. For example, remove the two lines above and replace them with something like this:
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
I would also suggest that you name your fields on the layout with descriptions of what they represent, rather than the defaults of textView and textView2. For example, consider changing textView2 to something like weight_label_textview. It will make it much easier to know which field you are dealing with both in the layout and in your code.
As you are using constraint layout every UI item must constraint vertical and horizontally. Try adding constraints to your EditText.

how to add stroke on text

I can't find a way to put a stroke on text (on the text, not on the whole text view box) , the stroke should be around the letters.
is there any custom text view or library or drawing can be implemented to draw a stroke
<TextView
style="#style/Header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="150dp"
android:text="HELLO DROID"
/>
i expect the text to look like so , (the stroke color is red)
I tried below two ways for adding stroke to text :
First is add shadow to your TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="#android:color/holo_blue_light"
android:text="Hello"
android:shadowRadius="10"
android:shadowDx="1"
android:shadowDy="1"
android:shadowColor="#android:color/holo_red_dark"/>
The output of above code is:
Second I found GitHub project android-outline-textview.
Please follow this Link
For this you need to add StrokedTextView and related files to your project
after that add below Code in XML file:
<com.skd.stackdemo.StrokedTextView
android:id="#+id/stroke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="Hello"
android:textColor="#android:color/holo_blue_light"
android:layout_marginTop="30dp"/>
Add below code in Java file:
StrokedTextView stroke = findViewById(R.id.stroke);
stroke.setStrokeColor(Color.RED);
stroke.setStrokeWidth(1f);
The output of above code is:
I hope it works for you.

Android Keyboard, custom popuplayout in Nougat

i developed a keyboard that works without issue on MM and lower. By the way on Nougat i have a layout issue, check this images to understand better: http://imgur.com/a/IHfeZ.
The 1st image is on Nougat ( the popup isn't showed completely) the 2nd is like appeared on MM ( all the lines are showed correctly ).
I used this xml to customize the popup:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/aosp_pressed">
<android.inputmethodservice.KeyboardView
android:layout_gravity="bottom"
android:id="#android:id/keyboardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:keyPreviewLayout="#layout/preview"
android:keyBackground="#drawable/key_background"
android:keyTextColor="#fff"
android:background="#color/aosp_background"
android:keyTextSize="25sp"/>
<ImageButton android:id="#android:id/closeButton"
android:background="#color/aosp_pressed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:clickable="true"
android:src="#drawable/ic_close_black" />
</LinearLayout>
As you can see the issue is caused by the position of the popup that isn't showed outside the keyboard. How can i solve this issue?
Thanks in advance to everyone.
What is your #layout/preview like?
My theory is that in between Android 6 and 7, the time when wrap_content chooses it's size changed.
Before when using
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/xxxxx"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</LinearLayout> //PARTIAL CODE!
there were no problems in 6.0. The text view would expand to fit the text and then be centered.
However, in 7, the text gets displayed completely vertically suggesting that the initial LinearLayout was getting set to a width of 0 and not adjusting.
Try playing with the height in your #layout/preview, or in your closeButton. Set them to manual heights and see if things change accordingly!

Make same space between buttons in a HorizontalScrollView

This is a solution in order to add same space between views in the layout:
make same space between button in linearlayout
But let say we have many items and we need to put them in HorizontalScrollView for handset devices.
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="#+id/shareLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnRequestAPrescriptionRefill"
android:layout_marginTop="10dp" >
<Button
android:id="#+id/btnFacebook"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/btn_facebook" />
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />
...
</LinearLayout>
</HorizontalScrollView>
This solution works fine for handsets but in the tablet where we have a big screen size, the same space between buttons is not working. Buttons are just sticking together without no space that we created by:
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />
Is there any solution?
A scroll view adapts it's size to its content view (the linear layout) but when the screen is large enough you want it the other way around, you want the content view to adapt its size to the scroll view. This is a little contradictory.
The only way to do this is specifying a minWidth property for your LinearLayout.
android:minWidth="200dp"
This is a pain in the butt because you probably need to set it at runtime, or have many versions of your XML for different screen resolutions.
Maybe another approach for large screen resolutions would be providing a different XML without the scrollview.

set Layout xml in android

`
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/round" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/round" />
</RelativeLayout>
`When I set the image on right side over transparent image(that is defined in relative layout) through layout_alignParentRight then image is placed at right of mobile screen but if I give the actual size of the image then the secondry image is place at end of image but I don't want to hardcode width of image.
You should link the images of your layout for better understanding about your problem.
In First Imageview
SET android:layout_aliginparentleft="true"
I hope problem will be solve.

Resources