0% found this document useful (0 votes)
63 views45 pages

Important Program Statements For Practical Exam of Mobile Application Development

This document provides code examples for 4 programming assignments related to mobile application development using Android Studio. The assignments cover using linear and table layouts to display user data, accepting and displaying user input, and implementing an autocomplete text view for search functionality. Code snippets are provided for XML layout files and a Java activity class to handle user input.

Uploaded by

Varun Jaipurkar
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)
63 views45 pages

Important Program Statements For Practical Exam of Mobile Application Development

This document provides code examples for 4 programming assignments related to mobile application development using Android Studio. The assignments cover using linear and table layouts to display user data, accepting and displaying user input, and implementing an autocomplete text view for search functionality. Code snippets are provided for XML layout files and a Java activity class to handle user input.

Uploaded by

Varun Jaipurkar
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

Mobile Application Development

Important Program Statements for Practical Exam of Mobile Application


Development
1) Write a program to place Name, Age and mobile number linearly (vertical) on
the display screen using linear layout. Exp-5
Ans:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
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=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Age"
android:gravity="center"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="age"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mobile no"
android:gravity="center"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="phone no"/>
</androidx.appcompat.widget.LinearLayoutCompat>

2) Write a program to display 10 students basic information in a table form using


Table Layout. exp-6
ans:activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

Department of Computer Engineering 1 M.M.Polytechnic, Thergaon


Mobile Application Development

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

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="roll no" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dept" />
</TableRow>
<TableRow>

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abc" />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="comp" />
</TableRow>
<TableRow>

<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="def" />

<TextView

Department of Computer Engineering 2 M.M.Polytechnic, Thergaon


Mobile Application Development

android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="456" />

<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="comp" />
</TableRow><!--add more<TableRow>to add 10 students data-->
</TableLayout>

3) Write a program to accept and display personal information of the student.


Exp-7
Ans:activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="name" />

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="age" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />

<TextView
android:id="@+id/textView3"

Department of Computer Engineering 3 M.M.Polytechnic, Thergaon


Mobile Application Development

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="department" />

<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
/>

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="submit"
android:onClick="submit"
/>

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

</LinearLayout>

Mainactivity.java
package com.example.username;

import androidx.appcompat.app.AppCompatActivity;

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

Department of Computer Engineering 4 M.M.Polytechnic, Thergaon


Mobile Application Development

public class MainActivity extends AppCompatActivity {


private EditText t1;
private EditText t2;
private EditText t3;
private TextView o1;
private TextView o2;
private TextView o3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=findViewById(R.id.editText1);
t2=findViewById(R.id.editText2);
t3=findViewById(R.id.editText3);
o1=findViewById(R.id.textView4);
o2=findViewById(R.id.textView5);
o3=findViewById(R.id.textView6);
}

public void submit(View view) {


String name=t1.getText().toString();
String age=t2.getText().toString();
String dept=t3.getText().toString();
o1.setText(name);
o2.setText(age);
o3.setText(dept);
}
}

4) Write a program to create a first display screen of any search engine using
auto Complete Text View.exp-8
Ans: Xml code
<?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="Enter semister"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.471"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.186" />

<AutoCompleteTextView

Department of Computer Engineering 5 M.M.Polytechnic, Thergaon


Mobile Application Development

android:id="@+id/autoCompleteTextView"
android:layout_width="301dp"
android:layout_height="57dp"
android:hint="1-6"
android:text=" "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.458"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.306" />

<TextView
android:id="@+id/textView"
android:layout_width="222dp"
android:layout_height="288dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.866" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java code
package com.example.autocomplet2;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
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.TextView;
import java.lang.reflect.Array;
public class MainActivity extends AppCompatActivity {
String[] sem={"first","second","third","fourth","fifth","sixth"};
AutoCompleteTextView tv;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter=new
ArrayAdapter<String>(this,android.R.layout.select_dialog_item,sem);
tv=findViewById(R.id.autoCompleteTextView);
t1=findViewById(R.id.textView);
tv.setThreshold(1);
tv.setAdapter(adapter);
tv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int i, long l) {
if(tv.getText().toString().equals("sixth")) {

Department of Computer Engineering 6 M.M.Polytechnic, Thergaon


Mobile Application Development

t1.setText("MAD\nMAN\nETI\nPWP\nNIS\nCPP");
t1.setTextSize(18);
tv.setTextColor(Color.RED);
}
}
});
}
}

