0% found this document useful (0 votes)
10 views8 pages

Modularidad en Desarrollo Android

The document outlines the steps to create a modular Android application with a navigation activity featuring three fragments: Home, Books, and Music. It includes instructions for setting up XML resources, modifying the MainActivity, and creating corresponding fragments and view models for Books and Music. The goal is to demonstrate the functionality of different fragments at runtime.

Uploaded by

jossavva
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Modularidad en Desarrollo Android

The document outlines the steps to create a modular Android application with a navigation activity featuring three fragments: Home, Books, and Music. It includes instructions for setting up XML resources, modifying the MainActivity, and creating corresponding fragments and view models for Books and Music. The goal is to demonstrate the functionality of different fragments at runtime.

Uploaded by

jossavva
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Modularidad

Objetivo
Mostrar el funcionamiento de diferentes fragmentos en tiempo de ejecución.

Desarrollo
1 Crear un nuevo proyecto, con una actividad de navegación con botones llamada
Modularidad.

2 Inserta en la carpeta drawable las imágenes que vas a utilizar.


3 Modifica los siguientes archivos:

[Link]
<resources>
<string name="app_name">Modularidad</string>
<string name="title_home">Home</string>
<string name="title_books">Books</string>
<string name="title_music">Music</string>
</resources>

bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="[Link]

<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />

<item
android:id="@+id/navigation_books"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/title_books" />

<item
android:id="@+id/navigation_music"
android:icon="@android:drawable/presence_audio_busy"
android:title="@string/title_music" />
</menu>

mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/mobile_navigation"
app:startDestination="@+id/navigation_home">

<fragment
android:id="@+id/navigation_home"
android:name="[Link]"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />

<fragment
android:id="@+id/navigation_books"
android:name="[Link]"
android:label="@string/title_books"
tools:layout="@layout/fragment_books" />

<fragment
android:id="@+id/navigation_music"
android:name="[Link]"
android:label="@string/title_music"
tools:layout="@layout/fragment_music" />
</navigation>

[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)

binding = [Link](layoutInflater)
setContentView([Link])

val navView: BottomNavigationView = [Link]

val navController = findNavController([Link].nav_host_fragment_activity_main)


// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
[Link].navigation_home, [Link].navigation_books, [Link].navigation_music
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
[Link](navController)
}
}

Dentro de la carpeta de interfaces de usuario ui crea una carpeta tipo paquete por cada
opción del menú para guardar las clases correspondientes.

Ahora elimina los paquetes y las interfaces xml que no vas a utilizar.

Debes crear una actividad para cada una de las opciones del menú. En este ejemplo voy a
crear una para books y otra para music.

En el paquete books se crea una actividad (en este caso Empty activity).
fragment_books.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".[Link]">

<TextView
android:id="@+id/text_notifications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>

[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class BooksFragment : Fragment() {

private var _binding: FragmentBooksBinding? = null

// This property is only valid between onCreateView and


// onDestroyView.
private val binding get() = _binding!!

override fun onCreateView(


inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val booksViewModel =
ViewModelProvider(this).get(BooksViewModel::[Link])

_binding = [Link](inflater, container, false)


val root: View = [Link]

val textView: TextView = [Link]


[Link](viewLifecycleOwner) {
[Link] = it
}
return root
}

override fun onDestroyView() {


[Link]()
_binding = null
}
}

[Link]
package [Link]

import [Link]
import [Link]
import [Link]

class BooksViewModel:ViewModel()
{
private val _text = MutableLiveData<String>().apply {
value = "Libros" }
val text: LiveData<String> = _text
}
fragment_books.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".[Link]">

<TextView
android:id="@+id/text_notifications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>

[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class MusicFragment : Fragment() {

private var _binding: FragmentMusicBinding? = null

// This property is only valid between onCreateView and


// onDestroyView.
private val binding get() = _binding!!

override fun onCreateView(


inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val musicViewModel =
ViewModelProvider(this).get(MusicViewModel::[Link])

_binding = [Link](inflater, container, false)


val root: View = [Link]

val textView: TextView = [Link]


[Link](viewLifecycleOwner) {
[Link] = it
}
return root
}

override fun onDestroyView() {


[Link]()
_binding = null
}
}

[Link]
package [Link]

import [Link]
import [Link]
import [Link]

class MusicViewModel:ViewModel()
{
private val _text = MutableLiveData<String>().apply {
value = "Música" }
val text: LiveData<String> = _text
}

You might also like