0% found this document useful (0 votes)
57 views23 pages

Andriod Application

This document contains code for an Android mobile application that allows transferring data between activities. It includes code for a MainActivity that allows entering text and clicking a button to launch a SecondActivity, passing the text. The SecondActivity receives and displays the text. It also includes code for a third activity, MainActivity3, that also receives and displays passed data using the same method.

Uploaded by

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

Andriod Application

This document contains code for an Android mobile application that allows transferring data between activities. It includes code for a MainActivity that allows entering text and clicking a button to launch a SecondActivity, passing the text. The SecondActivity receives and displays the text. It also includes code for a third activity, MainActivity3, that also receives and displays passed data using the same method.

Uploaded by

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

NAME # SOFIA NAZ

ROLL NO # CB428849

REGISTRATION NO # 20NKK00577

MOBILE APPLICATION DEVELOPMENT

(3499)
ASSIGNMENT NO # 01

TASK NO # 01 (BUTTON):-

MainActivity.kt
package com.example.application3

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {


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

val btnNewActivity2= findViewById<Button>(R.id.button2)


btnNewActivity2.setOnClickListener() {
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)

}
}

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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="154dp"
tools:layout_editor_absoluteY="426dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Main_activity2.kt
package com.example.application3

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity2 : AppCompatActivity() {


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

val btnNewActivity= findViewById<Button>(R.id.button2)


btnNewActivity.setOnClickListener() {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)

}
}

activity_main2.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=".MainActivity2">

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
tools:layout_editor_absoluteX="174dp"
tools:layout_editor_absoluteY="311dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

TASK NO # 02 (DATATRANSFER):-

MainActivity.kt
package com.example.datatransfer

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText

class MainActivity : AppCompatActivity() {

lateinit var send_button: Button


lateinit var send_text: EditText

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

send_button = findViewById(R.id.send_button_id)
send_text = findViewById(R.id.send_text_id)

// add the OnClickListener in sender button after clicked this button


following Instruction will run
send_button.setOnClickListener {
// get the value which input by user in EditText and convert it
to string

val str = send_text.text.toString()

// Create the Intent object of this class Context() to


Second_activity class
val intent = Intent(applicationContext,
Second_activity::class.java)
// now by putExtra method put the value in key, value pair key is
// message_key by this key we will receive the value, and put the
string
intent.putExtra("message_key", str)

// start the Intent


startActivity(intent)
}

}
}

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"
android:background="#32a895"
tools:context=".MainActivity">

<EditText
android:id="@+id/send_text_id"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="Input"
android:textSize="25dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/send_button_id"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#32a895"
android:text="Login"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.635" />
</androidx.constraintlayout.widget.ConstraintLayout>

Second_activity.kt
package com.example.datatransfer

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView

class Second_activity : AppCompatActivity()


{ lateinit var send_button: Button

lateinit var receiver_msg: TextView


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
send_button = findViewById(R.id.send_button_id)

receiver_msg = findViewById(R.id.received_value_id)
// create the get Intent object
val intent = intent
// receive the value by getStringExtra() method and
// key must be same which is send by first activity

val str = intent.getStringExtra("message_key")

// display the string into textView


receiver_msg.text = str

}
}

activity_second.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:id="@+id/send_button_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Second_activity">
<TextView
android:id="@+id/received_value_id"
android:layout_width="300dp"
android:layout_height="50dp"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/send_button"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#32a895"
android:text="Login"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.635" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity3.kt
package com.example.datatransfer

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class MainActivity3 : AppCompatActivity()


{
lateinit var receiver_msg: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main3)

receiver_msg = findViewById(R.id.received_value_id)
// create the get Intent object
val intent = intent
// receive the value by getStringExtra() method and
// key must be same which is send by first activity
val str = intent.getStringExtra("message_key")

// display the string into textView


receiver_msg.text = str

}
}

activity_main3.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=".MainActivity3">

<TextView
android:id="@+id/received_value_id"
android:layout_width="300dp"
android:layout_height="50dp"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

TASK NO # 03 (FRAGMENT):-

