Вы находитесь на странице: 1из 44

INHERITANCE PROGRAM

 Single Level Inheritance

import java.io.*;
public class Shape
{
int length;
int breadth;
}
public class Rectangle extends Shape
{
int area;
public void calcualteArea()
{
area = length*breadth;
}
public static void main(String args[])
{
Rectangle r = new Rectangle();
r.length = 10;
r.breadth = 20;
r.calcualteArea();
System.out.println("The Area of rectangle of length \" +r.length+"\" and breadth \""+r.breadth+"\”+”is”+ r.area);
}
}

Output :

The Area of rectangle of length "10" and breadth "20" is 200


 Multilevel Inheritance Example
import java.io.*;
class A
{
void bird()
{
System.out.println("Fly");
}
}
class B extends A
{
void fish()
{
System.out.println("Swim");
}
}
class C extends B
{
void tiger()
{
System.out.println(“Run");}
}
class Bscit
{
public static void main(String args[])
{
tiger d=new tiger();
d.tiger();
d.fish();
d.bird();
}}

Output:
Run

Swim

Fly
 Hierarchical Inheritance Example
import java.io.*;
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}
class Bscit
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
}}

Output:

mewoing...

eating...
 Hybrid Inheritance

import java.io.*;
public class A
{
public void dispA()
{
System.out.println("disp() method of Class A");
}
}
public interface B
{
public void show();
}
public interface C
{
public void show();
}
public class extends A implements B,C
{
public void show()
{
System.out.println("show() method implementation");
}
public void dispD()
{
System.out.println("disp() method of Class D");
}
public static void main(String args[])
{
D d = new D();
d.dispD();
d.show();
}
}

Output :

disp() method of Class D


show() method implementation

.
Abstract Class Program
import java.io.*;
abstract class Base
{
abstract void fun();
}
class Derived extends Base
{
void fun()
{
System.out.println("Derived fun() called");
}
}
class Main
{
public static void main(String args[])
{
Base b = new Derived();
b.fun();
}
}

Output:
Derived fun() called
Interface Program

import java.io.*;

interface A
{
void show();
}
class B implements A
{
public void show()
{
System.out.println("interface is implemented");
}
}
class testing
{
public static void main(String args[])
{
B obj=new B(); obj.show();
}
}

OUTPUT:

interface is implemented
Overloading Program

public class Sum


{
public int sum(int x, int y)
{
return (x + y);
}
public int sum(int x, int y, int z)
{
return (x + y + z);
}
public double sum(double x, double y)
{
return (x + y);
}
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}

Output :
30
60
31.0
Java Constructor Program
class ConsMain {
private int x;

// constructor
private ConsMain(){
System.out.println("Constructor Called");
x = 5;
}

public static void main(String[] args){


ConsMain obj = new ConsMain();
System.out.println("Value of x = " + obj.x);
}
}

Output:

Constructor Called
Value of x = 5
Overriding Program

class Parent
{
void show()
{
System.out.println("Parent's show()");
}
}
class Child extends Parent
{
void show()
{
System.out.println("Child's show()");
}
}
class Main
{
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.show();
Parent obj2 = new Child();
obj2.show();
}
}

Output:

Parent's show()

Child's show()
Thread Program by extending Thread class

class Multi extends Thread


{
public void run()
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
Multi t1=new Multi();
t1.start();
}
}

Output:
thread is running...

Thread Program by implementing Runnable interface

class Multi3 implements Runnable


{
public void run()
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}

