Noreen Khan
Noreen Khan

Reputation: 233

how to set EditText hint dynamically?

Below is my activity. I am calling this activity via Intent on click on Two diffrent buttons. Now what I want that when I open Below activity on click on first button then I want to show hint in edittext "Po No new" and when I open below activity on click on second button then I want to show hint in edittext "Po No old". How can I acieve this ?

Xml file -

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:text="Po no."
                    android:textColor="@color/textColor"
                    android:textSize="13sp"
                    android:textStyle="bold" />

                <EditText
                    android:id="@+id/txtPoNo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:completionThreshold="3"
                    android:imeOptions="actionDone"
                    android:inputType="number"
                    android:nextFocusDown="@+id/txtMeterSrMo"
                    android:singleLine="true"
                    android:textSize="14sp" />

             </LinearLayout>
</ScrollLayout>

Activity file -

public class InstallationDashBoardActivity extends AppCompatActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.categories_activity);

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

    }
}

Button Click -

installation.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(ServiceCategories.this,InstallationDashBoardActivity.class);
            intent.putExtra("isNew", true);
            startActivity(intent);


        }
    });

replacement.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent intent = new Intent(ServiceCategories.this,InstallationDashBoardActivity.class);
            intent.putExtra("isNew", false);
            startActivity(intent);
        }
    });

I place below Code in my fragment onCreateView -

//Getting Extras
        if(getActivity().getIntent().getExtras()!=null){
            isNew = getActivity().getIntent().getExtras().getBoolean("isNew");
        }

        // Added condition here
        if(isNew) {
            txtPoNo.setHint("Po No New");
        }
        else {
            txtPoNo.setHint("Po No Old");
        }

Upvotes: 2

Views: 3539

Answers (3)

Sachin Suthar
Sachin Suthar

Reputation: 692

  editText.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            editText.setHint("");
            return false;
        }

    });

    editText.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus){
                editText.setHint("Hint");
            }
        }
    });

Upvotes: 0

Dileep Patel
Dileep Patel

Reputation: 1988

@ Moin Khan try this , hope this can help you

installation.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(ServiceCategories.this, InstallationDashBoardActivity.class);
            intent.putExtra("key", "value");
            startActivity(intent);
        }
    });

    replacement.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(ServiceCategories.this, InstallationDashBoardActivity.class);
            startActivity(intent);
        }
    });

and in second activity

 Intent intent2 = getIntent();
    String strKey = intent2.getStringExtra("key");
    if (strKey.equalsIgnoreCase("value")) {
        txtPoNo.setHint("Po No New");
    } else {
        txtPoNo.setHint("Po No Old");
    }

Upvotes: 1

Jai
Jai

Reputation: 1984

Call

txtPoNo.setHint("Po No Old");

Update

You meant you want to detect which button is clicked and according to that you want to show hint right? For that very simple pass one boolean flag with intent and try to identify using that boolean value

installation.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(ServiceCategories.this,InstallationDashBoardActivity.class);

                //Added flag here
                intent.putExtras("isNew", true);
                startActivity(intent);


            }
        });

replacement.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(ServiceCategories.this,InstallationDashBoardActivity.class);

                //Added flag here
                intent.putExtras("isNew", false);
                startActivity(intent);
            }
        });

Get boolean value in your activity

public class InstallationDashBoardActivity extends AppCompatActivity {

//boolean added
public boolean isNew;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.categories_activity);

            //Getting Extras
            if(getIntent().getExtras()!=null){
               isNew = getIntent().getExtras().getBoolean("isNew");
             }

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

            // Added condition here
            if(isNew)
              txtPoNo.setHint("Po No New");
            else
               txtPoNo.setHint("Po No Old");

        }
    }

Upvotes: 0

Related Questions