5) Write a program to create a toggle button to display on/off functionality on


the display screen.exp-9
Ans:activitymain.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">

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textOff="off"
android:textOn="on"
android:onClick="clicked"/>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle button "
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.279" />
</androidx.constraintlayout.widget.ConstraintLayout>

Mainactivity.java
package com.example.togglebutton;

import androidx.appcompat.app.AppCompatActivity;

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

Department of Computer Engineering 7 M.M.Polytechnic, Thergaon


Mobile Application Development

import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {


TextView t1;
ToggleButton tb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=findViewById(R.id.textView);
tb=findViewById(R.id.toggleButton);
}

public void clicked(View view) {


if (tb.isChecked()){
t1.setText("toggle button is on ");
}
else {
t1.setText("toggle button is off");
}
}
}

6) Write a program to create a login form for a social networking site.exp-10


Ans: Xml code
<?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">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="login"
android:text="Login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.38"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2"
app:layout_constraintVertical_bias="0.222" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Department of Computer Engineering 8 M.M.Polytechnic, Thergaon


Mobile Application Development

android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.278"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.297" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.103"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.146" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.104"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.304" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.292"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintVertical_bias="0.102" />

<TextView
android:id="@+id/textView3"
android:layout_width="293dp"
android:layout_height="44dp"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.177"

Department of Computer Engineering 9 M.M.Polytechnic, Thergaon


Mobile Application Development

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintVertical_bias="0.26" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java code
package com.example.loginp1;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

public void login(View view){

Button b1=(Button) findViewById(R.id.button);

EditText u1=(EditText) findViewById(R.id.editText);

EditText p1=(EditText) findViewById(R.id.editText2);

TextView t1=(TextView) findViewById(R.id.textView3);

String username="aj";

String Password="123";

String input1=u1.getText().toString();

Department of Computer Engineering 10 M.M.Polytechnic, Thergaon


Mobile Application Development

String input2=p1.getText().toString();

if (username.equals(input1)&&Password.equals(input2))

t1.setText("Login successfully...");

else

t1.setText("login unsuccessfull.");

7) Write a program to show five checkboxes and toast selected checkboxes.exp-


11
Ans: Xml code
<?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:id="@+id/textView"
android:layout_width="257dp"
android:layout_height="33dp"
android:textAlignment="center"
android:text="Select Language"
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.11" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/checkBox2"

Department of Computer Engineering 11 M.M.Polytechnic, Thergaon


Mobile Application Development

app:layout_constraintHorizontal_bias="0.645"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.069" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Python"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.655"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.069" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".Net"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.286"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.215" />

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.591"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.215" />

<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Html"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.468"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.348" />
</androidx.constraintlayout.widget.ConstraintLayout>

Department of Computer Engineering 12 M.M.Polytechnic, Thergaon


Mobile Application Development

Java code
package com.example.checkbox;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.CheckBox;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

CheckBox cb1,cb2,cb3,cb4,cb5;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

cb1=findViewById(R.id.checkBox);

cb2=findViewById(R.id.checkBox2);

cb3=findViewById(R.id.checkBox3);

cb4=findViewById(R.id.checkBox4);

cb5=findViewById(R.id.checkBox5);

cb1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

