I am a newbie to coding with Android Studio / Korlin.
To get started I am trying to build a simple app that give a Treeview of the internal storage of the device its run on (In the AVD I am using a Pixel 6 Pro - 34.
The below code build and I can create the APK and deploy it, but opening it, it closes instantly.
This is starter code which I will look to build upon once I have a running starting point, but as youll see, that isnt what I presently have (LOL)
MainActivity.KT:
package com.example.treeview
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.content.Context
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import java.io.File
import java.util.ArrayList
class TreeViewActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var files: MutableList<File>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tree_view)
recyclerView = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)
files = ArrayList()
// Get all files from internal storage
val internalStorage = Environment.getExternalStorageDirectory()
val allFiles = internalStorage.listFiles()
for (file in allFiles) {
if (file.isDirectory()) {
files.add(file)
}
}
// Create a TreeViewAdapter and set it to the RecyclerView
val adapter = TreeViewAdapter(this, files)
recyclerView.adapter = adapter
}
class TreeViewAdapter(private val context: Context, private val files: MutableList<File>) :
RecyclerView.Adapter<TreeViewAdapter.ViewHolder>() {
private val inflater = LayoutInflater.from(context)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = inflater.inflate(R.layout.item_file, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val file = files[position]
holder.textView.text = file.name
// Check if the file is a directory
if (file.isDirectory()) {
holder.imageView.setImageResource(R.drawable.fileinfolder)
} else {
holder.imageView.setImageResource(R.drawable.files)
}
holder.itemView.setOnClickListener {
// Do something when the file is clicked
Toast.makeText(context, "File clicked: " + file.name, Toast.LENGTH_SHORT).show()
}
}
override fun getItemCount(): Int = files.size
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textView: TextView = itemView.findViewById(R.id.text_view)
val imageView: ImageView = itemView.findViewById(R.id.image_view)
}
}
}
Activity_Tree_View.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@String/no_files_found"
android:visibility="gone" />
</LinearLayout>
Item_file.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@String/no_files_found"
android:visibility="gone" />
</LinearLayout>
Build.Gradle (App):
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.treeview'
compileSdk 33
defaultConfig {
applicationId "com.example.treeview"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.3.2'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.10.1'
implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0')
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.activity:activity-compose:1.7.2'
implementation platform('androidx.compose:compose-orlin:2022.10.00')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.recyclerview:recyclerview:1.3.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
debugImplementation 'androidx.compose.ui:ui-tooling'
debugImplementation 'androidx.compose.ui:ui-test-manifest'
}
Anyone that can help get this to actually run on the AVD I would be very appreciative...
Bill
To get started I am trying to build a simple app that give a Treeview of the internal storage of the device its run on (In the AVD I am using a Pixel 6 Pro - 34.
The below code build and I can create the APK and deploy it, but opening it, it closes instantly.
This is starter code which I will look to build upon once I have a running starting point, but as youll see, that isnt what I presently have (LOL)
MainActivity.KT:
package com.example.treeview
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.content.Context
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import java.io.File
import java.util.ArrayList
class TreeViewActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var files: MutableList<File>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tree_view)
recyclerView = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)
files = ArrayList()
// Get all files from internal storage
val internalStorage = Environment.getExternalStorageDirectory()
val allFiles = internalStorage.listFiles()
for (file in allFiles) {
if (file.isDirectory()) {
files.add(file)
}
}
// Create a TreeViewAdapter and set it to the RecyclerView
val adapter = TreeViewAdapter(this, files)
recyclerView.adapter = adapter
}
class TreeViewAdapter(private val context: Context, private val files: MutableList<File>) :
RecyclerView.Adapter<TreeViewAdapter.ViewHolder>() {
private val inflater = LayoutInflater.from(context)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = inflater.inflate(R.layout.item_file, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val file = files[position]
holder.textView.text = file.name
// Check if the file is a directory
if (file.isDirectory()) {
holder.imageView.setImageResource(R.drawable.fileinfolder)
} else {
holder.imageView.setImageResource(R.drawable.files)
}
holder.itemView.setOnClickListener {
// Do something when the file is clicked
Toast.makeText(context, "File clicked: " + file.name, Toast.LENGTH_SHORT).show()
}
}
override fun getItemCount(): Int = files.size
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textView: TextView = itemView.findViewById(R.id.text_view)
val imageView: ImageView = itemView.findViewById(R.id.image_view)
}
}
}
Activity_Tree_View.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@String/no_files_found"
android:visibility="gone" />
</LinearLayout>
Item_file.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@String/no_files_found"
android:visibility="gone" />
</LinearLayout>
Build.Gradle (App):
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.treeview'
compileSdk 33
defaultConfig {
applicationId "com.example.treeview"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.3.2'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.10.1'
implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0')
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.activity:activity-compose:1.7.2'
implementation platform('androidx.compose:compose-orlin:2022.10.00')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.recyclerview:recyclerview:1.3.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
debugImplementation 'androidx.compose.ui:ui-tooling'
debugImplementation 'androidx.compose.ui:ui-test-manifest'
}
Anyone that can help get this to actually run on the AVD I would be very appreciative...
Bill