Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hilt, The DI Library For Android – Part1: Getting To Know
<?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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/ma_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/ma_names_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="learn.shendy.learnhilt">
<application
android:name=".LearnHiltApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ui.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
android {
compileSdkVersion 29
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "learn.shendy.learnhilt"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
// NOTE: Projects that use both Hilt and data binding require Android Studio 4.0 or higher.
// No need to enable dataBinding to use Hilt, I just prefer to use it.
buildFeatures {
dataBinding = true
}
}
dependencies {
// Hilt
implementation "com.google.dagger:hilt-android:2.28-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
// Everything else
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/hn_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/hn_name_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:text="Mommy"
android:textColor="@android:color/black"
android:textSize="18sp"
android:padding="20dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
@HiltAndroidApp
public class LearnHiltApp extends Application {
//
}
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
@Inject
NamesAdapter mNamesAdapter;
private ActivityMainBinding mBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mBinding.maNamesRecycler.setLayoutManager(new LinearLayoutManager(this));
mBinding.maNamesRecycler.setAdapter(mNamesAdapter);
mNamesAdapter.update(buildMomNameList());
}
// MARK: Helper Methods
private List<String> buildMomNameList() {
return Arrays.asList(
"Momzilla", "Maa", "Ammi", "MuNu", "Amma", "MooMoo",
"Life", "Paradise", "Heaven", "Queen", "Fairy", "Momey",
"Mommu", "Mumu", "Maaaaa", "Heartbeat", "Wonder Woman", "Yo Mama",
"SuperWoman", "Big Mama"
);
}
}
public class NamesAdapter extends RecyclerView.Adapter<NamesAdapter.NameHolder> {
private List<String> mNames = new ArrayList<>();
@Inject
public NamesAdapter() {}
@NonNull
@Override
public NameHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
HolderNameBinding binding = HolderNameBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new NameHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull NameHolder holder, int position) {
String name = mNames.get(position);
holder.mBinding.hnNameTv.setText(name);
}
@Override
public int getItemCount() {
return mNames.size();
}
public void update(List<String> names) {
mNames.clear();
mNames.addAll(names);
notifyDataSetChanged();
}
class NameHolder extends RecyclerView.ViewHolder {
HolderNameBinding mBinding;
public NameHolder(@NonNull HolderNameBinding binding) {
super(binding.getRoot());
mBinding = binding;
}
}
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment