javaandroiddata-bindingnavigation-drawerandroid-databinding

Navigation Drawer and android data binding


Sorry for my English. I need to process a button click with Android Data Binding. I include in content_main.xml data binding.

    <data>
            <variable
                name="temp"
                type="anton.navigationdrawer.DataBindingClass.MainActivityData" />
            <variable
                name="presenter"
                type="anton.navigationdrawer.DataBindingClass.MainActivityPresenter"/>
        </data>
....
     <android.support.design.widget.FloatingActionButton
                android:onClick="@{() -> presenter.onShowData(temp)}"

But MainActivity.java

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

        MainActivityPresenter mainActivityPresenter = new MainActivityPresenter(this);
        MainActivityData mainActivityData = new MainActivityData("Иди нахуй");
        binding.setTemp(mainActivityData);
        binding.setPresenter(mainActivityPresenter);

        setContentView(R.layout.activity_main);
....

 @Override
    public void showData(MainActivityData mainActivityData) {
        String textForRecognition = mainActivityData.getTextForRecognition();
        Toast.makeText(this, textForRecognition, Toast.LENGTH_SHORT).show();
    }

I want the function showData run then i click on FloatingActionButton in content_main. But showData - function in class MainActivity. And therefore as I understand it is not called when you click on the button located on the content_main.xml. What do i do? Create a separate class ContentActivity.java whith function showData.

I just want to MainActivity.java was responsible for processing the click on content_main. How to implement it correctly?


Solution

  • In Activity add this code for databinding like this

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        MainViewmodel mainViewmodel = ViewModelProviders.of(this).get(MainViewmodel.class);
        activityMainBinding.setHandlers(handlers);
        activityMainBinding.setViewmodel(mainViewmodel);
    
    
    
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("HOME");
    
        DrawerLayout drawer = activityMainBinding.drawerLayout;
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    
        NavigationView navigationView = activityMainBinding.navView;
        NavHeaderMainBinding navHeaderMainBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.nav_header_main, activityMainBinding.navView, true);
        navHeaderMainBinding.navEmail.setText(session.getFacilityEmail());
        navHeaderMainBinding.navName.setText(session.getFacilityname());
        navigationView.setNavigationItemSelectedListener(this);
    
    
    
    }
    

    in nav_hader_main.xml you have to be like this

    <?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">
    
    <data>
    
        <variable
            name="navigationheader"
            type="com.nudge.gurukul.view.activities.MainActivity" />
    
        <import type="android.view.View" />
    </data>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/nav_header_height"
        android:background="@drawable/side_nav_bar"
        android:gravity="bottom"
        android:orientation="vertical"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/nav_header_desc"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            app:srcCompat="@mipmap/app_logo_round" />
    
        <TextView
            android:id="@+id/nav_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
    
        <TextView
            android:id="@+id/nav_email"
    
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    this should as per you excepted and you can bind the text and image to navigation drawer