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

Mad Practical 8 9 10

The document contains XML and Java code for three practical Android applications. Practical 8 features an AutoCompleteTextView for country selection, Practical 9 includes a ToggleButton with state change handling, and Practical 10 presents a simple login interface with username and password fields. Each practical demonstrates different UI components and their interactions in an Android app.

Uploaded by

gaurangrane4
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)
20 views8 pages

Mad Practical 8 9 10

The document contains XML and Java code for three practical Android applications. Practical 8 features an AutoCompleteTextView for country selection, Practical 9 includes a ToggleButton with state change handling, and Practical 10 presents a simple login interface with username and password fields. Each practical demonstrates different UI components and their interactions in an Android app.

Uploaded by

gaurangrane4
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
You are on page 1/ 8

Practical 8

xml code
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">

<!-- AutoCompleteTextView for displaying suggestions -->


<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter country name"
android:dropDownHeight="200dp"
android:padding="16dp" />

<!-- Button to show the selected item -->


<Button
android:id="@+id/showSelectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected Country"
android:layout_marginTop="20dp" />

</LinearLayout>
Java code
package com.example.practical8;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private AutoCompleteTextView autoCompleteTextView;


private Button showSelectionButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Link to the XML layout

// Initialize the views


autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
showSelectionButton = findViewById(R.id.showSelectionButton);

// Create a list of suggestions for the AutoCompleteTextView


String[] countries = new String[] {
"India", "United States", "Australia", "Canada", "Germany", "Brazil",
"United Kingdom", "France", "Italy", "Spain", "Russia", "China", "Japan"
};

// Create an ArrayAdapter for AutoCompleteTextView


ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, countries);

// Set the adapter to AutoCompleteTextView


autoCompleteTextView.setAdapter(adapter);

// Set an item click listener to handle when a user selects a suggestion


autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedCountry = parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, "You selected: " + selectedCountry,
Toast.LENGTH_SHORT).show();
}
});

// Set a listener for the button to show the selected item


showSelectionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the current text in AutoCompleteTextView
String selectedCountry = autoCompleteTextView.getText().toString();

if (!selectedCountry.isEmpty()) {
Toast.makeText(MainActivity.this, "You selected: " + selectedCountry,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "No country selected",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Practical 9
xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_marginBottom="20dp" />

<ToggleButton
android:id="@+id/myToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:textOff="OFF"
android:layout_marginBottom="20dp"
android:checked="false"/>

<ImageButton
android:id="@+id/myImageButton"
android:layout_width="319dp"
android:layout_height="383dp"
android:contentDescription="Image Button"
android:scaleType="fitCenter"
android:src="@drawable/img_1" />

</LinearLayout>
Java code
package com.example.practical9;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Inflate the layout

// Find the ToggleButton by its ID


ToggleButton myToggleButton = findViewById(R.id.myToggleButton);

// Set an OnCheckedChangeListener to handle the toggle state change


myToggleButton.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Check the toggle state (ON/OFF)
if (isChecked) {
// If the toggle is ON
Toast.makeText(MainActivity.this, "Toggle ON", Toast.LENGTH_SHORT).show();
} else {
// If the toggle is OFF
Toast.makeText(MainActivity.this, "Toggle OFF", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Practical 10
<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="25dp"
android:orientation="vertical"
android:layout_marginTop="160">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Username"
android:textSize="25dp"
android:textColor="@color/black"/>

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter valid username"/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Password"
android:textSize="25dp"
android:textColor="@color/black"
android:layout_marginTop="29dp"/>

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter password"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center"
android:layout_marginTop="29dp"
android:textSize="20"/>
</LinearLayout>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy