This is easily the weirdest problem I've ever faced.
So today, I created a new Android Project. The first layout xml had just a textview that has the following properties (its inside a relative layout):
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="124dp"
android:background="#80ffffff"
android:gravity="center_vertical"
android:text="Welcome"
android:textSize="48dp"
android:textStyle="bold" />
What I wanted was to have a textview that has text in its center. But the text STARTS from the center and goes out of screen towers the right side.
So I thought I must be doing something wrong. I went into one of my older projects (it was also open in eclipse). I opened an XML there, and shockingly, all the text there has also moved to the left (It starts from the center, rather than actually centering the text). It was fine yesterday. Here's the button:
<Button
android:id="#+id/gettingstarted"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/login"
android:layout_below="#+id/login"
android:layout_marginTop="10dp"
android:text="Getting Started"
android:textColor="#ffffff" />
I have no idea what happened. Why is the text no longer centered in buttons in all my projects? I'm in a really tough spot right now. Kindly help.
For your first problem try
android:gravity="center_vertical|center"
but for your text views in your all projects , it's a general question , maybe it's occured because of changing in your apps theme or projects styles
Related
So, the title may be confusing, but to put it in context:
I have an ImageButton component in my app display that should be on the opposite side of my ImageView, lined up perfectly, and to repeat this for any other item on the same list.
My question and doubt itself is how to do this when the ImageButton in this case is inside a LinearLayout while the ImageView is outside of it. Is there like a code line that can connect an outside element to a LinearLayout inside element.
Here's how my display looks:
item_contact.xml
The blue rectangle, being the LinearLayout, means everything inside belongs to it, but the call button looks to be the on the other side of the user pic, which is not inside the LinearLayout, which is what I wanted, the problem is this:
activity_main.xml
Each of the TextViews are above and below both the user picture and the call button, but because they both also belong to the LinearLayout, it's obvious it's because of the latter.
Now I tried fixing the problem by simply adding the user picture to the LinearLayout, which seems like the obvious answer, but I needed to be sure, because my call button isn't correctly aligned to the right:
<ImageView
android:id="#+id/iv_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="16dp"
android:contentDescription="#string/todo"
android:src="#drawable/ic_baseline_person_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="ImageContrastCheck" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#id/iv_image"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textColor="#color/purple_700"
android:textSize="18sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/call_button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginStart="248dp"
android:background="#1072E1"
android:contentDescription="#string/call"
android:minWidth="48dp"
android:src="#android:drawable/stat_sys_phone_call" />
<TextView
android:id="#+id/tv_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="14sp" />
</LinearLayout>
My ImageButton's "right alignment" was only done with the manual positioning of the android:layout_marginStart which I set to 248dp by attempts. Surely there's a better way to do this, but I've yet to find a solution that works.
Lastly, there's also the aforementioned problem: I can't seem to fix the LinearLayout issue without having to backtrack the progress I've done and use new code lines or already existing ones to resolve the issue.
So:
How can I "connect" two components, one inside a LinearLayout and another not, with or without the use of constraints and/or RelativeLayouts, and do I really have to insert the outside component to the LinearLayout;
How can I forcefully align a component to the right without having to manually push it with margin inputs (attempts);
Is there a way to know exactly how to connect a component to another by using constraints accurately, so it repeats that process through the entire contact list?.
I would really like if someone could answer at least one of the listed questions or, even better, resolve one of the most developed questions, where I show some images of reference.
Maybe even a suited tutorial for my case in specific that explains the ways of designing the layout of an app.
If more information is needed, I will gladly help, and I'll also try to answer to every response I can get.
Also, in case you're curious, here's my last question about the same app (already resolved):
"[AppName] stopped working" when I try to run my app on an AVD emulator
(Keep in mind that the problem at hand in this question has barely anything to do with this one.)
Both properties are set programmatically. I tried restarting, changing AVD - bu still when TextView background is set - text dissapears. And yes, it has different color then background. Without background text is visible. I don't know where is the problem...
{This code below works fine}
<TextView
android:id="#+id/txt_Number"
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center"
android:text="1"
android:textColor="#color/red"
android:textSize="10sp"
android:textStyle="bold" />
This code below show only background, no text :/
<TextView
android:id="#+id/txt_Number"
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center"
android:text="1"
android:background="#drawable/txtbg"
android:textColor="#color/red"
android:textSize="10sp"
android:textStyle="bold" />
The image is in png format with transparent background. TextView is inside a TableRow in TableLayout. This Layout is part of the ListView item - but I don't think it's the problem.
With background:
Without background:
In fact, there are 2 TextViews, one showing distance and the second numbers - the first one has no background. When I try to put a background to the second one - both texts dissapear.
I faced with the same issue. After some researching I still don't know reasons to provide to this situation. To solve it, I simply put a View with desirable background below TextView. Not pretty but works.
I am working with modified version of sample WeatherListWidget to get a better understanding of App Widgets. Things are fine - except when I try to replace the dark_widget_item and light_widget_item layout files with slightly more complex layout files. Here is original layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget_item"
android:layout_width="match_parent"
android:layout_height="46dp"
android:paddingLeft="25dp"
android:gravity="center_vertical"
android:background="#drawable/item_bg_light"
android:textColor="#e5e5e1"
android:textSize="24sp" />
I would like to be able to have multiple text lines. But:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget_item"
android:background="#drawable/item_bg"
android:layout_width="match_parent"
android:layout_height="46dp"
android:paddingLeft="25dp">
<TextView android:id="#+id/type_string"
android:textColor="#666666"
android:textSize="20sp" />
<TextView android:id="#+id/title_string"
android:textColor="#666666"
android:textSize="18sp" />
</LinearLayout>
fails.
In fact, it results in "Sorry! The application Launcher (process com.android.launcher) has stopped unexpectedly. Please try again. Force close".
Reinstating TextView widget_item.xml fixes this. I suspect that part of the problem is how I reference RemoteViews in WeatherWidgetService.getViewAt() - but I am getting very little help from DDMS or LogCat or anything else.
Thanks guys, I got the notification. (SO requires a username with length > 2, hence the dot)
Answer as per comment:
I don't see any layout_width and layout_height attributes for both of the TextViews in your LinearLayout - they are mandatory. Also, if you want the two TextViews to be above eachother, add android:orientation="vertical" to the LinearLayout. And just to the record, you can break a CharSequence to multiple lines in a single TextView by adding "\n" inbetween the different elements.
If you're going to include an image as well, then you're probably better off with a LinearLayout than a single TextView indeed, although you could potentially use the intrinsic drawable option of the latter. That could get a little messy though, especially if you're planning on using different styles for the different lines of text... Not impossible, but I'd stick with the LinearLayout. ;)
When im designing my android app im using XML and for this app im using the Relative layout but when i put the buttons on my screen with the png background i made its just looking bad.. the buttons are not put equaly like they should..
Look at this picture:
How would i solve so the pluss buttons and percent and comma and equal button will be placed right instead of looking all that weird?
If you are intrested to see my XML code here is an link for pastebin:
http://pastebin.com/gpxnPT4P
I think you have to check the android:layout_... of every item that is showing wrong. For example, the declaration for the number 5 button is:
<Button android:background="#drawable/number5" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:id="#+id/number5" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/number8" android:layout_alignLeft="#+id/divided"></Button>
If you look closely the android:layout_alignLeft declaration is pointing to de divide button (#+id/divided) that is far away. There is a similar situation with the button for number 6.
Try this declaration for number 5 button:
<Button android:background="#drawable/number5" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:id="#+id/number5" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/number8" android:layout_alignLeft="#+id/number4"></Button>
Hope this helps
I'm having an issue in one of my activities with text being cut-off at the first letter.
The issue is, whenever navigating away from this activity to another (via the tabs at the top), and then going back to this activity, all of the text except the first letter in all of the textviews gets cut-off:
The funny thing is that every time I repeat this process (leaving the activity and going back), the text comes back, gets cut-off, comes back, gets cut off....repeating.
My table layout is defined as follows:
<TableLayout
android:id="#+id/statisticsMiddleTableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/statisticsMiddleTextTitleLayout"
android:stretchColumns="1">
With all rows having the following structure:
<TableRow
android:id="#+id/tableRow1Statistics">
<!-- TitleRow -->
<TextView
android:id="#+id/statisticsTableR1CLText"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="#string/statisticsTableR1CLText"
android:textStyle="bold"
android:textColor="#FF6600"
android:paddingTop="0dip"
android:layout_margin="0dip"
android:gravity="center_horizontal"
android:layout_weight="1"/>
<TextView
android:id="#+id/statisticsTableR1CRText"
android:text="#string/statisticsTableR1CRText"
android:textStyle="bold"
android:textColor="#FFFF66"
android:gravity="center_horizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:paddingTop="0dip"
android:layout_margin="0dip"
android:layout_weight="1"/>
</TableRow>
I have found that setting "android:layout_width" to something other than "0dip" for the textviews solves this problem and the text doesn't get cut-off, but then I can't get an equal width between the textviews. I'd have to rebuild the entire activity UI without a table, which I don't want to do.
The more interesting factor is that this does not happen on a real device (I only have one to test it on though) and I had never seen this bug before upgrading the android SDK tools to Revision 10.
So now I wonder, is this a bug in the SDK revision 10 tools, or should I rework the entire UI for this activity? Has anyone else experienced this issue with using "0dip" for the "layout_width" textview attribute inside of a table?
I would forget about the SDK tools and just worry about the emulator and devices. The majority of my layouts do not render correctly in the layout editor.