Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:19:55+00:00 2026-06-15T03:19:55+00:00

MainActivity public class MainActivity extends Activity { // Declare our Views, so we can

  • 0

MainActivity

   public class MainActivity extends Activity {
// Declare our Views, so we can access them later
private CheckUsernameEditText etUsername;
private EditText etPassword;
private EditText etPassword2;
private Button btnRegister;
private Button btnCancel;
private TextView lblUserStatus;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set Activity Layout
    setContentView(R.layout.activity_main);

    // Get the EditText and Button References
    etUsername = (CheckUsernameEditText) findViewById(R.id.username);
    etPassword = (EditText) findViewById(R.id.password);
    etPassword2 = (EditText) findViewById(R.id.password2);
    btnRegister = (Button) findViewById(R.id.register_button);
    btnCancel = (Button) findViewById(R.id.cancel_button);
    lblUserStatus = (TextView) findViewById(R.id.userstatus);

    // Set our new Listener to the Username EditText view
    etUsername.setOnUsernameAvailableListener(new OnUsernameAvailableListener() {
                @Override
                public void onAvailableChecked(String username,
                        boolean available) {
                    // Handle the event here
                    if (!available) {
                        etUsername.setTextColor(Color.RED);
                        lblUserStatus
                                .setText(username
                                        + " is already taken. Please choose another login name.");
                    } else {
                        etUsername.setTextColor(Color.GREEN);
                        lblUserStatus.setText(username + " is available.");
                    }
                }
            });

    // Set Click Listener
    btnRegister.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // create Account
        }
    });
    btnCancel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Close the application
            finish();
        }
    });
}

The corresponding XML

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

           *
           *
<EditText
    android:id="@+id/username"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="true" />

           *
           *
</LinearLayout>

CheckUsernameEditText

   public class CheckUsernameEditText extends EditText implements OnKeyListener {

OnUsernameAvailableListener onUsernameAvailableListener = null;
final private static String[] registeredUsers = new String[] {
        // This is just a fixed List for tutorial purposes
        // in a real application you'd check this server sided or inside the
        // database
        "tseng", "admin", "root", "joedoe", "john" };

   public CheckUsernameEditText(Context context) {
    super(context);
    // Set KeyListener to ourself
    this.setOnKeyListener(this);
}

public CheckUsernameEditText(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
    // Set KeyListener to ourself
    this.setOnKeyListener(this);
}

public CheckUsernameEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    // Set KeyListener to ourself
    this.setOnKeyListener(this);
}

// Allows the user to set an Listener and react to the event
public void setOnUsernameAvailableListener(
        OnUsernameAvailableListener listener) {
    onUsernameAvailableListener = listener;
}

// This function is called after the check was complete
private void OnUserChecked(String username, boolean available) {
    // Check if the Listener was set, otherwise we'll get an Exception when
    // we try to call it
    if (onUsernameAvailableListener != null) {
        // Only trigger the event, when we have a username
        if (!TextUtils.isEmpty(username)) {
            onUsernameAvailableListener.onAvailableChecked(username,
                    available);
        }
    }
}

@Override
public boolean onKey(View v, int keycode, KeyEvent keyevent) {
    // We only want to handle ACTION_UP events, when user releases a key
    if (keyevent.getAction() == KeyEvent.ACTION_DOWN)
        return false;

    boolean available = true;

    // Whenever a user press a key, check if the username is available
    String username = getText().toString().toLowerCase();
    if (!TextUtils.isEmpty(username)) {
        // Only perform check, if we have anything inside the EditText box
        for (int i = 0; i < registeredUsers.length; i++) {
            if (registeredUsers[i].equals(username)) {
                available = false;
                // Finish the loop, as the name is already taken
                break;
            }
        }
        // Trigger the Event and notify the user of our widget
        OnUserChecked(username, available);
        return false;
    }
    return false;
}

// Define our custom Listener interface
public interface OnUsernameAvailableListener {
    public abstract void onAvailableChecked(String username,
            boolean available);
}
   }

The problem is that I take a classclastexception. Because I declare on the xml the username as edittext and in the main code i declare it as CheckUsernameEditText. How I can solve this problem?Why the casting isn’t working, especially now that the CheckUsernameEditText extends the EditText class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:19:57+00:00Added an answer on June 15, 2026 at 3:19 am

    All CheckUsernameEditText objects are EditText objects,
    but not all EditText objects are CheckUsernameEditText objects.

    You should use your custom class in the XML:

    <your.package.name.CheckUsernameEditText
        android:id="@+id/username"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="true" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my code: public class MainActivity extends Activity { private ComponentName mService; private
findViewById returns null for EditText Java Code: public class MainActivity extends Activity { private
I have the following class: public class MainActivity extends Activity { /** Called when
I have this structure: 1) main activity: public class mainActivity extends Activity { @Override
public class MainActivity extends Activity implements OnClickListener, AdapterView.OnItemSelectedListener, OnCheckedChangeListener{ @Override public void onCreate(Bundle icicle)
I have 2 tabs, named : Tab1, Tab2 class MainActivity extends TabActivity. MainActivity.java public
I got some invalid Token parsing problem Code : public class viewparty extends Activity
I am trying to set a breakpoint inside an Activity class: public class MainActivity
import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; import android.os.Bundle; public class Map2Activity extends MapActivity { /** Called
Main Activity: private Button btnSubmit; private DataSource mDataSource; private Context mContext; @Override public void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.