Output:
thread is running...
Write a program to use finally keyword
Import java.util.*;
class TestFinallyBlock
{
public static void main(String args[])
{
Try
{
int data=25/5; System.out.println(data);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}

OUTPUT:

Finally block is always executed


rest of the code...
Write a program to use super in java.
Import java.util.*;
class Vehicle
{
int maxSpeed = 120;
}
class Car extends Vehicle
{
int maxSpeed = 180;
void display()
{
System.out.println("Maximum Speed: " + super.maxSpeed);
}
}
class Testo
{
public static void main(String[] args)
{
Car small = new Car(); small.display();
}
}

OUTPUT:
Maximum Speed : 120
Write a program to handle exceptions encountered in java.

Import java.util.*;
class JavaException
{
public static void main(String args[])
{
int d = 0; int n = 20;
try
{
int fraction = n / d;
System.out.println("This line will not be Executed");
}
catch (ArithmeticException e)
{
System.out.println("In the catch Block due to Exception = " + e);
}
System.out.println("End Of Main");
}
}

OUTPUT:

In the catch Block Due to exception = java.lang.ArithmeticException: / by zero


End of Main
Hello world app using android studio

 Android Layout File (activity_main.xml)


<?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.tutlane.helloworld.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>

 Android Manifest File (AndroidManifest.xml)


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.helloworld" >
<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" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

OUTPUT:
Addition of two numbers

 The 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=".MainActivity"
tools:layout_editor_absoluteY="81dp">

<!-- Text view for result view-->


<TextView
android:id="@+id/textView_answer"
android:layout_width="100dp"
android:layout_height="25dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="300dp"
android:text="0"
android:textSize="20dp"
android:textStyle="bold" />

<!--take the input first number-->


<EditText
android:id="@+id/editText_first_no"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="40dp"
android:inputType="number" />
<!-- for messege input first number-->
<TextView
android:id="@+id/textView_first_no"
android:layout_width="150dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp"
android:text="First number"
android:textSize="20dp" />

<!--view messege -->


<TextView
android:id="@+id/textView_second_no"
android:layout_width="150dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="100dp"
android:text="Second number"
android:textSize="20dp" />

<!-- take input for second number -->


<EditText
android:id="@+id/editText_second_no"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="90dp"
android:inputType="number"
tools:ignore="MissingConstraints" />

<!-- button for run add logic and view result -->

<Button
android:id="@+id/add_button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="200dp"
android:text="ADD" />

</RelativeLayout>

package org.geeksforgeeks.addtwonumbers;
import android.support.v7.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 {

EditText number1;
EditText number2;
Button Add_button;
TextView result;
int ans=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1=(EditText) findViewById(R.id.editText_first_no);
number2=(EditText) findViewById(R.id.editText_second_no);
Add_button=(Button) findViewById(R.id.add_button);
result = (TextView) findViewById(R.id.textView_answer);
Add_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
double sum = num1 + num2;
result.setText(Double.toString(sum));
}
}
}
}
OUTPUT:
Calculator using Android Studio
 activity_main.xml

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

<TextView
android:id="@+id/txtScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="right|center_vertical"
android:maxLength="16"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp"
android:typeface="serif" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/txtScreen"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/btnSeven"
android:text="7" />
<Button
android:id="@+id/btnEight"
android:text="8" />
<Button
android:id="@+id/btnNine"
android:text="9"/>
<Button
android:id="@+id/btnDivide"
android:text="/"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/btnFour"
android:text="4"/>
<Button
android:id="@+id/btnFive"
android:text="5" />
<Button
android:id="@+id/btnSix"
android:text="6" />
<Button
android:id="@+id/btnMultiply"
android:text="*" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/btnOne"
android:text="1" />
<Button
android:id="@+id/btnTwo"
android:text="2" />
<Button
android:id="@+id/btnThree"
android:text="3" />
<Button
android:id="@+id/btnSubtract"
android:text="-" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/btnDot"
android:text="." />
<Button
android:id="@+id/btnZero"
android:text="0" />
<Button
android:id="@+id/btnClear"
android:text="C" />
<Button
android:id="@+id/btnAdd"
android:text="+" />
</LinearLayout>

<Button
android:id="@+id/btnEqual"
android:text="=" />
</LinearLayout>
</RelativeLayout>

 button.xml

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


<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<gradient android:angle="90" android:endColor="#FFFFFF" android:startColor="#9EB8FF" android:type="linear" />
<padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp" />
<size android:width="60dp" android:height="60dp" />
<stroke android:width="1dp" android:color="#ff3da6ef" />
</shape>
</item>
<item>
<shape>
<gradient android:angle="90" android:endColor="#FFFFFF" android:startColor="#ffd9d9d9" android:type="linear" />
<padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp" />
<size android:width="60dp" android:height="60dp" />
<stroke android:width="0.5dp" android:color="#ffcecece" />
</shape>
</item>
</selector>
 MainActivity.java

package com.javahelps.calculator;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;

public class MainActivity extends ActionBarActivity


{
private int[] numericButtons = {R.id.btnZero, R.id.btnOne, R.id.btnTwo, R.id.btnThree, R.id.btnFour,
R.id.btnFive, R.id.btnSix, R.id.btnSeven, R.id.btnEight, R.id.btnNine
};
private int[] operatorButtons = {R.id.btnAdd, R.id.btnSubtract, R.id.btnMultiply, R.id.btnDivide
};
private TextView txtScreen;
private boolean lastNumeric;
private boolean stateError;
private boolean lastDot;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.txtScreen = (TextView) findViewById(R.id.txtScreen);
setNumericOnClickListener();
setOperatorOnClickListener();
}

