Android button text not aligned properly - java

I have a button and even though the text is centered on the layout preview, when I run the application it slides down. Any idea of what may be happening.
Button XML
<Button
android:id="#+id/increment_setup_activity"
android:layout_width="131dp"
android:layout_height="59dp"
android:background="#drawable/settings_panel"
android:text="+"
android:gravity="center"
android:textColor="#color/soft_color"
android:textSize="50sp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout3"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="#+id/
Android Studio Layout Preview
Running on my phone

Since you have a larger font size center align text (by default on a button) does not work try reducing the font size more info visit this stackoverflow answer

add it's:
<LinearLayout android:layout_width="match_parent"
android:id="#+id/linearLayout1"
android:gravity="center"
>
add your component here and button
</LinearLayout>

Related

Positioning icon half above layout

Recently, designers in my company liked creating half-above elements in layouts for Android apps. I've been struggling for a while trying to make these elements behave as good as possible, but I'm already tired of it. Is there any way of positioning views like on this image
with an assumption that if user touch row/card it became "checked" and this black dot icon became visible (second click makes it invisible of course).
You can do something like this:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toEndOf="#+id/circle_view"
android:background="#android:color/holo_blue_bright">
<android.support.v7.widget.CardView
android:id="#+id/second_view"
android:layout_width="100dp"
android:layout_height="match_parent"/>
</FrameLayout
<View
android:id="#+id/circle_view"
android:layout_width="16dp"
android:layout_height="16dp"
android:background="#android:color/holo_red_dark"
android:layout_marginEnd="-8dp"
android:layout_centerVertical="true"/>
</RelativeLayout>
second_view is endOf circle_view so if circle_view is gone second_view will align to startOf parent.
circle_view should have marginEnd set to negative value of its width divided by two

Can't set the background of my EditText

I have this in my fragment xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.gelasoft.answeringball.MainActivity$PlaceholderFragment"
android:background="#drawable/wg_blurred_backgrounds_14"
>
<ImageView
android:id="#+id/sphereIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/magicBallDescr"
android:src="#drawable/rsz_31mystic" />
<LinearLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/sphereIcon"
android:orientation="vertical" >
<TextView
android:id="#+id/txtAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/txtQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#string/textHint"
android:inputType="textMultiLine"
android:lines="6" />
<Button
android:id="#+id/btnAsk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btnAskQ"
android:paddingEnd="#dimen/activity_vertical_margin"
/>
</LinearLayout>
</RelativeLayout>
and here is how it looks:
Now I would like to set a different background image on the EditText, so I edited the xml and make it like:
<EditText
android:id="#+id/txtQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#string/textHint"
android:inputType="textMultiLine"
android:lines="6"
android:background="#drawable/background"/>
But Now the background I set on the EditText is taking the wholw LinearLayout space.
Here is how it looks in the editor after the changes:
Why is it taking the whole place and hiding the button?
What am I missing here? I'm sure it is something pretty small, but I'm not able to spot it.
I am not sure but i guest that your background image so big. you can try small width and height type image replace of that. Hopefully you can solve this problem. Best of luck!
You're suffering from an out of memory exception, as visible in the last few lines of your stacktrace.
The solution would be to reduce the size of the drawable being used as a background. It's just an EditText, and it's small size doesn't warrant a high resolution file as a background.
This can be done using a variety of image editing software, from Photoshop to Microsoft Paint.
With Paint:
Step 1: Open image in Paint.
Step 2: Click on the "Resize" button at the top, next to your toolbox.
Step 3: Reduce the size of your image to an appropriate value.
Step 4: Save the image in your drawable folders.
Step 5: If you're using Eclipse, right-click on your project and click on "Refresh"
Step 6: Launch your app again.
Now for the expansion of the EditText:
You're EditText height is set to wrap_content. When you set an image as it's background, that too becomes it's content. Hence, you're EditText bloats itself to accomodate it's new background.
A solution would be to hardcode the height like
android: layout_height="30dp" though the ability to use this depends on your requirements.

Android placing multiple bitmaps in imageview

I have multiple ImageViews which are transparent and are meant to over lap one another. I am aligning then to one another but one problem presist. How can I set the order in which they are layered? Ive tried revising the code in both xml and how the images are set in the actual java code...
I have tried FrameLayout but maybe Im not using it right...here is my implementation:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".RadarActivity" >
<ImageView
android:id="#+id/imageview_topo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:scaleType="center"
android:visibility="invisible" />
<ImageView
android:id="#+id/imageview_counties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:scaleType="center"
android:visibility="invisible" />
No matter what I do...imageview_topo is always placed on top. Even in the code i have instantated topo first and retrieved its image first!
Thank you!
I referred a site here that says:
If multiple child views exist, then they are drawn, in order, one atop
the other. This means that the first view added to the frame layout
will display on the bottom of the stack, and the last view added will
display on top.
You can check this site too: http://blog.neteril.org/blog/2013/10/10/framelayout-your-best-ui-friend/
May be i can help you more if you show the screenshot of your UI.

Android Padding not affecting layout

OK, Im starting to get the hang of how XML operates with the Android interface, but I've ran into a problem...
I just have a HorizontalScrollView with a RelativeLayout inside to which I will place other views later. The HorizontalScrollView as you can see is sitting above a button, and below another button. I like where its placed, but I dont want it exactly right above and below the buttons. I would like some spacing (padding). Ive tried adding padding but it seems to have no effect...
Heres my attempted XML:
<HorizontalScrollView
android:id="#+id/scrollview_xyzInfo"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_above="#+id/button_refresh"
android:layout_alignParentRight="true"
android:layout_below="#+id/button_mySettings"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:background="#drawable/refresh_selector" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</RelativeLayout>
</HorizontalScrollView>
Any help would be much appreciated :-)
Nevermind! Use margins when seperating two views like so:
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
Answer Found at:
Padding not working Android
Padding and margin works only with LinearLayout. Which layout are you using?

How to leave area for future dynamic activity?

I am trying to figure out how to make my layout have the following coded buttons stay in their place despite future dynamic activity, maybe through reserving a free space. In between the menu button and the other three buttons will be a grid of buttons with both random size and in random amounts based on preselections created dynamically. But no matter how many buttons or the sizes I want these four buttons designed in main.xml to remain in their proper positions.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="MENU" />
This is the area of concern. I want to make it where the menu button (above) is always attached to the top of the screen and the step, pause, and simulate buttons (below) are always at the very bottom of the screen.
<Button
android:id="#+id/step"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/grid"
android:layout_centerHorizontal="true"
android:text="STEP" />
<Button
android:id="#+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/grid"
android:layout_toLeftOf="#id/step"
android:text="PAUSE" />
<Button
android:id="#+id/sim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/grid"
android:layout_toRightOf="#id/step"
android:text="SIMULATE" />
<SeekBar
android:id="#+id/speed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/sim"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
I just figured out a solution my own question.
I had to use "android:layout_alignParentBottom="true" after the menu button on the other three buttons, now any buttons added dynamically can be placed in between as needed.
The website wont let me answer my own question.
Make your RelativeLayout a LinearLayout. Give all Buttons "android:layout_weight="0"". Then at the desired empty space, add a View that has android:layout_height="0" and "android:layout_weight="1"". That should do it, provided I understand your question correctly.

Categories