Wednesday, December 11, 2024
android lab programs
1. Radio Button
Aim: Write a Program to select gender using radio button
activity_main.xml
MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Button but;
RadioGroup rgroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but=(Button) findViewById(R.id.button);
rgroup=(RadioGroup) findViewById(R.id.radiogrp);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selected=rgroup.getCheckedRadioButtonId();
RadioButton radio=(RadioButton) findViewById(selected);
Toast.makeText(MainActivity.this,"You selected : "+radio.getText(),Toast.LENGTH_LONG).show();
}
});
}
}
Output
2. Addition of two Numbers
Aim: Write a program to add two numbers
activity_main.xml
MainActivity.java
package com.example.myapplication;
import android.widget.EditText;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
EditText firstnum,secondnum;
TextView r;
Button bt;
double a,b,c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstnum=(EditText) findViewById(R.id.first);
secondnum=(EditText) findViewById(R.id.second);
bt=(Button) findViewById(R.id.buttonadd);
r=(TextView) findViewById(R.id.result);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a=Double.parseDouble(firstnum.getText().toString());
b=Double.parseDouble(secondnum.getText().toString());
c=a+b;
r.setText("Sum="+c);
}
});
}
Output
3. Alert Box
Aim: Write a program to Display an alert box with OK and Cancel
activity_main.xml
MainActivity.java
package com.example.myapplication;
import android.content.DialogInterface;
import android.widget.EditText;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button but;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but=(Button) findViewById(R.id.button);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder b= new AlertDialog.Builder(MainActivity.this);
b.setTitle("Alert");
b.setMessage("Are you sure ?");
b.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "You Clicked OK",
Toast.LENGTH_SHORT).show();
}
});
b.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "You Clicked Cancel",
Toast.LENGTH_SHORT).show();
}
});
AlertDialog d = b.create();
d.show();
}
});
}
}
4. Multiplication table
Aim: Write a program to display multiplication table of a given number
activity_main.xml
MainActivity.java
package com.example.myapplication43;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
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 {
TextView txtres ;
Button but ;
EditText edtnum ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . activity_main ) ;
txtres = findViewById ( R . id . textViewResult ) ;
edtnum = findViewById ( R . id . editTextNumber ) ;
but = findViewById ( R . id . button ) ;
but . setOnClickListener ( new View . OnClickListener () {
@Override
public void onClick ( View v ) {
int num = Integer . parseInt ( edtnum . getText () . toString () ) ;
txtres . setText ( " " ) ;
for ( int i =1; i <11; i ++) {
txtres . append ( i + " * " + num + " = " + i* num + " \n" ) ;
}
}
}) ;
}
}
Output
5. Change the Background Color
Aim: Write a program to Change the Background colour of the Activity
activity_main.xml
MainActivity.java
package com.example.myapplicationbgcolor;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Button but , butc;
RelativeLayout rel;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
setContentView (R.layout.activity_main );
but = findViewById (R.id.button);
butc = findViewById (R.id.butclear);
rel = findViewById (R.id.rel);
but.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick ( View v ) {
rel.setBackgroundColor (Color.MAGENTA);
}
}) ;
butc.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick ( View v ) {
rel.setBackgroundColor(Color.WHITE);
}
});
}
Output
6. Spinner
Aim: Write a Program to spin the six items
activity_main.xml
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Spinner spin ;
String[] items = {"asp.net ","java" ,"rdbms","android","android lab "};
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate (savedInstanceState) ;
setContentView (R.layout.activity_main) ;
spin=findViewById(R.id.spinner) ;
spin.setOnItemSelectedListener (new AdapterView. OnItemSelectedListener()
{
@Override
public void onItemSelected ( AdapterView parent , View view , int
position , long id ) {
Toast . makeText ( MainActivity . this , " Selected Item "+ items [
position ] , Toast . LENGTH_SHORT ) . show () ;
}
@Override
public void onNothingSelected ( AdapterView parent ) {
Toast . makeText ( MainActivity . this , " No item selected " , Toast .
LENGTH_SHORT ) . show () ;
}
}) ;
ArrayAdapter < String > adt = new ArrayAdapter < String >( this ,
android . R . layout . simple_list_item_1 , items ) ;
spin . setAdapter ( adt ) ;
}
}
Output
7. Fetch data
Aim: Fetch data from an Edit Text and display it in a Text View
activity_main.xml
MainActivity.java
package com.example.myapplicationfetchdata;
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 {
EditText edt;
Button but;
TextView txt;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate (savedInstanceState ) ;
setContentView (R.layout.activity_main ) ;
edt = findViewById ( R.id.editText ) ;
but = findViewById ( R.id.button );
txt = findViewById ( R.id.text_display ) ;
but.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick ( View v ) {
txt.setText ( edt.getText().toString());
}
}) ;
}
}
Output
8. Change the background Image
Aim: Write a program to Change the Background image
activity_main.xml
MainActivity.java
com.example.myapplicationchangebgimage;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android . graphics . Color ;
import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;
import android . widget . RelativeLayout ;
public class MainActivity extends AppCompatActivity {
Button but;
RelativeLayout rel;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState ) ;
setContentView ( R.layout.activity_main);
but = findViewById (R.id.button);
rel = findViewById (R.id.rel) ;
but.setOnClickListener ( new View.OnClickListener() {
@Override
public void onClick ( View v ) {
rel.setBackgroundResource (R.drawable.sujeercolor ) ;
}
}) ;
}
}
Output
9. Get current time and date
Aim: Write a program to get current time and date
activity_main.xml
MainActivity.java
package com.example.myapplicationdateandtime;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
@TargetApi(Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView=findViewById(R.id.date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
String currentDateandTime = sdf.format(new Date());
textView.setText(currentDateandTime);
}
}
Output
10. Marquee Text
Aim: Write a program to make a marquee text
activity_main.xml
MainActivity.java
package com.example.myapplicationmarquee;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt = findViewById(R.id.text);
txt.setSelected(true);
}
}
Output
Subscribe to:
Posts (Atom)