LiveData-Android Architecture Components

Introduction

Advantages

Implementation

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'android.arch.lifecycle:extensions:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
public class UserDetails {
private String name;
private int age;

public UserDetails(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
}
public class LiveDataObject extends ViewModel {
private MutableLiveData<UserDetails> userDetailsMutableLiveData;

public MutableLiveData<UserDetails> getUserDetailsMutableLiveData() {
if (userDetailsMutableLiveData == null) {

userDetailsMutableLiveData = new MutableLiveData<UserDetails>();
}
return userDetailsMutableLiveData;
}

}
public class MainActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final EditText edtName = findViewById(R.id.edt_username);
final EditText edtAge = findViewById(R.id.edt_age);

final TextView txtUserName = findViewById(R.id.username);
final TextView txtAge = findViewById(R.id.age);

findViewById(R.id.clickMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


}
});


}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<EditText
android:id="@+id/edt_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter Username"
android:padding="10dp"
/>

<EditText
android:id="@+id/edt_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter Age"
android:padding="10dp"
/>
</LinearLayout>


<Button
android:id="@+id/clickMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
></Button>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:text="Hello World!"
/>

<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:text="Hello World!"
/>
</LinearLayout>

</LinearLayout>
LiveDataObject liveDataObject = ViewModelProviders.of(this).get(LiveDataObject.class);
final Observer<UserDetails> userDetailsObserver = new Observer<UserDetails>() {

@Override
public void onChanged(UserDetails userDetails) {
txtUserName.setText(userDetails.getName());
txtAge.setText("" + userDetails.getAge());
}
};
liveDataObject.getUserDetailsMutableLiveData().observe(this, userDetailsObserver);
public class MainActivity extends AppCompatActivity {

LiveDataObject liveDataObject;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final EditText edtName = findViewById(R.id.edt_username);
final EditText edtAge = findViewById(R.id.edt_age);

final TextView txtUserName = findViewById(R.id.username);
final TextView txtAge = findViewById(R.id.age);


liveDataObject = ViewModelProviders.of(this).get(LiveDataObject.class);
final Observer<UserDetails> userDetailsObserver = new Observer<UserDetails>() {

@Override
public void onChanged(UserDetails userDetails) {
txtUserName.setText(userDetails.getName());
txtAge.setText("" + userDetails.getAge());
}
};

findViewById(R.id.clickMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


}
});

liveDataObject.getUserDetailsMutableLiveData().observe(this, userDetailsObserver);
}
}
findViewById(R.id.clickMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

UserDetails userDetails = new UserDetails(edtName.getText().toString(), Integer.parseInt(edtAge.getText().toString()));
liveDataObject.getUserDetailsMutableLiveData().setValue(userDetails);
}
});

--

--

Mobile Application Developer | Flutter Developer | Project Manager | Team Leader | Scrum Certified | PMI APC Appeard | Writing (starting soon)

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store