TextView in alert dialog is not centered or formatted

317 Views Asked by At

I want to show a popup dialog with a text message but the formatting does not work.
I want it to be in the center of the popup and with specific font but neither is applied.
My code:

AlertDialog dialog = new AlertDialog.Builder(context).create();  
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);  
View view = LayoutInflater.from(mContext).inflate(R.layout.my_view, null);  
dialog.setView(view);  
TextView messageView = (TextView) view.findViewById(R.id.message);  
messageView.setTextIsSelectable(true);  
messageView.setText("This is the message to show");  
dialog.show(); 

And the layout:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_gravity="center_horizontal"  
    android:background="@color/red"  
    android:layout_width="match_parent"    
    android:layout_height="match_parent">  
    <TextView    
        android:id="@+id/message"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:fontFamily="sans-serif"  
        android:textSize="14sp"/>  
</LinearLayout>   

What is the problem here?

enter image description here

3

There are 3 best solutions below

1
On

create layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/red"
android:orientation="vertical">

<TextView
    android:id="@+id/message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"        
    android:textSize="14sp" />
</LinearLayout>

create dialog

AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create();
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom, null);
            dialog.setView(view1);
            TextView messageView = (TextView) view1.findViewById(R.id.message);
            messageView.setTextIsSelectable(true);
            messageView.setText("the is text demo my friend");
            dialog.show();

enter image description here

0
On
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="center_horizontal"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/message"
        android:text="asasasasas"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:fontFamily="sans-serif"
        android:textSize="14sp"/>
</LinearLayout>   
0
On
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="center_horizontal"
    android:background="@color/red"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:text="This is the message to show"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textSize="14sp" />
</RelativeLayout>

Try this. Hopefully this would solve your problem.