Create an Android Application that draw basic Graphic primitives on the screen
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />
</RelativeLayout>
MainActivity.java
package com.example.graphicprimitives;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating a Bitmap
Bitmap bg = Bitmap.createBitmap(720, 1280, Bitmap.Config.ARGB_8888);
//Setting the Bitmap as background for the ImageView
ImageView i = (ImageView) findViewById(R.id.imageView);
i.setBackgroundDrawable(new BitmapDrawable(bg));
//Creating the Canvas Object
Canvas canvas = new Canvas(bg);
//Creating the Paint Object and set its color & TextSize
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(50);
//To draw a Rectangle
canvas.drawText("Rectangle", 420, 150, paint);
canvas.drawRect(400, 200, 650, 700, paint);
//To draw a Circle
canvas.drawText("Circle", 120, 150, paint);
canvas.drawCircle(200, 350, 150, paint);
//To draw a Square
canvas.drawText("Square", 120, 800, paint);
canvas.drawRect(50, 850, 350, 1150, paint);
//To draw a Line
canvas.drawText("Line", 480, 800, paint);
canvas.drawLine(520, 850, 520, 1150, paint);
}
}
Create an Android Application to display the time. (Use Timepicker with Spinner).
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"/>
<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" />
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);
}
}
Create an Android Application that implements Multithreading
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/info" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="fetchData"
android:text="Start MULTITHREAD" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main thread" />
</LinearLayout>
MainActivity.java
package com.example.multithreading;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
public class multithreading extends Activity
{
private TextView tvOutput;
private static final int t1 = 1;
private static final int t2 = 2;
private static final int t3 = 3;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvOutput = (TextView) findViewById(R.id.textView1);
}
public void fetchData(View v)
{
tvOutput.setText("Main thread");
thread1.start();
thread2.start();
thread3.start();
}
Thread thread1 = new Thread(new Runnable()
{
@Override
public void run()
{
for (int i = 0; i < 5; i++)
{
try { Thread.sleep(1000); }
catch (InterruptedException e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(t1);
}
}
});
Thread thread2 = new Thread(new Runnable()
{
@Override
public void run()
{
for (int i = 0; i < 5; i++)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(t2);
}
}
});
Thread thread3 = new Thread(new Runnable()
{
@Override
public void run()
{
for (int i = 0; i < 5; i++)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(t3);
}
}
});
Handler handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
if(msg.what == t1)
{
tvOutput.append("\nIn thread 1");
}
if(msg.what == t2)
{
tvOutput.append("\nIn thread 2");
}
if(msg.what == t3)
{
tvOutput.append("\nIn thread 3");
}
}};
}
Create an Android Application to display a dialog objects
activity_main.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"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close app" />
</LinearLayout>
MainActivity.java
package com.example.dialogboxdemoapp;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
Button closeButton;
AlertDialog.Builder builder;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
closeButton = (Button) findViewById(R.id.button);
builder = new AlertDialog.Builder(this);
closeButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
builder.setMessage("Do you want to close the application");
builder.setCancelable(false);
builder.setPositiveButton("yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
finish();
Toast.makeText(getApplicationContext(), "",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
Toast.makeText(getApplicationContext(),"you choose the no action in the alert
box",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert =builder.create();
alert.setTitle("AlertDialogExample");
alert.show();
}
});
}
}