* Find and set OnClickListener to numeric buttons.

private void setNumericOnClickListener()


{
View.OnClickListener listener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Button button = (Button) v;
if (stateError)
{
txtScreen.setText(button.getText());
stateError = false;
}
else
{
txtScreen.append(button.getText());
}
lastNumeric = true;
}
};
for (int id : numericButtons)
{
findViewById(id).setOnClickListener(listener);
}
}

* Find and set OnClickListener to operator buttons, equal button and decimal point button.

private void setOperatorOnClickListener()


{
View.OnClickListener listener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (lastNumeric && !stateError)
{
Button button = (Button) v;
txtScreen.append(button.getText());
lastNumeric = false;
lastDot = false;
}
}
};
for (int id : operatorButtons)
{
findViewById(id).setOnClickListener(listener);
}
findViewById(R.id.btnDot).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (lastNumeric && !stateError && !lastDot)
{
txtScreen.append(".");
lastNumeric = false;
lastDot = true;
}
}
});
findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
txtScreen.setText("");
lastNumeric = false;
stateError = false;
lastDot = false;
}
});
findViewById(R.id.btnEqual).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
onEqual();
}
});
}

* Logic to calculate the solution.

private void onEqual()


{
if (lastNumeric && !stateError)
{
String txt = txtScreen.getText().toString();
Expression expression = new ExpressionBuilder(txt).build();
try
{
double result = expression.evaluate();
txtScreen.setText(Double.toString(result));
lastDot = true;
}
catch (ArithmeticException ex)
{
txtScreen.setText("Error");
stateError = true;
lastNumeric = false;
}
}
}
}
OUTPUT:
Android Implicit Intent Program
 activity_main.xml
<?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="example.javatpoint.com.implicitintent.MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:ems="10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.575"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginLeft="156dp"
android:layout_marginTop="172dp"
android:text="Visit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>

 Activity class
package example.javatpoint.com.implicitintent;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity


{
Button button;
EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String url=editText.getText().toString();
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
Android Explicit Intent Program

 activity_main.xml

<?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="example.javatpoint.com.explicitintent.FirstActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="First Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="392dp"
android:onClick="callSecondActivity"
android:text="Call second activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
 ActivityOne class

package example.javatpoint.com.explicitintent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class FirstActivity extends AppCompatActivity


{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
public void callSecondActivity(View view)
{
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("Value1", "Android By Javatpoint");
i.putExtra("Value2", "Simple Tutorial");
startActivity(i);
}
}

 Activitytwo_main.xml

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


<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/andr
oid"
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="example.javatpoint.com.explicitintent.SecondActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Second Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="392dp"
android:onClick="callFirstActivity"
android:text="Call first activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

 ActivityTwo class

package example.javatpoint.com.explicitintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity


{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle extras = getIntent().getExtras();
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
Toast.makeText(getApplicationContext(),"Values are:\n First value: "+value1+
"\n Second Value: "+value2, Toast.LENGTH_LONG).show();
}
public void callFirstActivity(View view)
{
Intent i = new Intent(getApplicationContext(), FirstActivity.class);
startActivity(i);
}
}

Output:
Android ToggleButton Program
 activity_main.xml

<?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="example.javatpoint.com.togglebutton.MainActivity">

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="80dp"
android:text="ToggleButton"
android:textOff="Off"
android:textOn="On"
app:layout_constraintEnd_toStartOf="@+id/toggleButton2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ToggleButton
android:id="@+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:layout_marginTop="80dp"
android:text="ToggleButton"
android:textOff="Off"
android:textOn="On"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="144dp"
android:layout_marginLeft="148dp"
android:text="Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
 Activity class

package example.javatpoint.com.togglebutton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity


{
private ToggleButton toggleButton1, toggleButton2;
private Button buttonSubmit;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

addListenerOnButtonClick();
}

public void addListenerOnButtonClick()


{
//Getting the ToggleButton and Button instance from the layout xml file
toggleButton1=(ToggleButton)findViewById(R.id.toggleButton);
toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);
buttonSubmit=(Button)findViewById(R.id.button);
buttonSubmit.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View view) {
StringBuilder result = new StringBuilder();
result.append("ToggleButton1 : ").append(toggleButton1.getText());
result.append("\nToggleButton2 : ").append(toggleButton2.getText());
//Displaying the message in toast
Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show();
}
});
}
}
Output:
Android Spinner Program
 activity_main.xml

