svenkapudija
svenkapudija

Reputation: 5166

Get resource id by name returns "wrong" id

Why the firstId is not equal to secondId? firstId is the wrong one, secondId is the correct one.

How can I "force" getIdentifier to search for the right search_button?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    int firstId = getResources().getIdentifier("android:id/search_button", null, null);
    int secondId = recursiveLoopChildren(searchView, "search_button").getId();     

    return true;
}

public View recursiveLoopChildren(ViewGroup p_parent, String p_resourceName) {
    View view = null;

    for (int i = p_parent.getChildCount() - 1; i >= 0; i--) {
        final View child = p_parent.getChildAt(i);
        if (child instanceof ViewGroup) {
            View returnedChild = recursiveLoopChildren((ViewGroup) child, p_resourceName);
            if(returnedChild != null) {
                view = returnedChild;
                break;
            }
        } else {
            String entryName = getResources().getResourceEntryName(child.getId());
            if (child != null && entryName.equals(p_resourceName)) {
                view = child;
                break;
            }
        }
    }

    return view;
}

Upvotes: 0

Views: 331

Answers (2)

samgak
samgak

Reputation: 24417

Call getIdentifier() like this:

int firstId = getResources().getIdentifier("search_button", "id", getPackageName());

Then it should return the correct id.

Upvotes: 1

M S Gadag
M S Gadag

Reputation: 2057

try this

final View child = p_parent.getChildAt(i - 1);

if not works.. try with this condition..

for (int i = p_parent.getChildCount(); i > 0; i--)

Upvotes: 0

Related Questions