Search code examples
javaandroidxmlandroid-layoutandroid-custom-view

Check input type of Custom View


My question is about Android/Java.

How can I check the input type of an custom view without creating an attr.xml?

My main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="wrap_content"
        android:inputType="textEmailAddress"
        android:layout_height="wrap_content"
        android:ems="10"/>

    <org.javaforum.input
        android:layout_width="wrap_content"
        android:inputType="textEmailAddress"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter your E-Mail"
        />

</LinearLayout>

My input.java:

import android.widget.*;
import android.content.*;
import android.util.*;
import android.view.*;
import android.view.LayoutInflater.*;
import android.content.res.*;

public class input extends TextView{
    public input(Context context, AttributeSet attr) {
        //How can check if input type are textEmailAddress?
        super(context,attr);
    }

    @Override
    public void onFinishInflate() {
        // this is the right point to do some things with View objects,
        // as example childs of THIS View object
    }
}

So I wanna know if the inputtype of my custom view was set to "textEmailAddress". How can I do this?


Solution

  • As long as you inherit your class from TextView, you can use getInputType() method, it will return an integer value, you can check if it is equal toTYPE_TEXT_VARIATION_EMAIL_ADDRESS

    public boolean isTextEmailAddress() {
            return getInputType() == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
        }