<?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="example.javatpoint.com.spinner.MainActivity">

<Spinner
android:id="@+id/spinner"
android:layout_width="149dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />

</android.support.constraint.ConstraintLayout>

 Activity class

package example.javatpoint.com.spinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements


AdapterView.OnItemSelectedListener {
String[] country = { "India", "USA", "China", "Japan", "Other"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
Toast.makeText(getApplicationContext(),country[position] , Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}

Output:
RadioButton Program
 main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />

</RadioGroup>

<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" />

</LinearLayout>

 MyAndroidAppActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity


{
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton()
{
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
int selectedId = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}

OUTPUT:
Android Checkbox Program

 strings.xml

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


<resources>
<string name="hello">Hello World, MyAndroidAppActivity!</string>
<string name="app_name">MyAndroidApp</string>
<string name="chk_ios">IPhone</string>
<string name="chk_android">Android</string>
<string name="chk_windows">Windows Mobile</string>
<string name="btn_display">Display</string>
</resources>

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

<CheckBox
android:id="@+id/chkIos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_ios" />

<CheckBox
android:id="@+id/chkAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_android"
android:checked="true" />
<CheckBox
android:id="@+id/chkWindows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_windows" />

<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" />

</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity


{
private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnChkIos();
addListenerOnButton();
}
public void addListenerOnChkIos()
{
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkIos.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (((CheckBox) v).isChecked())
{
Toast.makeText(MyAndroidAppActivity.this,
"Bro, try Android :)", Toast.LENGTH_LONG).show();
}
}
});
}

public void addListenerOnButton()


{
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
chkWindows = (CheckBox) findViewById(R.id.chkWindows);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
StringBuffer result = new StringBuffer();
result.append("IPhone check : ").append(chkIos.isChecked());
result.append("\nAndroid check : ").append(chkAndroid.isChecked());
result.append("\nWindows Mobile check :").append(chkWindows.isChecked());
Toast.makeText(MyAndroidAppActivity.this, result.toString(),
Toast.LENGTH_LONG).show();
}
});
}
}

OUTPUT:
Sign in Form Android

 MainActivity.java.

package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity


{
Button b1,b2;
EditText ed1,ed2;
TextView tx1;
int counter = 3;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
ed1 = (EditText)findViewById(R.id.editText);
ed2 = (EditText)findViewById(R.id.editText2);
b2 = (Button)findViewById(R.id.button2);
tx1 = (TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
if(ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("admin"))
{
Toast.makeText(getApplicationContext(),
"Redirecting...",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "WrongCredentials",Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
}
});
}
}

 activity_main.xml.

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


<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height = "match_parent" android:paddingLeft= "@dimen/activity_horizontal_margin"
android:paddingRight = "@dimen/activity_horizontal_margin"
android:paddingTop = "@dimen/activity_vertical_margin"
android:paddingBottom = "@dimen/activity_vertical_margin" tools:context = ".MainActivity">

<TextView android:text = "Login" android:layout_width="wrap_content"


android:layout_height = "wrap_content"
android:id = "@+id/textview"
android:textSize = "35dp"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true" />

<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Tutorials point"
android:id = "@+id/textView"
android:layout_below = "@+id/textview"
android:layout_centerHorizontal = "true"
android:textColor = "#ff7aff24"
android:textSize = "35dp" />

<EditText
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/editText"
android:hint = "Enter Name"
android:focusable = "true"
android:textColorHighlight = "#ff7eff15"
android:textColorHint = "#ffff25e6"
android:layout_marginTop = "46dp"
android:layout_below = "@+id/imageView"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_alignParentRight = "true"
android:layout_alignParentEnd = "true" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText"
android:textColorHint="#ffff299f"
android:hint="Password" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attempts Left:"
android:id="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView3"
android:layout_alignTop="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/textView2"
android:layout_toEndOf="@+id/textview"
android:textSize="25dp"
android:layout_toRightOf="@+id/textview" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/textview"
android:layout_toStartOf="@+id/textview" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/textview"
android:layout_toEndOf="@+id/textview" />

</RelativeLayout>

 string.xml.

<resources>
<string name="app_name">My Application</string>
</resources>

 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name=".MainActivity"
android:label="@string/app_name" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

</application>
</manifest>

Вам также может понравиться