andDev
andDev

Reputation: 275

find out parent id of a button in android

In my application i hava a linearlayout and its child button. After touch listener on button i need to know id of linearlayout. Here is my code

private final class MyTouchListener implements OnTouchListener 
{   

    public boolean onTouch(View view, MotionEvent motionEvent)
    {               
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN)
        {
            View v = ((View)view.getParent());
            int id = v.getId();
            Toast.makeText(getApplicationContext(), id, Toast.LENGTH_SHORT).show();

i get android.content.res.Resources$NotFoundException. how can i get id of linearlayout. Thanks....

Upvotes: 0

Views: 755

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82543

Try using:

View v = ((View)view.getParent());
int id = v.getId();
Toast.makeText(getApplicationContext(), String.valueOf(id), Toast.LENGTH_SHORT).show();

This will toast the ID of your linear layout in the integer format that its stored in in R.java.

Your current implementation is such that your Toast.makeText() matches the makeText(Context, int, int) method signature, which is meant to be used with R.string.* elements. However, since you pass it an R.id.* element where it expects a string identifier, is uses that to try and find a matching String in strings.xml. When it fails to find one, it throws a Resources$NotFoundException.

Upvotes: 1

Related Questions