Note that there are some explanatory texts on larger screens.

plurals
  1. POCenter text in EditText not working programmatically when adding background resource
    primarykey
    data
    text
    <p>I am working on an app that inserts an EditText box programmatically onto the layout. I cannot seem to get the center text working when I add the EditText programmatically but it works fine when I do it through the XML layout. I have nailed down the problem to be when I add the background resource. I have added code below to show both EditText when added through code and also XML. </p> <pre><code>&lt;LinearLayout android:id="@+id/layoutLinear_main" 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:orientation="vertical" android:background="@drawable/background" &gt; &lt;LinearLayout android:id="@+id/layoutLinear_answerDisplay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" android:orientation="vertical" &gt; &lt;EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingRight="5dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:gravity="center" android:background="@drawable/background_field" android:layout_gravity="center" android:layout_marginLeft="120dp" android:layout_marginRight="120dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:textSize="30dp" android:hint="F" android:text="F" /&gt; &lt;/LinearLayout&gt;&lt;/LinearLayout&gt; </code></pre> <p>Then within the main code. I add an EditText programmatically. I've removed the margins and paddings, just to clearly identify the issue.</p> <pre><code>LinearLayout layoutLinear_answerDisplay = ((LinearLayout) this.findViewById(R.id.layoutLinear_answerDisplay)); EditText ed = new EditText(this); ed.setGravity(Gravity.CENTER); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); layoutParams.gravity = Gravity.CENTER; ed.setLayoutParams(layoutParams); ed.setBackgroundResource(R.drawable.background_field); ed.setTextSize(30); ed.setHint("F"); ed.setText("F"); layoutLinear_answerDisplay.addView(ed); </code></pre> <p>The following is then the ouptut. You can see from the image that the top EditText has centered text (this was done through the XML code). You can see from the bottom EditText, that the text is off centered (done through the code behind). </p> <p><img src="https://i.stack.imgur.com/RfqDn.png" alt="enter image description here"></p> <p>I have also attached my background resource for the EditText. Any help would be greatly appreciated. Thanks</p> <p><img src="https://i.stack.imgur.com/xhwlS.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload