Is possible to change Button text using xml attribute onClick="click" and without creating a Button Object

Viewed 85

I am a beginner in android app development, I have seen there are two methods to handle on button click. First one is using setOnClickListener and second is using xml attribute onClick.

onClick xml attribute quite easy for beginner/me.

I can change button text by creating an object of a button and then: btn.setText("Button Clicked");

But my question is can I use setText, isEnabled, etc.. methods for button without creating its object.

Is possible to change button text without creating a Button Object or using the XML attribute onClick.

2 Answers
  1. To Your build.gradle (Module: app) add this:
android {
    buildFeatures{
        dataBinding = true
    }
}
  1. To Your String resources (res/values/strings.xml) add this:
<resources>
    <string name="button_clicked">Button Clicked</string>
</resources>
  1. And in activity_main.java:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="com.example.myapplication.R"/>
    </data>

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

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{()->button.setText(R.string.button_clicked)}"/>

    </FrameLayout>
</layout>

This will allow You to change button text without having a button instance. But I think this is not an optimal method and the standard setOnClickListener is a way better.


Another method

  1. activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="buttonClicked"
            type="java.lang.String"
            />
    </data>

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

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{()-> button.setText(buttonClicked)}"/>
        
    </FrameLayout>
</layout>
  1. MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import com.example.myapplication.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        binding.setButtonClicked("Button clicked");
    }
}

This can be achieved by doing manipulation in onClick handlers.

or

It can be done with data bindings: Just add your fragment instance as a variable, then you can link any method with onClick.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.testapp.fragments.CustomFragment">

<data>
    <variable android:name="fragment" android:type="com.example.testapp.fragments.CustomFragment"/>
</data>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_place_black_24dp"
        android:onClick="@{() -> fragment.buttonClicked()}"/>
</LinearLayout>
Related