







































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
CS8662 Lab Manual for CSE students
Typology: Lecture notes
1 / 79
This page cannot be seen from the preview
Don't miss anything!








































































Ex. No: 01 Develop an application that uses GUI components, Font and Colors Date:
Aim: To develop a Simple Android Application that uses GUI components, Font and Colors. Procedure: Creating a New project: Open Android Studio and then click on File -> New -> New project.
Then type the Application name as “exno 1 ″ and click Next. Then select the Minimum SDK as shown below and click Next. Then select the Empty Activity and click Next. Finally click Finish. It will take some time to build and load the project. After completion it will look as given below.
Designing layout for the Android Application: Click on app -> res -> layout -> activity_main.xml. Now click on Text as shown below. Then delete the code which is there and type the code as given below. Code for Activity_main.xml:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView t= (TextView) findViewById(R.id.textView); Button b1= (Button) findViewById(R.id.button1); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { t.setTextSize(font); font = font + 5; if (font == 50) font = 30; } }); Button b2= (Button) findViewById(R.id.button2); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { switch (ch) { case 1: t.setTextColor(Color.RED); break; case 2: t.setTextColor(Color.GREEN); break; case 3: t.setTextColor(Color.BLUE); break; case 4: t.setTextColor(Color.CYAN); break; case 5: t.setTextColor(Color.YELLOW); break; case 6: t.setTextColor(Color.MAGENTA); break; } ch++; if (ch == 7)
ch = 1; } }); } }
So now the Coding part is also completed. Now run the application to see the output.
Output:
Result: Thus a Simple Android Application that uses GUI components, Font and Colors is developed and executed successfully.
Type the Activity Name as SecondActivity and click Finish button. Thus Second Activity For the application is created. Designing Layout for Main Activity: Click on app -> res -> layout -> activity_main.xml. Now click on Text as shown below. Then delete the code which is there and type the code as given below. Code for Activity_main.xml:
Designing Layout for Second Activity: Click on app -> res -> layout -> activity_second.xml. Now click on Text as shown below. Then delete the code which is there and type the code as given below. Code for Activity_second.xml:
Now click on Design and your activity will look as given below. So now the designing part of Second Activity is also completed. Java Coding for the Android Application: Java Coidng for Main Activity: Click on app -> java -> com.example.exno2 -> MainActivity. Then delete the code which is there and type the code as given below. Code for MainActivity.java: package com.example.exno2;
import android.content.Intent; //import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
//Defining the Views EditText e1,e2; Button bt; Spinner s;
//Data for populating in Spinner String [] dept_array={"CSE","ECE","IT","Mech","Civil"};
String name,reg,dept;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
//Referring the Views e1= (EditText) findViewById(R.id.editText);
Code for SecondActivity.java: package com.example.exno2;
import android.content.Intent; //import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity { TextView t1,t2,t3; String name,reg,dept; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); t1= (TextView) findViewById(R.id.textView1); t2= (TextView) findViewById(R.id.textView2); t3= (TextView) findViewById(R.id.textView3); //Getting the Intent Intent i = getIntent(); //Getting the Values from First Activity using the Intent received name=i.getStringExtra("name_key"); reg=i.getStringExtra("reg_key"); dept=i.getStringExtra("dept_key"); //Setting the Values to Intent t1.setText(name); t2.setText(reg); t3.setText(dept); } }
So now the Coding part of Second Activity is also completed. Now run the application to see the output.
Output:
Result: Thus a Simple Android Application that uses Layout Managers and Event Listeners is developed and executed successfully.
Code for MainActivity.java: package com.example.exno3;
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); } } So now the Coding part is also completed. Now run the application to see the output.
Output:
Result: Thus a Simple Android Application that draws basic Graphical Primitives on the screen is developed and executed successfully.
<EditText android:id="@+id/Marks" android:layout_width="150dp" android:layout_height="wrap_content"
android:layout_x="175dp" android:layout_y="200dp" android:inputType="number" android:textSize="20sp" />
<Button android:id="@+id/View" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_x="200dp" android:layout_y="400dp" android:text="View"