pengwang
pengwang

Reputation: 19946

how to put the hint at the right of the editView?

the default hint of the editView at the left of editView,can i put at the right of the editView,if there is some api i an use?could you give me some advice,thank you?

edit: when i used android:gravity="right",the hint can at the right of editView,but the focus also at the right of editView,i want the hint at right,but the focus is at the left,when i input something ,it can start form left. i used @Chirag method,but the hint also at the left,if the hint is fixed at the right,but i can input at the left?

Upvotes: 2

Views: 1515

Answers (2)

Navid
Navid

Reputation: 921

I think its more applicable to use textchange listener:

    edittext.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (edittext.getText().length() == 0)
            {
                edittext.setGravity(Gravity.RIGHT);
            }
            else {
                edittext.setGravity(Gravity.LEFT);
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

Upvotes: 1

Chirag
Chirag

Reputation: 56925

Please set edittext gravity as right :

<EditText android:id="@+id/search"
        android:layout_height="wrap_content" 
        android:layout_gravity="left"
        android:textSize="15sp"                             
        android:layout_marginTop="12dp" android:layout_marginLeft="36dp"
        android:layout_width="175dp" android:hint="Search"
        android:singleLine="true" android:textColor="@color/grey"
        android:imeOptions="actionSearch"
        android:gravity="right" />

search.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {   
            search.setGravity(Gravity.LEFT);
        }
    });

search.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) 
        {

            if ((event.getKeyCode() == 67)
                    && (event.getAction() == KeyEvent.ACTION_DOWN)) {
                try {
                    if (searchTrack.getText().toString().length() == 0) 
                   {
                           search.setGravity(Gravity.RIGHT);

                   }
                } catch (Exception e) {
                    // TODO: handle exception
                }

            }
            return false;
        }
    });

Upvotes: 3

Related Questions