Alexis
Alexis

Reputation: 16839

how to access elements inside an included view in a android xml layout file?

I created a layout and I included an android layout inside it :

<include
    layout="@android:layout/simple_list_item_multiple_choice" />

Once my whole view is inflated how can I access the element inside the simple_list_item_multiple_choice ? I guess there are a TextView and a CheckBox that I should be able to use but what's their id ?

EDIT: Do I need to inflate more than one thing ?

Upvotes: 3

Views: 4225

Answers (3)

BrantApps
BrantApps

Reputation: 6472

I complete such tasks by providing an @+id to the <include/> statement. So for something like your example above I would write...

  <include
  android:id="@+id/listItemMultiChoiceWrapper"
  layout="@android:layout/simple_list_item_multiple_choice" />

You could then access the text1 field by writing at your activity/fragment level (where ever you set the view containing the <include/>)

  final CheckedTextView ctv = (CheckedTextView) findViewById(id.listItemMultiChoiceWrapper).findViewById(id.text1);

If that doesn't work then check for duplicate ids confusing the situation (Lint should pick these up too).

Upvotes: 2

Stefano Ortisi
Stefano Ortisi

Reputation: 5336

You can access it doing this:

CheckedTextView ctv = (CheckedTextView)findViewById(android.R.id.text1);

Upvotes: 2

Tomislav Novoselec
Tomislav Novoselec

Reputation: 4620

This is the code for simple_list_item_multiple_choice from Android SDK:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceListItem"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingLeft="8dip"
    android:paddingRight="8dip"
/>

I guess you can handle it from here.

Upvotes: 1

Related Questions