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

  • SEARCH
  • Home
  • 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 4619920
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:29:23+00:00 2026-05-22T02:29:23+00:00

New to android and programming in general, so learning concepts as I go. I

  • 0

New to android and programming in general, so learning concepts as I go.

I have a LinearLayout to which I am adding edittexts dynamically.

I need to be able to get the Index AND Id of any edittext when it is selected or in focus.

I have tried looping through a child count to check for selected like so:

int count = llmain.getChildCount();
      for (int i=0; i< count; ++i) {
          if ((llmain.getChildAt(i).isSelected()) == true){
            //Do Stuff
          }

but I have no idea if its even close, and that would be only for index…..

Help would be greatly appreciated!

Thanks

EDIT: still dont have a reliable way to achieve this. The example below with the

if(v instanceOf EditText) {
id = v.getId();
index = ll.indexOfChild(v);

returns a -1 for index and a ten digit number for the id, however, i am assigning an id on creation. ?The odd thing is that the code inside the “if” is running, so it at least thinks it has the focused view. Now, if I change the “instanceof” to a null check like

if (v != null){
            id = v.getId();
            index = llmain.indexOfChild(v);

AND I add a setFocusableInTouchMode(true), I get a proper return, however, it then acts like i called clearFocus(), because none of the EditTexts are focused. Here is my full proof of concept code that returns the correct values but no longer lets the EditTexts actually have focus.

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&(event.getKeyCode() == 66)) // KeyEvent.* lists all the key codes u pressed
    {   
        View myView = linflater.inflate(R.layout.action, null);
        myView.setId(pos);
        pos++;
        myView.setFocusableInTouchMode(true);
        llmain.addView(myView);
        myView.requestFocus();
        View v = llmain.findFocus();

        if (v != null){
            id = v.getId();
            index = llmain.indexOfChild(v);
            Context context = getApplicationContext();
            CharSequence text = "index is:" + index + "id is:" + id;
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            }


    }
    return false;   
}       

This returns the correct values unless I comment the setFocusableInTouchMode line, then it goes back to the odd -1 for index and ten digit for ID. What am I doing wrong? going to have to be a good (and working) answer to get over half of my rep…..

So nobody has a solution for this? its still driving me nuts!

thanks again

  • 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-05-22T02:29:24+00:00Added an answer on May 22, 2026 at 2:29 am

    My initial thought would be to set the ID equal to the index when adding an EditText, assuming only EditTexts will be in this particular layout:

    LinearLayout llMain = (LinearLayout)findViewById(R.id.llmain);
    EditText editText = new EditText(this);
    //0-based index, so get the number of current views, and use it for the next
    editText.setId(llMain.getChildCount());
    llMain.addView(editText);
    

    Then, to retrieve the info, put a check into a listener of some sort (onTouch, onFocus, something like that):

    @Override
    public void onTouch(View v, MotionEvent ev) {
        int indexAndId = v.getId();
    }
    

    Give this a shot:

    LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
    int index, id;
    
    //finds the currently focused View within the ViewGroup
    View v = ll.findFocus();
    
    if(v instanceOf EditText) {
        id = v.getId();
        index = ll.indexOfChild(v);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to android and programming in general so go easy! I have created
I am new to programming in general and have been teaching myself using Android
I'm new to programming applications for the Android OS. As far as general architecture
I am new to Android programming. I have installed the Eclipse and Android SDK.
I'm really new in Android programming, so I have a simple question getting a
I am new to Android Programming. I need to display a Pop-Up message in
I'm new to Java programming, I am programming Java 1.6 with Android. I have
I am new to Android programming, and looking for some general knowledge. I am
I'm new to android programming, but one thing that I can't seem to get
I am completly new to android programming and I try to get clicking on

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.