MainActivity.java

package com.example.fragmenttest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Fragment manager to manage code
FragmentManager fragmentManager = getSupportFragmentManager();

Button btnNews = findViewById(R.id.btnNews);


btnNews.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

fragmentManager.beginTransaction()
.replace(R.id.fragmentContainerView,
NewsFragment.class, null)
.setReorderingAllowed(true)
.addToBackStack("name")
.commit();
}

});

/// Code for Sports Button to switch Fragments


Button btnSports = findViewById(R.id.btnSports);
btnSports.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

FragmentManager fragmentManager =
getSupportFragmentManager();

fragmentManager.beginTransaction()
.replace(R.id.fragmentContainerView,
SportsFragment.class, null)
.setReorderingAllowed(true)
.addToBackStack("name")
.commit();
}
});

/// Code for Science Button to switch Fragments


Button btnSci = findViewById(R.id.btnScience);
btnSci.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

FragmentManager fragmentManager =
getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.fragmentContainerView,
ScienceFragment.class, null)
.setReorderingAllowed(true)
.addToBackStack("name")
.commit();
}
});
}
}

activity_main.xml
<?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="wrap_content"
android:background="#B4C1E6"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="85dp"
android:background="#673AB7"
android:fontFamily="@font/zilla_slab_bold_italic"
android:paddingTop="20dp"
android:text="Fragment Test"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="34sp"
android:textStyle="bold" />

<LinearLayout
android:id="@+id/fragmentContainerView"
android:layout_width="match_parent"
android:layout_height="97dp"
android:orientation="horizontal"
android:paddingTop="10dp">

<Button
android:id="@+id/btnNews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="#F44336"
android:text="News"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />

<Button
android:id="@+id/btnScience"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="#E91E63"
android:text="Science"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />

<Button
android:id="@+id/btnSports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="#FFC107"
android:text="Sports"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />

<Button
android:id="@+id/btnmovie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="#00BCD4"
android:text="movie"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="2809dp"
android:orientation="vertical"/>
</LinearLayout>

NewsFragment.java
package com.example.fragmenttest;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* Use the {@link NewsFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class NewsFragment extends Fragment {

// TODO: Rename parameter arguments, choose names that match


// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters


private String mParam1;
private String mParam2;

public NewsFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment NewsFragment.
*/
// TODO: Rename and change types and number of parameters
public static NewsFragment newInstance(String param1, String param2) {
NewsFragment fragment = new NewsFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_news, container, false);
}
}
fragment_news.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:backgroundTint="#7C524F"
android:textAlignment="center"
tools:context="NewsFragment">

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


<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4CAF50"
android:fontFamily="@font/zilla_slab_bold_italic"
android:paddingTop="30dp"
android:text="News Fragment"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="48sp"
android:textStyle="bold" />

</FrameLayout>

ScienceFragment.java
package com.example.fragmenttest;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* A simple {@link Fragment} subclass.
* Use the {@link ScienceFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class ScienceFragment extends Fragment {

// TODO: Rename parameter arguments, choose names that match


// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters


private String mParam1;
private String mParam2;

public ScienceFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ScienceFragment.
*/
// TODO: Rename and change types and number of parameters
public static ScienceFragment newInstance(String param1, String param2) {
ScienceFragment fragment = new ScienceFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_science, container, false);
}
}

fragment_science.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".ScienceFragment">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#218E9C"
android:fontFamily="@font/zilla_slab_bold_italic"
android:paddingTop="30dp"
android:text="Science Fragment"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="48sp"
android:textStyle="bold" />
</FrameLayout>

SportsFragment.java
package com.example.fragmenttest;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* A simple {@link Fragment} subclass.
* Use the {@link SportsFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class SportsFragment extends Fragment {

// TODO: Rename parameter arguments, choose names that match


// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters


private String mParam1;
private String mParam2;

public SportsFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment SportsFragment.
*/
// TODO: Rename and change types and number of parameters
public static SportsFragment newInstance(String param1, String param2) {
SportsFragment fragment = new SportsFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sports, container, false);
}
}

