Showing posts with label EditText. Show all posts
Showing posts with label EditText. Show all posts

How to Align an EditText to the Bottom in a View

android:layout_alignBottom was added in API Level 1
Makes the bottom edge of this view match the bottom edge of the given anchor view ID. Accommodates bottom margin.

1. In your main.xml file, add a RelativeLayout.

2. Add an EditText to the main.xml file.

3. Add the below line between the <EditText> tags. This will move the EditText to the bottom of the view within its parent.

android:layout_alignParentBottom = "true"

4. Compile and run!

Next Recommended Article: android:gravity - Move Text inside a EditText (center, top, bottom, right, left, and more)

Resources:
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_alignBottom

setRawInputType - How to Display Soft Keyboard on EditText, in Java

public void setRawInputType (int type) was added in API level 3
Directly change the content type integer of the text view, without modifying any other state.
Related XML Attributes: android:inputType

1. Add a EditText in the main.xml file or Instantiate a new EditText object named myEditText.

2. Add the below line to the import section.

import android.text.InputType;

2. In the MainActivity.java file, add the code below. This will set the soft keyboard displayed to be numeric.

myEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER);

3. Compile and run!

4. Other InputType options are list on this page: developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBER

Resources:
http://developer.android.com/reference/android/widget/TextView.html#setRawInputType(int)
developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBER
http://stackoverflow.com/questions/9933574/edittext-setinputtype-vs-setrawinputtype

How to Set EditText Text Size, in Java

public void setTextSize (int unit, float size) was added in API Level 1
Set the default text size to a given unit and value. See TypedValue for the possible dimension units.
Related XML Attributes: android:textSize
Parameters:
unit - The desired dimension unit.
size - The desired size in the given units.
Size of the text. Recommended dimension type for text is "COMPLEX_UNIT_SP".

1. Add a EditText in the main.xml file or Instantiate a new EditText object named myEditText.

2. Add the below line to the import section.

import android.util.TypedValue;

2. In the MainActivity.java file, add the code below. This will set the TypedValue to SP, and the size to 48.

myEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 48); 

3. Compile and run!
4. Other TypedValue options are:

COMPLEX_UNIT_DIP - value is Device Independent Pixels
COMPLEX_UNIT_IN - value is in inches
COMPLEX_UNIT_MM - value is in millimeters
COMPLEX_UNIT_PT - value is in points
COMPLEX_UNIT_PX - value is raw pixels
COMPLEX_UNIT_SP - value is a scaled pixel

Resources:
http://developer.android.com/reference/android/widget/TextView.html#setTextSize(int, float)
http://developer.android.com/reference/android/R.attr.html#textSize
http://developer.android.com/reference/android/util/TypedValue.html
http://alvinalexander.com/java/jwarehouse/android/core/java/android/content/res/Resources.java.shtml
http://stackoverflow.com/questions/9494037/how-to-set-text-size-of-textview-dynamically-for-diffrent-screens

How to Set the Hint on an EditText, in Java

public final void setHint (int resid) was added in API level 1
Sets the text to be displayed when the text of the EditText is empty, from a resource.

1. Find EditText by Id or Instantiate a new EditText.

2. In the MainActivity.java file, add the code below to the onCreate method 

myEditText.setHint("Type to create event"); 

3. Compile and run!

Resources:

findViewById - How to Find an EditText using ID

findViewById was added in API Level 1
Look for a child EditText with the given id. If this view has a given id, return this EditText.

1.In the main.xml file,  Name an EditText, myEditText.

2. In the MainActivity.java file, Declare an EditText named myEditText.

3. In MainActivity, java file, add the below highlighted code to the onCreate method. Where R.id.myEditText is the same as the android:id name in your main.xml file, and myEditText is the EditText name referred to in Java.

myEditText = (EditText) findViewById(R.id.myEditText); 

4. Compile and run!

Resources:

android:id - How to name an EditText

android:id was added in API Level 1
This is a unique identifier name for the EditText so you can refer to the EditText later in your project.

1. Add an EditText in your main.xml file.

2. In the main.xml file, add the code below.

  <EditText
        ...
       android:id = "@+id/myEditText"
  />

3. The "+" character is required if you have not yet defined that id, if perhaps the element you reference is lower in the .xml file.

Otherwise you if it's already defined above (or included) then you can omit the "+" char:

4. Compile and run!
Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:id
http://developer.android.com/reference/android/R.attr.html#id

How to Add a Widget/Custom View to an AlertDialog

public AlertDialog.Builder setView (View view) was added in API level 1
Set a custom view to be the contents of the Dialog.
Parameters: view - The view to use as the contents of the Dialog.
Returns: This Builder object to allow for chaining of calls to set methods


1. Create an AlertDialog with Supplied Arguments named myAlertDialogBuilder.

2. In the showAlertDialog method, instantiate the object(s) you would like to add to the AlertDialog. In this example I will Instantiate a new EditText named myEditText, after the AlertDialog is instantiated. Other example items you might instantiate are an ImageView, CheckBox,or a LinearLayout to include many widgets.

3. Add the below line to add the EditText to the AlertDialog. This example below will add the EditText myEditText to the Alertdialog named myAlertDialogBuilder.

myAlertDialogBuilder.setView(myEditText);

4. Compile and run!
Soft keyboard pop-ups when user presses EditText

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setView(android.view.View)
http://www.codeofaninja.com/2011/07/android-alertdialog-example.html

How to Instantiate a new EditText

EditText was added in API Level 1

1. Declare an EditText.

2. After you declare the EditText object, add this line below to instantiate it. This will instantiate a new EditText named myEditText.

myEditText = new EditText(this);

3. Compile and run!

Resources:
www.codeofaninja.com/2011/07/android-alertdialog-example.html

How to Declare an EditText

EditText was added in API Level 1
EditText is a thin veneer over TextView that configures itself to be editable.

1. In the MainActivity.java file, add the below import to the imports

 import android.widget.EditText;

2. In a method such as onCreate, another method, or created as a class variable, add the line below to declare an EditText variable named myEditText.

EditText myEditText;

3. Compile and run!

Next Recommended Article: How to Instantiate a new EditText

Resources:
http://developer.android.com/reference/android/widget/EditText.html


android:scrollHorizontally - Scroll Text in an EditText

How to Scroll Text with finger in an EditText

added in API Level 1

Text wider than the EditText can be scrolled horizontally. Put your finger on the text and move text from side to side to read.

1. Add an EditText in the main.xml file.

2. In the main.xml file, add the code below.

<EditText
        ...
android:scrollHorizontally = "true"
/>

3. Compile and run!
http://developer.android.com/reference/android/widget/TextView.html#attr_android:scrollHorizontally
http://developer.android.com/reference/android/R.attr.html#scrollHorizontally

android:rotationY - Rotate a TextView/EditText on the y axis

How to Rotate a TextView/EditText on the y axis

added in API Level 11

rotation of the TextView/EditText around the y axis, in degrees. Rotates both the text and the TextView object.

To imagine how the rotationY works, take a spiral notebook and hold it directly in front of you, vertically, with spiral on left. You will see it looks like an exact rectangle. This is how a rotationY = 0 looks like.

Now slowly rotate the notebook backwards, while keeping the spiral edge in the same location. Move it about 45 degrees backwards. The paper will look like a sideways trapezoid from your perspective. The back edge will look shorter and the sides will move inward. (Like looking at railroad tracks, the tracks look like they are moving inwards towards each other.) This looks like a sideways trapezoid.

The closer you move it to 89 degrees, the smaller in height the sideways trapezoid will get and the closer the sides will appear.

At 90 degrees you would not be able to see what is on the paper, looking directly at it. It would be completely sideways.

