Unpossible
Unpossible

Reputation: 10697

Android: Access to sibling View

I have an Activity that inflates an XML FrameLayout that has a custom View, and a RelativeLayout containing a number of Text/ImageViews:

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

    <my.package.name.CanvasView
        android:id="@+id/canvas"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="24dp"
        android:background="#66000000">
    <TextView 
        android:id="@+id/toolbar_time"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:gravity="right|center_vertical"
        android:textColor="#FFFFFFFF"
        android:shadowColor="#000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2" />
    <ImageView
        android:id="@+id/toolbar_notification"
        android:layout_toLeftOf="@+id/toolbar_time"
        android:layout_width="20dp"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:gravity="center|center_vertical" />
    <TextView 
        android:id="@+id/toolbar_title"
        android:layout_toLeftOf="@+id/toolbar_notification"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:gravity="left|center_vertical"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:textColor="#FFFFFFFF"
        android:shadowColor="#000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2" />
    </RelativeLayout>
</FrameLayout>

In the custom 'CanvasView', I do some drawing, and intercept onTouchEvent(), etc. Based on the user's actions in the View I'd like to update one of the TextView's, specifically toolbar_notification. It would be best to do this in the CanvasView itself, but as the TextView is not a childview of it, I can't access it via findViewById().

I've tried this solution, but I get a RuntimeException of "Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class my.package.name.CanvasView" everytime I try to access the parent View's properties via:

ViewGroup parentView = (ViewGroup) getParent();
parentView.getChildCount();

Any suggestions?

Thanks,

Paul

Upvotes: 2

Views: 4620

Answers (2)

Matthew
Matthew

Reputation: 44919

I would create a Listener interface on your custom Canvas and have your Activity implement it. In your touch event you can call the Listener method and get back into the Activity.

Reasons to do so:

  • Your activity already knows about all of the views in the hierarchy.
  • This method will still work even if your TextView moves around.
  • More consistent with the way the framework works

In your custom Canvas:

public static interface Listener {
    public void someEventHappened(String moreInfo);
}

private Listener listener;

public void setListener(Listener listener) { this.listener = listener; }

In your Activity, implement the interface

public class MyActivity extends Activity implements MyCanvas.Listener { ...

and set the listener in your onCreate() method in your activity:

myCanvas.setListener(this);

Then you can put your TextView-changing code in the callback inside your Activity:

public void someEventHappened(String moreInfo) {
    TextView myTextView = (TextView) findViewById(R.id.my_text_view);
    myTextView.setText(moreInfo);
}

Upvotes: 3

raukodraug
raukodraug

Reputation: 11639

If you get a NullPointerException it means that parentView is null, are you querying getParent() inside CanvasView? or are you doing it in the Activity? With your code that should work, but it should be inside CanvasView.

Also you can try to cast CanvasView's context into the Activity and then get the child that you want. I hope this helps

Upvotes: 0

Related Questions