fragment_sports.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".SportsFragment">

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A3811C"
android:fontFamily="@font/zilla_slab_bold_italic"
android:paddingTop="30dp"
android:text="Sports Fragment"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="48sp"
android:textStyle="bold" />
</FrameLayout>

fragment_movie.java
package com.example.fragmenttest;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* A simple {@link Fragment} subclass.
* Use the {@link fragment_movie#newInstance} factory method to
* create an instance of this fragment.
*/
public class fragment_movie extends Fragment {

// TODO: Rename parameter arguments, choose names that match


// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters


private String mParam1;
private String mParam2;

public fragment_movie() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ScienceFragment.
*/
// TODO: Rename and change types and number of parameters
public static fragment_movie newInstance(String param1, String param2) {
fragment_movie fragment = new fragment_movie();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_movie, container, false);
}
}

fragment_movie.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".ScienceFragment">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#218E9C"
android:fontFamily="@font/zilla_slab_bold_italic"
android:paddingTop="30dp"
android:text="Movie Fragment"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="48sp"
android:textStyle="bold" />
</FrameLayout>

TASK NO # 04 & 05 (TOAST ARRAY & ADAPTER ARRAY):-

MainActivity.kt

package com.example.toastdemo

import android.app.Activity
import android.content.Context
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.ListView
import android.widget.Toast

class MainActivity : AppCompatActivity() {


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

// Sample data for the ListView


val listItems = arrayOf(
"programming",
"Data Structure",
"Database",
"Python",
"Java",
"Operating System",
"Compiler Design",
"Android Development"
)

// Set up the ListView


val listView: ListView = findViewById(R.id.listView)
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1,
listItems)
listView.adapter = adapter

// Set up a click listener for ListView items


listView.setOnItemClickListener { _, _, position, _ ->
val selectedItem = listItems[position]
val toastMessage = "Clicked on: $selectedItem"
Toast.makeText(this, toastMessage, Toast.LENGTH_SHORT).show()
}
listView.onItemClickListener = AdapterView.OnItemClickListener { _,
view, _, _ ->
view.setBackgroundColor(Color.RED)

}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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"
android:background="#E91E63"
tools:context=".MainActivity">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

TASK NO # 06 (LOGIN PAGE):-

colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>

<color name="pink">#BF6081</color>
</resources>

custom_edittext.xml

<?xml version="1.0" encoding="utf-8"?>


<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<stroke
android:width="2dp"
android:color="@color/design_default_color_secondary_variant"/>

<corners
android:radius="30dp"/>

</shape>

activity_main.xml

<?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:background="@drawable/ic_launcher_background"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
app:cardCornerRadius="30dp"
app:cardElevation="20dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/custom_edittext"
android:orientation="vertical"
android:padding="24dp">

<TextView
android:id="@+id/loginText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="36sp"
android:textStyle="bold" />

<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="40dp"
android:background="@drawable/custom_edittext"
android:drawableLeft="@drawable/ic_launcher_foreground"
android:drawablePadding="8dp"
android:hint="Username"
android:padding="8dp"
android:textColor="@color/black" />

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:background="@drawable/custom_edittext"
android:drawableLeft="@drawable/ic_launcher_foreground"
android:drawablePadding="8dp"
android:hint="Password"
android:inputType="textPassword"
android:padding="8dp"
android:textColor="@color/black" />

<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="30dp"
android:backgroundTint="@color/pink"
android:text="Login"
android:textSize="18sp"
app:cornerRadius="20dp" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

MainActivity.kt

package com.example.sofia6

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.example.sofia6.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

lateinit var username : EditText


lateinit var password: EditText
lateinit var loginButton: Button

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.loginButton.setOnClickListener(View.OnClickListener {
if (binding.username.text.toString() == "user" &&
binding.password.text.toString() == "1234"){
Toast.makeText(this, "Login Successful!",
Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Login Failed!",
Toast.LENGTH_SHORT).show()
}
})
}
}
}

===========THE END===========

You might also like