Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android data binding. How to access view via ID inside a included layout

I have a layout includes some layouts. One of them have a progressBar and I want to access it using data binding but I can't.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    >
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:splitMotionEvents="true"
        tools:context="jp.co.sakony.activities.DiaryNewArticleActivity"
        >

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/ToolBar"
            />

    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

        <ScrollView
                android:id="@+id/diary_new_article_scrollView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true"
                >

            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/margin_medium"
                    android:layout_marginRight="@dimen/margin_medium"
                    android:orientation="vertical"
                    android:splitMotionEvents="true"
                    >

                <jp.co.sakony.views.contents.InputFieldDescriptionView
                        android:id="@+id/diary_new_article_input_title_description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/margin_medium"
                        app:input_field_maxLength="@integer/diary_new_article_title_max_length"
                        app:input_field_required="true"
                        app:input_field_title="@string/diary_new_article_input_title"
                        />

                <android.support.design.widget.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="-10dp"
                        app:counterEnabled="true"
                        app:counterMaxLength="@integer/diary_new_article_title_max_length"
                        app:counterTextAppearance="@style/counterText"
                        app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
                        >
                    <jp.co.sakony.views.APEditText
                            android:id="@+id/diary_new_article_input_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:inputType="text"
                            android:maxLength="@integer/diary_new_article_title_max_length"
                            android:maxLines="1"
                            android:textCursorDrawable="@drawable/shape_edit_cursor_orange"
                            app:textLineColor="@color/orange"
                            />

                </android.support.design.widget.TextInputLayout>

                <jp.co.sakony.views.contents.InputFieldDescriptionView
                        android:id="@+id/diary_new_article_input_body_description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/margin_medium"
                        app:input_field_maxLength="@integer/diary_new_article_body_max_length"
                        app:input_field_required="true"
                        app:input_field_title="@string/diary_new_article_input_body"
                        />

                <android.support.design.widget.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="-10dp"
                        app:counterEnabled="true"
                        app:counterMaxLength="@integer/diary_new_article_body_max_length"
                        app:counterTextAppearance="@style/counterText"
                        app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
                        >
                    <jp.co.sakony.views.APEditText
                            android:id="@+id/diary_new_article_input_body"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:maxLength="@integer/diary_new_article_body_max_length"
                            android:scrollbars="vertical"
                            android:textCursorDrawable="@drawable/shape_edit_cursor_orange"
                            app:textLineColor="@color/orange"
                            />

                </android.support.design.widget.TextInputLayout>

                <jp.co.sakony.views.contents.InputFieldDescriptionView
                        android:id="@+id/diary_new_article_input_allow_comment_description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/margin_medium"
                        app:input_field_explanation="@string/diary_new_article_input_allow_comment_explanation1"
                        app:input_field_required="true"
                        app:input_field_title="@string/diary_new_article_input_allow_comment"
                        />

                <android.support.v7.widget.SwitchCompat
                        android:id="@+id/diary_new_article_input_allow_comment"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/margin_medium"
                        android:background="@drawable/selector_comment_reply_background"
                        android:checked="true"
                        android:padding="@dimen/margin_small"
                        android:text="@string/diary_new_article_allow_comment_switch_on"
                        android:textSize="@dimen/font_small"
                        android:theme="@style/AllowCommentSwitch"
                        />

            </LinearLayout>

        </ScrollView>

        <include layout="@layout/toolbar_shadow"/>

        <include
                layout="@layout/progress_bar_masked"
                android:id="@+id/progress"
                />

    </FrameLayout>

</LinearLayout>

progress_bar_masked.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
        android:id="@+id/progressbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/transparent_grey"
        android:clickable="true"
        >

    <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            style="?android:attr/progressBarStyle"
            />

</RelativeLayout>

Activity

abstract public class BaseDiaryArticleActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher,
    DialogOkCancelFragment.DialogOKCancellCallback {

protected ActivityBaseDiaryArticleBinding mBinding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBinding = DataBindingUtil.setContentView(this, R.layout.activity_base_diary_article);

    // can't find progressbar.
    mBinding.progress.progressbar
}

mBinding.progress return View. but I think it should return ProgressBarMaskedBinding.

UPDATED

I solved this problem.

progress_bar_masked.xml is in a library project. now I moved this layout into app project and it's working. Maybe it's a databinding bug isn't it?

like image 827
sakony Avatar asked Aug 16 '16 07:08

sakony


3 Answers

You must give your include an ID:

    <include android:id="@+id/toolShadow" layout="@layout/toolbar_shadow"/>

Then access views within the included layout via the included binding:

mBinding.toolShadow.progressbar.setProgress(100);
like image 155
George Mount Avatar answered Nov 15 '22 06:11

George Mount


you must wrap included view in layout tag

<layout xmlns:android="http://schemas.android.com/apk/res/android">
</layout>

then you can access them like ActivityMainBinding.parentview.innerview; hope it helps someone.

like image 45
Mateen Chaudhry Avatar answered Nov 15 '22 05:11

Mateen Chaudhry


You must give include an ID, and also implement data-binding in included file as adding: <layout xmlns:android="http://schemas.android.com/apk/res/android"> then you will be able to access views from included layout file

like image 45
Balflear Avatar answered Nov 15 '22 04:11

Balflear