0% found this document useful (0 votes)
100 views12 pages

Espino Codelab6 Menus PDF

The document contains XML layout files and Kotlin code for an Android app that implements menus and navigation using fragments. It includes layout files for the main activity, toolbar, navigation drawer, and fragments for home, gallery, and about screens. It also includes code for the AboutFragment class that is used to display static content for the about screen fragment.

Uploaded by

kamiku ace
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
100 views12 pages

Espino Codelab6 Menus PDF

The document contains XML layout files and Kotlin code for an Android app that implements menus and navigation using fragments. It includes layout files for the main activity, toolbar, navigation drawer, and fragments for home, gallery, and about screens. It also includes code for the AboutFragment class that is used to display static content for the about screen fragment.

Uploaded by

kamiku ace
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

Espino, Amos C.

codelab6 - Menus

activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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"
tools:context="com.example.menus.HomeActivity"
android:id="@+id/drawer_layout">

<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorAccent"
app:popupTheme="@style/Theme.AppCompat.Light"/>

</FrameLayout>

<com.google.android.material.navigation.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
android:layout_gravity="start"
app:headerLayout="@layout/navi_header"
app:menu="@menu/main_menu"/>

</androidx.drawerlayout.widget.DrawerLayout>

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="239dp"
android:layout_height="211dp"
android:layout_marginTop="96dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logs" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome!"
android:textColor="@color/colorAccent"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintVertical_bias="0.0" />

<EditText
android:id="@+id/edt_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.0" />

<EditText
android:id="@+id/edt_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_username"
app:layout_constraintVertical_bias="0.0" />

<Button
android:id="@+id/btn_login"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#4CAF50"
android:text="Login"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_password"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">

<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar_main"
android:background="@color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"/>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout"/>

</com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

fragment_about.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.menus.AboutFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="About fragment"
android:textSize="30sp"
android:gravity="center"/>

</FrameLayout>

fragment_gallery.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.menus.GalleryFragment">

<!-- TODO: Update blank fragment layout -->


<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Gallery Fragment"
android:textSize="30sp"
android:gravity="center"/>

</FrameLayout>

fragment_home.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.menus.HomeFragment">

<!-- TODO: Update blank fragment layout -->


<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home Fragement"
android:gravity="center"
android:textSize="30sp"/>

</FrameLayout>

navi_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="@color/colorPrimary"
android:gravity="bottom"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

</LinearLayout>

main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navi_home"
android:title="Home"
android:icon="@drawable/ic_home"/>

<item
android:id="@+id/navi_gallery"
android:title="Gallery"
android:icon="@drawable/ic_gallery"/>
<item
android:id="@+id/navi_about"
android:title="About"
android:icon="@drawable/ic_about"/>

</menu>

AboutFragment.kt
package com.example.menus

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.loginactivitywithhelloandroid.R

ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class AboutFragment : Fragment() {
private var param1: String? = null
private var param2: String? = null

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}

override fun onCreateView(


inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about, container, false)
}

companion object {

@JvmStatic
fun newInstance(param1: String, param2: String) =
AboutFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

GalleryFragment.kt
package com.example.menus

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.loginactivitywithhelloandroid.R

ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class GalleryFragment : Fragment() {

private var param1: String? = null


private var param2: String? = null

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}

override fun onCreateView(


inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

return inflater.inflate(R.layout.fragment_gallery, container, false)


}

companion object {

@JvmStatic
fun newInstance(param1: String, param2: String) =
GalleryFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

HomeActivity.kt
package com.example.menus

import android.content.res.Configuration
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.widget.Toolbar
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.FragmentTransaction
import com.example.loginactivitywithhelloandroid.R
import com.google.android.material.navigation.NavigationView
import kotlinx.android.synthetic.main.activity_home.*

class HomeActivity : AppCompatActivity(),


NavigationView.OnNavigationItemSelectedListener {

lateinit var drawer: DrawerLayout


lateinit var toggle: ActionBarDrawerToggle
lateinit var homeFragment: HomeFragment
lateinit var galleryFragment: GalleryFragment
lateinit var aboutFragment: AboutFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)

val toolbar = findViewById<Toolbar>(R.id.toolbar_main)


setSupportActionBar(toolbar)

drawer = findViewById(R.id.drawer_layout)

toggle = ActionBarDrawerToggle(this, drawer, toolbar,


R.string.navigation_drawer_open,
R.string.navigation_drawer_close)
drawer.addDrawerListener(toggle)
toggle.syncState()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeButtonEnabled(true)

navigation_view.setNavigationItemSelectedListener(this)
}

override fun onPostCreate(savedInstanceState: Bundle?) {


super.onPostCreate(savedInstanceState)
toggle.syncState()
}

override fun onConfigurationChanged(newConfig: Configuration) {


super.onConfigurationChanged(newConfig)
toggle.onConfigurationChanged(newConfig)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {


if (toggle.onOptionsItemSelected(item))
return true

return super.onOptionsItemSelected(item)
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {


when(item.itemId){
R.id.navi_home -> {
Toast.makeText(this,"clicked home", Toast.LENGTH_LONG).show()
homeFragment = HomeFragment()
supportFragmentManager
.beginTransaction()
.replace(R.id.content_frame, homeFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
R.id.navi_gallery -> {
Toast.makeText(this,"clicked gallery", Toast.LENGTH_LONG).show()
galleryFragment = GalleryFragment()
supportFragmentManager
.beginTransaction()
.replace(R.id.content_frame, galleryFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
R.id.navi_about -> {
Toast.makeText(this,"clicked about", Toast.LENGTH_LONG).show()
aboutFragment = AboutFragment()
supportFragmentManager
.beginTransaction()
.replace(R.id.content_frame, aboutFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
}

drawer.closeDrawer(GravityCompat.START)
return true
}

override fun onBackPressed() {


if (drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START)
}else
super.onBackPressed()
}
}

HomeFragment.kt
package com.example.menus

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.loginactivitywithhelloandroid.R

ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class HomeFragment : Fragment() {

private var param1: String? = null


private var param2: String? = null

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

return inflater.inflate(R.layout.fragment_home, container, false)


}

companion object {

@JvmStatic
fun newInstance(param1: String, param2: String) =
HomeFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

MainActivity.kt
package com.example.loginactivitywithhelloandroid

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import com.example.menus.HomeActivity
import com.google.android.material.snackbar.Snackbar

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val username = findViewById<EditText>(R.id.edt_username)


val password = findViewById<EditText>(R.id.edt_password)
val btnLogin = findViewById<Button>(R.id.btn_login)

btnLogin.setOnClickListener {
if (username.text.toString().equals("admin") &&
password.text.toString().toString().equals("admin")) {
val intent = Intent(this, HomeActivity::class.java)
startActivity(intent)
} else {
val snackbar = Snackbar.make(
it, "incorect username or password",
Snackbar.LENGTH_SHORT
).setAction("Action", null)
snackbar.show()
}
}
}
}

You might also like