Hot questions for Using Android EditText in android view

Question:

I have an EditText (vertically scrollable) inside one of the fragments in a ViewPager (horizontally swipable).

By default, touch events inside the EditText can swipe the ViewPager but not scroll the EditText. By using the code below (which I don't really understand), touch events inside the EditText can scroll the EditText but not swipe the ViewPager:

editText.setOnTouchListener(new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        v.getParent().requestDisallowInterceptTouchEvent(! ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP));
        return false;
    }
});

How can I simultaneously allow swiping of the ViewPager and scrolling of the EditText?


Answer:

You can handle this according to the solution given here.

pager.setOnTouchListener(new View.OnTouchListener() 
{
       public boolean onTouch(View p_v, MotionEvent p_event) 
        {
               editText.getParent().requestDisallowInterceptTouchEvent(false);
           //  We will have to follow above for all scrollable contents
           return false;
        }
});

For the EditText, you will have to add this snippet.

editText.setOnTouchListener(new View.OnTouchListener() 
{
      public boolean onTouch(View p_v, MotionEvent p_event)
       {
          // this will disallow the touch request for parent scroll on touch of child view
           p_v.getParent().requestDisallowInterceptTouchEvent(true);
           return false;
       }
});

Hope it helps...

Question:

I'm trying to implement OnFocusChangeListener on an EditText. The problem is I'm getting this error

Class 'FacilityScreen' must either be declared abstract or implement abstract method 'onFocusChange(View, boolean)' in 'OnFocusChangeListener'

even though the method onFocusChange has been implemented in OnFocusChangeListener.

Here is my code

 public class FacilityScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener,View.OnFocusChangeListener{

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

        EditText editText = (EditText) findViewById(R.id.enter_location);
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View view, boolean hasfocus) {
                if(hasfocus){
                     //do something
                }
            }
        });
    }
}

What am I getting wrong?


Answer:

You need to @Override public void onFocusChange() method inside your activity

Than use like this editText.setOnFocusChangeListener(this)

Check this example

public class FacilityScreen extends AppCompatActivity implements  View.OnFocusChangeListener{

    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_facilty_screen);
        editText = (EditText) findViewById(R.id.enter_location);
        editText.setOnFocusChangeListener(this);

    }


    @Override
    public void onFocusChange(View view, boolean b) {
        if(view==editText){
            // perfom your action here
        }
    }
}

Question:

This is how I go about adding a border to a EditText. How can I go about adding a border only on 3 sides of a EditText, and define a different color and width to each of the borders?

EditText editText = new EditText(this);
editText.setText("Find");
editText.setWidth(555);

GradientDrawable border = new GradientDrawable();
border.setColor(0xFFFFFFFF);  // white background
border.setStroke(1, 0xFF000000);  // black border with full
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
    editText.setBackgroundDrawable(border);
} else {
    editText.setBackground(border);
}

My attempt below doesn't work:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FF0000" />
        </shape>
    </item>
    <item android:right="5dp">
        <shape android:shape="rectangle">
            <solid android:color="#FFFF" />
        </shape>
    </item>
    <item android:left="22dp">
        <shape android:shape="rectangle">
            <solid android:color="#746565" />
        </shape>
    </item>
</layer-list>

Vielen dank im voraus.


Answer:

Create a layer-list drawable and define 3 different rectangle shape with custom width and color, and hide 3 side of each shape to show only one side. like this:

my_edittext_border.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list android:paddingLeft="30dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="-1dp"
        android:left="-1dp"
        android:right="-1dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="#ff0000" />
        </shape>
    </item>

    <item
        android:bottom="-3dp"
        android:right="-3dp"
        android:top="-3dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="3dp"
                android:color="#11ee66" />
        </shape>
    </item>

    <item
        android:bottom="-5dp"
        android:left="-5dp"
        android:top="-5dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="5dp"
                android:color="#0000ff" />
        </shape>
    </item>
</layer-list>

Now set this drawable to your EditText background:

android:background="@drawable/my_edittext_border"

Question:

I'm trying to style text inEditText. I've managed to stytle the EditText itself, but not its content. How can I go about changing the font weight, font color, style etc for text held inside the EditText?

This is how I style the EditText view:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingBottom="10dp"
    android:paddingLeft="42dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp">
    <item android:bottom="3dp">
        <bitmap
            android:gravity="center_vertical|left"
            android:src="@drawable/ic_games_black_24dp" />
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="#37474f" />
        </shape>
    </item>
</layer-list>

Then I call it:

EditText editText = new EditText( mContext );
editText.setHint(" Find");
editText.setWidth(555);
editText.setBackgroundResource(R.drawable.add_new_bag_dr);

What is the best way to go about it? What options do I have?

UPDATE

How can I attach a style defined in an XML to a View?


Answer:

you can use the following:

editText.setTextColor("set the color you want");

Define your custom style in style.xml and then use

editText.setTextAppearance(R.style.yourCustomStyle);

Hope it helps!!!