NAME-Nidhi ROLLNO-17/CS/014 SUBJECT-Android Programming
NAME-Nidhi ROLLNO-17/CS/014 SUBJECT-Android Programming
NAME-Nidhi ROLLNO-17/CS/014 SUBJECT-Android Programming
·1 ROLLNO-17/CS/014
·2 SUBJECT-Android programming
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView)findViewById(R.id.tv1);
t1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String t=t1.getText().toString();
Toast.makeText(MainActivity.this, t,
Toast.LENGTH_LONG).show();
}
});
}
}
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nidhipc.helloworld.MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="30sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
OUTPUT SCREEN
2.Create an application to display various android activity
lifecycle phases.
JAVA CODE
package com.example.nidhipc.activitylifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(),"You are
initialising",Toast.LENGTH_LONG).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(),"You are
Start",Toast.LENGTH_LONG).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(),"You are
Resuming",Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(),"You
paused",Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(),"you
stopped",Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(),"Destorying",Toast.LENGTH_LONG).show();
}
}
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.nidhipc.activitylifecycle.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" />
</android.support.constraint.ConstraintLayout>
OUTPUT SCREEN
3.Create an application with first activity with an editText
and send button .On click of send button,make use of
explicit intent to send text to second activity and display
there in text view.
JAVA CODE
i)First Activity:-
package com.example.nidhipc.intentapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
bt=(Button)findViewById(R.id.button1);
et=(EditText)findViewById(R.id.editText);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent
intent=newIntent(getApplicationContext(),SecondActivity.class);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,et.getText().toString());
startActivity(intent);
}
});
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
b=(Button)findViewById(R.id.button2);
t=(TextView)findViewById(R.id.editText2);
Intent intent=getIntent();
t.setText(intent.getStringExtra(Intent.EXTRA_TEXT));
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SecondActivity.super.onBackPressed();
}
});
}
}
XML CODE
i) First Activity:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nidhipc.intentapp.FirstActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="124dp"
android:text="Send To Second Activity" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THIS IS YOUR FIRST ACTIVITY !"
android:textSize="24sp"
android:layout_above="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginBottom="86dp" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textSize="30sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back To First Activity"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp"
android:layout_marginTop="93dp"
android:layout_alignTop="@+id/editText2"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THIS IS YOUR SECOND ACTIVITY !"
android:textSize="24sp"
android:layout_above="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="54dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textSize="30sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="223dp" />
</RelativeLayout>
OUTPUT SCREEN
4.Create an application with first activity with an editText
and send button .On click of send button,make use of
implicit intent that uses a SEND ACTION and let user
select app from app chooser and navigate to that
application.
JAVA CODE
i) Sender App:-
package com.example.abc.sender;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button)findViewById(R.id.button1);
ed=(EditText)findViewById(R.id.editText1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,ed.getText().toString());
startActivity(intent);
}
});
}
}
import android.app.Activity;
import android.os.Bundle;
second activity
package com.example.abc.receiver;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv=(TextView)findViewById(R.id.textView1);
Intent intent=getIntent();
tv.setText(intent.getStringExtra(Intent.EXTRA_TEXT));
}
}
XML CODE
i) Sender App:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.sender.MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="Send"
android:textSize="24sp"
android:textStyle="bold|italic" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="106dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textColor="@color/colorAccent"
android:textSize="30sp"
android:textStyle="italic" />
</RelativeLayout>
ii)Receiver App:-
first activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.abc.receiver.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" />
</android.support.constraint.ConstraintLayout>
second activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.receiver.Main2Activity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="105dp"
android:layout_marginTop="126dp"
android:text="TextView"
android:textColor="@color/colorAccent"
android:textSize="30sp"
android:textStyle="italic" />
</RelativeLayout>
Manifest.xml File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abc.receiver">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
OUTPUT SCREEN
5.Create spinner with strings taken from resource
folder(res>>value folder) and on changing the spinner
value,image will change.
JAVA CODE
package com.example.abc.spinnerapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner)findViewById(R.id.sp);
iv=(ImageView)findViewById(R.id.i1);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,int
i,long l)
{
if(i==0)
{
Toast.makeText(getApplicationContext(),"You selected
Pizza",Toast.LENGTH_LONG).show();
iv.setImageDrawable(getResources().getDrawable(R.drawable.pizza));
}
else
if(i==1)
{
Toast.makeText(getApplicationContext(),"You selected
Burger",Toast.LENGTH_LONG).show();
iv.setImageDrawable(getResources().getDrawable(R.drawable.burger));
}
else if(i==2)
{
Toast.makeText(getApplicationContext(),"You selected
Momo",Toast.LENGTH_LONG).show();
iv.setImageDrawable(getResources().getDrawable(R.drawable.momo));
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView)
{
iv.setVisibility(View.GONE);
}
});
}
}
XML CODE
strings.xml
<resources>
<string name="app_name">spinnerapp</string>
<array name="Spinner">
<item>Pizza</item>
<item>Burger</item>
<item>Momo</item>
</array>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.spinnerapp.MainActivity">
<Spinner
android:id="@+id/sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="37dp"
android:background="@android:color/background_light"
android:backgroundTint="@android:color/background_light"
android:entries="@array/Spinner" />
<ImageView
android:id="@+id/i1"
android:layout_width="350dp"
android:layout_height="400dp"
android:src="@mipmap/ic_launcher"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="13dp" />
</RelativeLayout>
OUTPUT SCREEN
6.Create a menu with 5 options and selected option
should appear in text box.
JAVA CODE
package com.example.abc.appmenu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(TextView)findViewById(R.id.tv1);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.mainmenu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.i1:
t.setText("About is selected");
break;
case R.id.i2:
t.setText("Crop is selected");
break;
case R.id.i3:
t.setText("Details is selected");
break;
case R.id.i4:
t.setText("Slideshow is selected");
break;
case R.id.i5:
t.setText("Settings is selected");
break;
}
return true;
}
}
XML CODE
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.appmenu.MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="107dp"
android:text=""
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="220dp" />
</RelativeLayout>
mainmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/i1"
android:title="About"/>
<item
android:id="@+id/i2"
android:title="Crop"/>
<item
android:id="@+id/i3"
android:title="Details"/>
<item
android:id="@+id/i4"
android:title="Slideshow"/>
<item
android:id="@+id/i5"
android:title="Settings"/>
</menu>
OUTPUT SCREEN
7.Create a radio button group with radio buttons of all
courses in your college and on selecting a particular
course ,teacher-in-charge of that course should appear at
the bottom of the screen.
JAVA CODE
package com.example.abc.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
RadioGroup rg=null;
RadioButton rb1=null;
RadioButton rb2=null;
RadioButton rb3=null;
TextView tv=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg = (RadioGroup)findViewById(R.id.radiogroup1);
rb1= (RadioButton)findViewById(R.id.radiobutton1);
rb2= (RadioButton)findViewById(R.id.radiobutton2);
rb3 =(RadioButton)findViewById(R.id.radiobutton3);
tv= (TextView)findViewById(R.id.textview1);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup radioGroup, int
i) {
if(i==rb1.getId())
{
tv.setText("The TIC is:Dr Manisha Bansal");
}
if (i==rb2.getId())
{
tv.setText("The TIC is:ABC ");
}
if(i==rb3.getId())
{
tv.setText("The TIC is:XYZ");
}
}
}
);
}
}
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.myapplication.MainActivity">
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/radiogroup1"
android:layout_marginStart="128dp"
android:layout_marginTop="74dp"
android:text=""
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radiogroup1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radiobutton1"
android:text="Computer Science" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radiobutton2"
android:text="Maths" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radiobutton3"
android:text="BMMMC"/>
</RadioGroup>
</RelativeLayout>
OUTPUT SCREEN
8.Create an application with three buttons vertically
aligned,on selecting a button color of the screen will
change.
JAVA CODE
package com.example.abc.trybutton8;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
l.setBackgroundColor(getResources().getColor(R.color.greenCode));
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
l.setBackgroundColor(getResources().getColor(R.color.bluecode));
}
});
}
}
XML CODE
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.trybutton8.MainActivity">
<LinearLayout
android:id="@+id/l1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
tools:layout_editor_absoluteX="14dp"
tools:layout_editor_absoluteY="363dp">
<Button
android:id="@+id/red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Red" />
<Button
android:id="@+id/green"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Green" />
<Button
android:id="@+id/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Blue" />
</LinearLayout>
</RelativeLayout>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="greenCode">#00ff00</color>
<color name="redCode">#ff0000</color>
<color name="bluecode">#0000ff</color>
</resources>
OUTPUT SCREEN
9.Create an application with three buttons horizontally
aligned,on selecting a button color of the screen will
change.
JAVA CODE
package com.example.abc.tryhorizontalbutton9;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
l.setBackgroundColor(getResources().getColor(R.color.greenCode));
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
l.setBackgroundColor(getResources().getColor(R.color.blueCode));
}
});
}
}
XML CODE
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.tryhorizontalbutton9.MainActivity">
<LinearLayout
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/textView"
android:orientation="horizontal">
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Red" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Green" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Blue" />
</LinearLayout>
</RelativeLayout>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="greenCode">#00ff00</color>
<color name="redCode">#ff0000</color>
<color name="blueCode">#0000ff</color>
</resources>
OUTPUT SCREEN
10.Create a login application (check username and
password).On successful login,pop up the message
("Welcome Username").
JAVA CODE
package com.example.abc.loginapp10;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button);
e1=(EditText)findViewById(R.id.ed1);
e2=(EditText)findViewById(R.id.ed2);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String st1=e1.getText().toString();
String st2=e2.getText().toString();
if (st1.equals("Nidhi")&& st2.equals("1030"))
{
Toast.makeText(getApplicationContext(),"Successful
Login",Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(),"Unsuccessful
Login",Toast.LENGTH_LONG).show();
}
});
}
}
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.loginapp10.MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="Username"
android:textSize="24sp"
android:textStyle="bold|italic"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="89dp"
android:text="Password"
android:textSize="24sp"
android:textStyle="bold|italic"
android:layout_below="@+id/tv1"
android:layout_alignParentStart="true" />
<EditText
android:id="@+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/tv1"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<EditText
android:id="@+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:layout_below="@+id/tv2"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ed2"
android:layout_marginStart="14dp"
android:layout_marginTop="40dp"
android:layout_toEndOf="@+id/tv1"
android:text="Log In"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
OUTPUT SCREEN
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button)findViewById(R.id.button1);
bu=(Button)findViewById(R.id.button2);
u1=(EditText)findViewById(R.id.editText1);
p1=(EditText)findViewById(R.id.editText2);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (u1.getText().toString().equals("Nidhi"))
{
if (p1.getText().toString().equals("1030"))
{
Intent
myintent=newIntent(getApplicationContext(),Main2Activity.class);
myintent.setType("text/plain");
myintent.putExtra(Intent.EXTRA_TEXT,u1.getText().toString());
startActivity(myintent);
}
else
{
Toast.makeText(getApplication(),"Loginfailed",Toast.LENGTH_LONG).show();
}
}
}
});
bu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
u1.setText("");
p1.setText("");
}
});
}
}
Second Activity:-
package com.example.abc.login;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv=(TextView)findViewById(R.id.textView);
Intent intent=getIntent();
tv.setText(intent.getStringExtra(Intent.EXTRA_TEXT));
b1=(Button)findViewById(R.id.bu);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog adialog=adbuilder.create();
adialog.show();
}
});
adbuilder=new AlertDialog.Builder(this);
adbuilder.setTitle("Logout");
adbuilder.setMessage("Do u really want to logout?");
adbuilder.setCancelable(true);
adbuilder.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent=new
Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
});
adbuilder.setNegativeButton("No", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.cancel();
}
});
}
}
XML CODE
First Activity:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@color/colorAccent"
android:backgroundTint="@color/colorAccent"
tools:context="com.example.abc.login.MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="21dp"
android:layout_marginTop="125dp"
android:text="Username"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentEnd="true"
android:layout_marginEnd="15dp"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText2"
android:layout_alignStart="@+id/textView1"
android:text="Password"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginStart="20dp"
android:layout_marginTop="53dp"
android:ems="10"
android:inputType="textPassword"
android:textSize="14sp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Log In"
android:textSize="30sp"
android:textStyle="italic"
android:layout_below="@+id/textView2"
android:layout_alignEnd="@+id/textView1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignStart="@+id/editText2"
android:layout_marginStart="25dp"
android:text="Reset"
android:textSize="30sp"
android:textStyle="italic" />
</RelativeLayout>
Second Activity:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@color/colorAccent"
android:backgroundTint="@color/colorAccent"
tools:context="com.example.abc.login.Main2Activity">
<Button
android:id="@+id/bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
android:textSize="30sp"
android:textStyle="italic"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="1dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="56dp"
android:layout_marginTop="165dp"
android:text="Welcome"
android:textSize="30sp"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tv"
android:layout_alignBottom="@+id/tv"
android:layout_toEndOf="@+id/bu"
android:text=""
android:textSize="30sp"
android:textStyle="italic" />
</RelativeLayout>
OUTPUT SCREEN
12.Create an application to create,insert,update and
delete operation on the database.
JAVA CODE
MainActivity.java
package com.example.abc.databaseproject12;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.jar.Attributes;
Main2Activity.java
package com.example.abc.databaseproject12;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
XML CODE
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.databaseproject12.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="118dp"
android:text="DATABASE"
android:textSize="40sp"
android:textStyle="italic"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/tv"
android:layout_below="@+id/tv"
android:layout_marginStart="41dp"
android:layout_marginTop="59dp"
android:text="Update/Delete" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="Show"
android:layout_below="@+id/b1"
android:layout_alignParentStart="true"
android:layout_marginStart="80dp" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="insert"
android:layout_alignBaseline="@+id/b2"
android:layout_alignBottom="@+id/b2"
android:layout_alignParentEnd="true"
android:layout_marginEnd="55dp" />
</RelativeLayout>
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.databaseproject12.Main2Activity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="38dp"
android:text="NAME"
android:textSize="24sp"
tools:layout_editor_absoluteX="34dp"
tools:layout_editor_absoluteY="94dp"
android:layout_above="@+id/ed2"
android:layout_alignStart="@+id/tv2" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASSWORD"
android:textSize="24sp"
tools:layout_editor_absoluteX="34dp"
tools:layout_editor_absoluteY="168dp"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/ed2" />
<EditText
android:id="@+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/tv1"
android:layout_marginEnd="35dp"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:layout_alignBottom="@+id/tv2"
android:layout_alignStart="@+id/ed1" />
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="46dp"
android:layout_marginTop="58dp"
android:text="ADD"
android:textSize="24sp"
android:layout_below="@+id/tv2"
android:layout_toEndOf="@+id/tv1" />
</RelativeLayout>
activity_main3.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.databaseproject12.Main3Activity">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="60dp"
android:text="ID"
android:textSize="24sp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="34dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:text="Name"
android:textSize="24sp"
android:layout_below="@+id/editText"
android:layout_alignStart="@+id/t1" />
<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:textSize="24sp"
android:layout_below="@+id/editText2"
android:layout_alignParentStart="true"
android:layout_marginTop="36dp" />
<Button
android:id="@+id/bu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="99dp"
android:text="Delete"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/t1" />
<Button
android:id="@+id/bu1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="Get Data"
android:layout_above="@+id/bu3"
android:layout_toEndOf="@+id/t2" />
<Button
android:id="@+id/bu3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/bu2"
android:layout_alignBottom="@+id/bu2"
android:layout_toEndOf="@+id/bu1"
android:text="update" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:layout_alignTop="@+id/t1"
android:layout_alignStart="@+id/editText2" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:layout_alignTop="@+id/t2"
android:layout_alignStart="@+id/editText3" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:layout_alignTop="@+id/t3"
android:layout_toEndOf="@+id/t3"
android:layout_marginStart="18dp" />
</RelativeLayout>
OUTPUT SCREEN