if (cb1.isChecked()){

Toast.makeText(MainActivity.this,cb1.getText().toString()+" is

Department of Computer Engineering 13 M.M.Polytechnic, Thergaon


Mobile Application Development

checked",Toast.LENGTH_SHORT).show();

else

Toast.makeText(MainActivity.this,cb1.getText().toString()+" is
unchecked",Toast.LENGTH_SHORT).show();

});

cb2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

if (cb2.isChecked()){

Toast.makeText(MainActivity.this,cb2.getText().toString()+" is
checked",Toast.LENGTH_SHORT).show();

else

Toast.makeText(MainActivity.this,cb2.getText().toString()+" is
unchecked",Toast.LENGTH_SHORT).show();

});

cb3.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

if (cb3.isChecked()){

Department of Computer Engineering 14 M.M.Polytechnic, Thergaon


Mobile Application Development

Toast.makeText(MainActivity.this,cb3.getText().toString()+" is
checked",Toast.LENGTH_SHORT).show();

else

Toast.makeText(MainActivity.this,cb3.getText().toString()+" is
unchecked",Toast.LENGTH_SHORT).show();

});

cb4.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

if (cb4.isChecked()){

Toast.makeText(MainActivity.this,cb4.getText().toString()+" is
checked",Toast.LENGTH_SHORT).show();

else

Toast.makeText(MainActivity.this,cb4.getText().toString()+" is
unchecked",Toast.LENGTH_SHORT).show();

});

cb5.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Department of Computer Engineering 15 M.M.Polytechnic, Thergaon


Mobile Application Development

if (cb5.isChecked()){

Toast.makeText(MainActivity.this,cb5.getText().toString()+" is
checked",Toast.LENGTH_SHORT).show();

else

Toast.makeText(MainActivity.this,cb5.getText().toString()+" is
unchecked",Toast.LENGTH_SHORT).show();

});

8) Write a program to show functionality of radio group and radio buttons to


toast selected gender.exp-12
Ans: Xml code
<?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:id="@+id/textView"
android:layout_width="248dp"
android:layout_height="41dp"
android:text="Single radio buttons"
android:textAlignment="center"
android:textColor="#9C27B0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.091" />

Department of Computer Engineering 16 M.M.Polytechnic, Thergaon


Mobile Application Development

<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.27"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.218" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.27"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.31" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="258dp"
android:layout_height="135dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.516"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.545">

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio button inside radiogroup"
android:textAlignment="center"
android:textColor="#9C27B0" />

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="male" />

<RadioButton
android:id="@+id/radioButton4"
android:layout_width="match_parent"
android:layout_height="46dp"

Department of Computer Engineering 17 M.M.Polytechnic, Thergaon


Mobile Application Development

android:text="female" />
</RadioGroup>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show selected"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.781" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java code
package com.example.radiobutton;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

RadioButton rb1,rb2,rb3;

RadioGroup rg1;

Button b1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

rb1=findViewById(R.id.radioButton);

Department of Computer Engineering 18 M.M.Polytechnic, Thergaon


Mobile Application Development

rb2=findViewById(R.id.radioButton2);

rg1=findViewById(R.id.radioGroup);

b1=findViewById(R.id.button);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

if (rb1.isChecked()){

Toast.makeText(MainActivity.this,rb1.getText().toString()+" is
clicked",Toast.LENGTH_SHORT).show();

else if (rb2.isChecked()){

Toast.makeText(MainActivity.this,rb2.getText().toString()+" is
clicked",Toast.LENGTH_SHORT).show();

int id=rg1.getCheckedRadioButtonId();

rb3=findViewById(id);

Toast.makeText(MainActivity.this,rb3.getText().toString()+" is
clicked",Toast.LENGTH_SHORT).show();

});

9) Write a program to display and image using image view and a button named
as “Change Image”. Once you click on button another image should get
displayed.exp-14
Ans: Xml code

Department of Computer Engineering 19 M.M.Polytechnic, Thergaon


Mobile Application Development

<?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="324dp"
android:layout_height="339dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.609"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.104"
app:srcCompat="@drawable/pexel" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

Java code
package com.example.changeimg;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

Department of Computer Engineering 20 M.M.Polytechnic, Thergaon


Mobile Application Development

public class MainActivity extends AppCompatActivity {

Button b1;

ImageView ig;

public int count=1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

b1=findViewById(R.id.button);

ig=findViewById(R.id.imageView);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

if(count==0){

ig.setImageResource(R.drawable.pexel);

count=1;

else if (count==1) {

ig.setImageResource(R.drawable.img2);

count=0;

});

10) Write a program to display 15 buttons using grid view.exp-14

Department of Computer Engineering 21 M.M.Polytechnic, Thergaon


Mobile Application Development

Ans: Xml code


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

<GridView
android:id="@+id/gridview"
android:numColumns="3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="20dp"
android:verticalSpacing="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >

</GridView>
</androidx.constraintlayout.widget.ConstraintLayout>

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

<Button

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button"

/>

Java code
package com.example.buttons15gridviewpg73p3;

import androidx.appcompat.app.AppCompatActivity;

Department of Computer Engineering 22 M.M.Polytechnic, Thergaon


Mobile Application Development

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.GridView;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

String
buttons[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","1
5"};

GridView gv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ArrayAdapter adapter=new ArrayAdapter<String


>(this,R.layout.gridview,buttons);

gv=findViewById(R.id.gridview);

gv.setAdapter(adapter);

11) Write a program to display three checkboxes and one button named
”Order” as shown below. Once you click on button it should toast different
selected checkboxes along with items individual and total price.exp-15

Department of Computer Engineering 23 M.M.Polytechnic, Thergaon


Mobile Application Development

Ans: Xml code


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

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="232dp"
android:layout_height="56dp"
android:text="Pizza"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.469"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.236" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="232dp"
android:layout_height="56dp"
android:text="Coffee"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.469"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.37" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="232dp"
android:layout_height="56dp"
android:text="Burger"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.469"

Department of Computer Engineering 24 M.M.Polytechnic, Thergaon


Mobile Application Development

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.485" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ORDER"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.781" />
</androidx.constraintlayout.widget.ConstraintLayout>

Java code
package com.example.threecheckboxtoastpg78p1;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {


CheckBox cb1,cb2,cb3;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1=findViewById(R.id.checkBox1);
cb2=findViewById(R.id.checkBox2);
cb3=findViewById(R.id.checkBox3);
b1=findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int total=0;
StringBuilder result=new StringBuilder();
result.append("selected items:");
if(cb1.isChecked()){
result.append("\nPizza 100Rs");
total+=100;

Department of Computer Engineering 25 M.M.Polytechnic, Thergaon


Mobile Application Development

}
if (cb2.isChecked()){
result.append("\nCoffe 50Rs");
total+=50;
}
if (cb3.isChecked()){
result.append("\nBurger 120Rs");
total+=120;
}
result.append("\nTOtal:"+total+"Rs");

Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT)
.show();
}
});
}
}

12) Write a program to display following output. Use Time Picker with
Spinner mode.exp-16

