Set hint in EditText in android programmatically

Add hint in editText through programming on button click.

Application developer can set editText hint via MainActivity.java programming file this feature called as dynamically setting up hint text on button click function. So here is the complete step by step tutorial for Set hint in edittext in android programmatically.

android-project-download-code-button

How to Set hint in EditText in android programmatically.

Code for MainActivity.java file.

 package com.android_examples.com.edittexthintprogrammatically;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

 EditText ediTextBox;
 Button HintSetButton;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 ediTextBox = (EditText)findViewById(R.id.editText1);
 HintSetButton = (Button)findViewById(R.id.button1);
 
 HintSetButton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 ediTextBox.setHint("Hint via programming");
 
 }
 }); 
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.edittexthintprogrammatically.MainActivity" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:ems="10" >

 <requestFocus />
 </EditText>

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="18dp"
 android:text="Click Here to Set editText Hint programmatically" />

</RelativeLayout>

Screenshot:

Set hint in edittext in android programmatically

Click here To Download Set hint in edittext in android programmatically project.

Leave a Reply

Your email address will not be published. Required fields are marked *