skip to Main Content

I’m creating an app and i want the text to allign under a button, it looks like this:
enter image description here

so i want the large text starting on the left at(5dp) under the button and end at(155dp from the left) the end of the button. And if the text is too long to fit under the button, i don’t want it to go in the next line, i want it to cut of and put in “..” at the end so, that it looks like this f.e. “Large T..”, i photoshoped that picture real quick to show you what i mean:

enter image description here

I’m kind of new to xml and the whole ndroid app scene, so any help is appreciated 🙂

2

Answers


  1. Use LinearLayouts with layout_wight. Example code below:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@android:drawable/ic_menu_save"/>
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@android:drawable/ic_menu_save"/>
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@android:drawable/ic_menu_save"/>
    
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:orientation="horizontal">
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="LOOOOOOOOOOOOOOOOOOOOONG"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="LOOOOOOOOOOOOOOOOOOOOONG"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="LOOOOOOOOOOOOOOOOOOOOONG"/>
    
    
    </LinearLayout>
    

    Result will be:

    enter image description here

    Login or Signup to reply.
  2. In your TextView add these properties:

    android:ellipsize="end"
    android:maxEms="8"
    android:singleLine="true"
    

    NOTE: You can adjust the ems size to how many chars you want to show.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search