Ans: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"
tools:context=".MainActivity">

<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:timePickerMode="spinner"/>

Department of Computer Engineering 26 M.M.Polytechnic, Thergaon


Mobile Application Development

<TimePicker
android:id="@+id/timePicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="160dp"
android:timePickerMode="spinner"/>

<TimePicker
android:id="@+id/timePicker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="360dp" />
</RelativeLayout>

MainActivity.java
package com.example.timepicker;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

TimePicker t1,t2,t3;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

t1=(TimePicker)findViewById(R.id.timePicker);

t2=(TimePicker)findViewById(R.id.timePicker2);

t3=(TimePicker)findViewById(R.id.timePicker3);

t1.setIs24HourView(true);

t2.setIs24HourView(false);

t3.setIs24HourView(true);

Department of Computer Engineering 27 M.M.Polytechnic, Thergaon


Mobile Application Development

13) Write a program to create a Hello World activity using all lifecycles
methods to display messages using Log.d .exp-17
Ans: <?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_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Java code
package com.example.activitylifecyclelogpg91p1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

public class MainActivity extends AppCompatActivity {

public String tag="application is:";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Log.d(tag,"Created");

Department of Computer Engineering 28 M.M.Polytechnic, Thergaon


Mobile Application Development

@Override

protected void onStart() {

super.onStart();

Log.d(tag,"Started");

@Override

protected void onResume() {

super.onResume();

Log.d(tag,"Resumed");

@Override

protected void onPause() {

super.onPause();

Log.d(tag,"Paused");

@Override

protected void onStop() {

super.onStop();

Log.d(tag,"Stoped");

@Override

protected void onDestroy() {

Department of Computer Engineering 29 M.M.Polytechnic, Thergaon


Mobile Application Development

super.onDestroy();

Log.d(tag,"Destroyed");

@Override

protected void onRestart() {

super.onRestart();

Log.d(tag,"Restarted");

14) Write a program to create button “Start Dialer”. When u click on this
button it should open the phone dialer.exp-18
 Ans: 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"

tools:context=".MainActivity">

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="300dp"

android:layout_marginLeft="150dp"

Department of Computer Engineering 30 M.M.Polytechnic, Thergaon


Mobile Application Development

android:text="Start Dialer" />

</RelativeLayout>

 MainActivity.java
package com.example.dialer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button b1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

b1=(Button)findViewById(R.id.button);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(Intent.ACTION_DIAL);

intent.setData(Uri.parse("tel:" + "+91
9657991378"));

startActivity(intent);

Department of Computer Engineering 31 M.M.Polytechnic, Thergaon


Mobile Application Development

});

15) Create sample application with login module.(check username and


password) on successful login, Change text view “login successful” and on
login fail, alert user using Toast ”Login Fail”.exp-27
Ans: Activitymain.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="LOGIN FORM"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.101"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="User-name"
android:inputType="textPersonName"
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.317" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"

Department of Computer Engineering 32 M.M.Polytechnic, Thergaon


Mobile Application Development

android:inputType="textPassword"
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.556" />

<TextView
android:id="@+id/textView"
android:layout_width="121dp"
android:layout_height="21dp"
android:text="USERNAME"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.341"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.225" />

<TextView
android:id="@+id/textView2"
android:layout_width="121dp"
android:layout_height="21dp"
android:text="PASSWORD"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.341"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.439" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submit"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.742" />

</androidx.constraintlayout.widget.ConstraintLayout>

Mainactivity.java
package com.example.loginformpg143p1;

Department of Computer Engineering 33 M.M.Polytechnic, Thergaon


Mobile Application Development

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;

public class MainActivity extends AppCompatActivity {

EditText uname,password;

Button b1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

uname=findViewById(R.id.editText);

password=findViewById(R.id.editText2);

b1=findViewById(R.id.button);

public void submit(View view) {

if (uname.getText().toString().equals("ankur")){

if (password.getText().toString().equals("hi123")){

Toast.makeText(getApplicationContext(),"login
Successful",Toast.LENGTH_SHORT).show();

Department of Computer Engineering 34 M.M.Polytechnic, Thergaon


Mobile Application Development

else {

Toast.makeText(getApplicationContext(),"login
Unseccessful",Toast.LENGTH_SHORT).show();

16) Write a program to create the login form with necessary validations
like length of username and password, empty text fields, count of unsuccessful
login attempts. Display the login successful/unsuccessful toastmessage.exp-28
Ans: Activitymain.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:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOGIN FORM"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.182"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.057" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USERNAME"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.187"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"

Department of Computer Engineering 35 M.M.Polytechnic, Thergaon


Mobile Application Development

app:layout_constraintVertical_bias="0.216" />

<EditText
android:id="@+id/editText"
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.282"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.306" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASSWORD"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.173"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.435" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.297"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.542" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"
android:textSize="18dp"
android:onClick="submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.188"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"

Department of Computer Engineering 36 M.M.Polytechnic, Thergaon


Mobile Application Development

app:layout_constraintVertical_bias="0.689" />

</androidx.constraintlayout.widget.ConstraintLayout>

Mainactivity.java
package com.example.loginvalidationspg148p1;

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;

public class MainActivity extends AppCompatActivity {

EditText uname,password;

Button b1;

String userin,passin;

int count;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

uname=findViewById(R.id.editText);

password=findViewById(R.id.editText2);

public void submit(View view) {

userin=uname.getText().toString();

Department of Computer Engineering 37 M.M.Polytechnic, Thergaon


Mobile Application Development

passin=password.getText().toString();

if(!userin.isEmpty()){

if (userin.length()>=5){

if (userin.equals("ankur")){

if (!passin.isEmpty()){

if (passin.length()>=8){

if (passin.equals("ankur2000")){

count+=1;

Toast.makeText(getApplicationContext(),"Login sccessful\n after number of


attempts:"+count,Toast.LENGTH_SHORT).show();

else {

count+=1;

Toast.makeText(getApplicationContext(),"Login unsccessful\ninvalid
password\n number of attempts:"+count,Toast.LENGTH_SHORT).show();

else {

count+=1;

Toast.makeText(getApplicationContext(),"Login
unsccessful\npassword must be atleast 8 length\n number of
attempts:"+count,Toast.LENGTH_SHORT).show();

else {

count+=1;

Toast.makeText(getApplicationContext(),"Login
unsccessful\nenter password\n number of
attempts:"+count,Toast.LENGTH_SHORT).show();

Department of Computer Engineering 38 M.M.Polytechnic, Thergaon


Mobile Application Development

else {

count+=1;

Toast.makeText(getApplicationContext(),"Login
unsccessful\nenter valid username\n number of
attempts:"+count,Toast.LENGTH_SHORT).show();

else {

count+=1;

Toast.makeText(getApplicationContext(),"Login
unsccessful\nuser name must be atleast 5 length\n number of
attempts:"+count,Toast.LENGTH_SHORT).show();

else {

count+=1;

Toast.makeText(getApplicationContext(),"Login
unsccessful\nenter username\n number of
attempts:"+count,Toast.LENGTH_SHORT).show();

17) Write a program to display following output using Relative layout.

Department of Computer Engineering 39 M.M.Polytechnic, Thergaon


Mobile Application Development

 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andr
oid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Button1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Button2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"

Department of Computer Engineering 40 M.M.Polytechnic, Thergaon


Mobile Application Development

android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Button3" />

<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button4" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn2"
android:layout_centerHorizontal="true"
android:text="Button5" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn4"
android:layout_centerHorizontal="true"
android:text="Button6" />
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/btn1"
android:layout_toRightOf="@+id/btn1"
android:layout_alignParentRight="true"
android:text="Button7" />
</RelativeLayout>

18) Write a program to display following output using Table layout.

 activity_main.xml

Department of Computer Engineering 41 M.M.Polytechnic, Thergaon


Mobile Application Development

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:stretchColumns="1">

<TableRow android:padding="5dip">

<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_span="2"
android:gravity="center_horizontal"
android:text="@string/loginForm"
android:textColor="#0ff"
android:textSize="25sp"
android:textStyle="bold" />
</TableRow>

<TableRow>

<TextView
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:text="@string/userName"
android:textColor="#fff"
android:textSize="16sp" />

<EditText
android:id="@+id/userName"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginLeft="10dp"
android:background="#fff"
android:hint="@string/userName"
android:padding="5dp"
android:textColor="#000" />
</TableRow>

<TableRow>

<TextView

Department of Computer Engineering 42 M.M.Polytechnic, Thergaon


Mobile Application Development

android:layout_height="wrap_content"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="@string/password"
android:textColor="#fff"
android:textSize="16sp" />

<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="#fff"
android:hint="@string/password"
android:padding="5dp"
android:textColor="#000" />
</TableRow>

<TableRow android:layout_marginTop="20dp">

<Button
android:id="@+id/loginBtn"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_span="2"
android:background="#0ff"
android:text="@string/login"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>

19) Write a program to display following output using Relative layout.

Department of Computer Engineering 43 M.M.Polytechnic, Thergaon


Mobile Application Development

 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
android:id="@+id/btnButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>

<Button
android:id="@+id/btnButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_toRightOf="@+id/btnButton1"/>

<Button
android:id="@+id/btnButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_below="@+id/btnButton1"/>

Department of Computer Engineering 44 M.M.Polytechnic, Thergaon


Mobile Application Development

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnButton3"
android:layout_marginTop="94dp"
android:text="User :"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:layout_toRightOf="@+id/btnButton3" />

<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:text="Submit" />

</RelativeLayout>

Department of Computer Engineering 45 M.M.Polytechnic, Thergaon

You might also like