Now, past 90 degrees, imagine that notebooks spiral is a clear piece of plastic with text on it. When the notebook is turned past 90 degrees, the text will turn upside, and appear backwards to you. However, now it looks like a inverted sideways trapezoid, up until 180 degrees.

At 180 degrees, it will be a square and the text upside down and backwards.

181 degrees to 269 degrees, the text will appear upside down and backwards, in a sideways trapezoid form.

At 270 degrees,  is like 90 degrees, it will be completely horizontal and you would not be able to see it.

271 to 359 degrees, text is right side up again, with a sideways trapezoid. The lower the number, the small in height the sideways trapezoid will appear.

If set to (175-185) degrees, will make the text upside down, and backwards. To read the text you would need a mirror.

1. Add a TextView in your main.xml file.

2. Ensure to change the minSDKVersion to 11, or greater.

3. In the main.xml file, add the code below. This will rotate the TextView 45 degrees.

  <TextView
        ...
       android:rotationY = "45"
  />

4. Compile and run!

Related Articles:
Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:rotationY
http://developer.android.com/reference/android/R.attr.html#rotationY

android:rotationX - Rotate a TextView/EditText on the x axis

How to Rotate a TextView/EditText on the x axis

added in API Level 11

rotation of the TextView/EditText around the x axis, in degrees. Rotates both the text and the TextView object.

To imagine how the rotationX works, take a spiral notebook and turn it on its side with the spiral on the bottom. Hold it directly in front of you, vertically, with the spiral on bottom. You will see it looks like an exact rectangle. This is how a rotationX = 0 looks like.

Now slowly rotate the notebook backwards, while keeping the spiral edge in the same location. Move it about 45 degrees backwards. The paper will look like a trapezoid from your perspective. The back edge will look shorter and the sides will move inward. (Like looking at railroad tracks, the tracks look like they are moving inwards towards each other.) This looks like a trapezoid.

The closer you move it to 89 degrees, the smaller in height the trapezoid will get and the closer the sides will appear.

At 90 degrees you would not be able to see what is on the paper, looking directly at it. It would be completely sideways.

Now, past 90 degrees, imagine that notebooks spiral is a clear piece of plastic with text on it. When the notebook is turned past 90 degrees, the text will turn upside, and appear backwards to you. However, now it looks like a inverted trapezoid, up until 180 degrees.

At 180 degrees, it will be a square and the text upside down and backwards.

181 degrees to 269 degrees, the text will appear upside down and backwards, in a trapezoid form.

At 270 degrees,  is like 90 degrees, it will be completely horizontal and you would not be able to see it.

271 to 359 degrees, text is right side up again, with a trapezoid. The lower the number, the small in height the trapezoid will appear.
 
If set to (175-185) degrees, will make the text upsize down, and backwards. To read the text you would need a mirror.

1. Add a TextView in your main.xml file.

2. Ensure to change the minSDKVersion to 11, or greater.

3. In the main.xml file, add the code below. This will rotate the TextView 45 degrees.

  <TextView
        ...
       android:rotationX = "45"
  />

4. Compile and run!

Related Articles:
Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:rotationX
http://developer.android.com/reference/android/R.attr.html#rotationX

android:paddingTop - Set the Padding on the Top Edge of a TextView/EditText

How to Set the Padding on the Top Edge of  a TextView/EditText

added in API Level 1

Sets the padding, in pixels, of the top edge. Padding is defined as space between the edges of the view and the view's content. A view size will include it's padding.

1. Add a TextView in your main.xml file.

2. In the main.xml file, add the code below.

  <TextView
        ...
       android:paddingTop = "50sp"
  />

3. Compile and run!

Related Articles:
Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:paddingTop
http://developer.android.com/reference/android/R.attr.html#paddingTop

android:paddingStart - Set the Padding at the Start Edge of a TextView/EditText

How to Set the Padding at the Start Edge of  a TextView/EditText

added in API Level 17

Sets the padding, in pixels, at the start edge. Padding is defined as space between the edges of the view and the view's content. A view size will include it's padding.

1. Add a TextView in your main.xml file.

2. Ensure the minSDKVersion is, 17 or greater.

3. In the main.xml file, add the code below.

  <TextView
        ...
       android:paddingStart = "50sp"
  />

4. Compile and run!

Related Articles:
Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:paddingEnd
http://developer.android.com/reference/android/R.attr.html#paddingEnd

android:paddingRight - Set the Padding on the Right Edge of a TextView/EditText

How to Set the Padding on the Right Edge of  a TextView/EditText

android:paddingRight was added in API Level 1

Sets the padding, in pixels, of the right edge. Padding is defined as space between the edges of the view and the view's content. A view size will include it's padding.

1. Add a TextView in your main.xml file.

2. In the main.xml file, add the code below.

  <TextView
        ...
       android:paddingRight = "50sp"
  />

3. Compile and run!

Related Articles:
Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:paddingRight
http://developer.android.com/reference/android/R.attr.html#paddingRight

android:paddingLeft - Set the Padding on the Left Edge of a TextView/EditText

How to Set the Padding on the Left Edge of  a TextView/EditText

android:paddingLeft was added in API Level 1

Sets the padding, in pixels, of the left edge. Padding is defined as space between the edges of the view and the view's content. A view size will include it's padding.

1. Add a TextView in your main.xml file.

2. In the main.xml file, add the code below.

  <TextView
        ...
       android:paddingLeft = "50sp"
  />

3. Compile and run!

Related Articles:
Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:paddingLeft
http://developer.android.com/reference/android/R.attr.html#paddingLeft

android:paddingEnd - Set the Padding on the Edge of a TextView/EditText

How to Set the Padding on the Edge of  a TextView/EditText

android:paddingEnd was added in API Level 17

Sets the padding, in pixels, of the end edge. Padding is defined as space between the edges of the view and the view's content. A view size will include it's padding.

1. Add a TextView in your main.xml file.

2. Ensure the minSDKVersion is, 17 or greater.

3. In the main.xml file, add the code below.

  <TextView
        ...
       android:paddingEnd = "50sp"
  />

4. Compile and run!

Related Articles:
Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:paddingEnd
http://developer.android.com/reference/android/R.attr.html#paddingEnd

android:paddingBottom - Set the Bottom Padding on a TextView/EditText

How to Set the Padding on a TextView/EditText

android:paddingBottom was added in API Level 1
Sets the padding, in pixels, of the bottom edge. Padding is defined as space between the edges of the view and the view's content. A view size will include it's padding.

1. Add a TextView in your main.xml file.

2. In the main.xml file, add the code below.

  <TextView
        ...
       android:paddingBottom = "50sp"
  />

3. Compile and run!

Related Articles:
Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:paddingBottom
http://developer.android.com/reference/android/R.attr.html#paddingBottom

android:padding - How to Set the Padding on a TextView/EditText

android:padding was added in API Level 1
Sets the padding, in pixels, of all four edges. Padding is defined as space between the edges of the view and the view's content. A view size will include it's padding.

1. Add a TextView in your main.xml file.

2. In the main.xml file, add the code below.

  <TextView
        ...
       android:padding = "10dp"
  />

3. Compile and run!

Related Articles:
Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:padding
http://developer.android.com/reference/android/R.attr.html#padding


android:longClickable - Disable Long Click for EditText

How to Disable Long Click for EditText

android:longClickable was added in API Level 1

Defines whether the EditText reacts to long click events.
This will disable the capability for (Edit text, Select word, Select all, Paste) and/or (Edit Text, Select word, Select all, Paste, Input Method) pop-up selections to appear when long-clicking the EditText.

1. Add a EditText  in your main.xml file.

2. In the main.xml file, add the code below.

  <EditText
        ...
       android:longClickable = "false"
  />

3. Compile and run!

Related Articles:
Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:longClickable
http://developer.android.com/reference/android/R.attr.html#longClickable