0% found this document useful (0 votes)
110 views15 pages

Android Lab Programs

The document describes the steps to develop a simple Android application that uses layout managers and event listeners. It involves creating a new project, adding two activities with their layouts defined in XML, and Java code to populate the layouts and pass data between activities when a button is clicked. Layout managers like linear layout, grid layout, and relative layout are used to position views. Event listeners like button click listeners are added to trigger intent-based navigation between activities.

Uploaded by

SURESH
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
110 views15 pages

Android Lab Programs

The document describes the steps to develop a simple Android application that uses layout managers and event listeners. It involves creating a new project, adding two activities with their layouts defined in XML, and Java code to populate the layouts and pass data between activities when a button is clicked. Layout managers like linear layout, grid layout, and relative layout are used to position views. Event listeners like button click listeners are added to trigger intent-based navigation between activities.

Uploaded by

SURESH
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 15

3. Develop an android application for Layout Managers and Event Listeners.

Aim:
        To develop a Simple Android Application that uses Layout Managers and Event Listeners.

Procedure:
Creating a New project:
 Open Android Stdio and then click on File -> New -> New project.
 Then type the Application name as “ex.no.2″ 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.
Creating Second Activity for the Android Application:
 Click on File -> New -> Activity -> Empty Activity.
 Type the Activity Name as SecondActivity and click Finish button.
 Thus Second Activity For the application is created.
Designing layout for the Android Application:
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:

<?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">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="30dp"
            android:text="Details Form"
            android:textSize="25sp"
            android:gravity="center"/>
    </LinearLayout>
 
    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="200dp"
        android:columnCount="2"
        android:rowCount="3">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_row="0"
            android:layout_column="0"
            android:text="Name"
            android:textSize="20sp"
            android:gravity="center"/>
 
        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_row="0"
            android:layout_column="1"
            android:ems="10"/>
 
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_row="1"
            android:layout_column="0"
            android:text="Reg.No"
            android:textSize="20sp"
            android:gravity="center"/>
 
        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_row="1"
            android:layout_column="1"
            android:inputType="number"
            android:ems="10"/>
 
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_row="2"
            android:layout_column="0"
            android:text="Dept"
            android:textSize="20sp"
            android:gravity="center"/>
 
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_row="2"
            android:layout_column="1"
            android:spinnerMode="dropdown"/>
 
    </GridLayout>
 
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_marginBottom="150dp"
        android:text="Submit"/>
 
</RelativeLayout>
 Now click on Design and your activity will look as given below.
 So now the designing part of Main Activity is completed.
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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.devang.exno2.SecondActivity"
    android:orientation="vertical"
    android:gravity="center">
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="New Text"
        android:textSize="30sp"/>
 
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="New Text"
        android:textSize="30sp"/>
 
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="New Text"
        android:textSize="30sp"/>
 
</LinearLayout>
 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;
 
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);
        e2= (EditText) findViewById(R.id.editText2);
 
        bt= (Button) findViewById(R.id.button);
 
        s= (Spinner) findViewById(R.id.spinner);
 
        //Creating Adapter for Spinner for adapting the data from array to Spinner
        ArrayAdapter adapter= new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,dept_array);
        s.setAdapter(adapter);
 
        //Creating Listener for Button
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 
                //Getting the Values from Views(Edittext & Spinner)
                name=e1.getText().toString();
                reg=e2.getText().toString();
                dept=s.getSelectedItem().toString();
 
                //Intent For Navigating to Second Activity
                Intent i = new Intent(MainActivity.this,SecondActivity.class);
 
                //For Passing the Values to Second Activity
                i.putExtra("name_key", name);
                i.putExtra("reg_key",reg);
                i.putExtra("dept_key", dept);
 
                startActivity(i);
 
            }
        });
    }
}
 So now the Coding part of Main Activity is completed.
Java Coding for Second Activity:
 Click on app -> java -> com.example.exno2 -> SecondActivity.
 Then delete the code which is there and type the code as given below.
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;
 
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);
 
    }
}

Output:
4. Develop an Android Application using Activity and Intents.
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>  

MainActivity.java
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);  
            }  
        });  
    }  
}  

OUTPUT:

 
5. Develop an android application using menus.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>  
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.a
ndroid.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.optionmenu.MainActivity">  
  
    <android.support.design.widget.AppBarLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:theme="@style/AppTheme.AppBarOverlay">  
  
        <android.support.v7.widget.Toolbar  
            android:id="@+id/toolbar"  
            android:layout_width="match_parent"  
            android:layout_height="?attr/actionBarSize"  
            android:background="?attr/colorPrimary"  
            app:popupTheme="@style/AppTheme.PopupOverlay" />  
  
    </android.support.design.widget.AppBarLayout>  
  
    <include layout="@layout/content_main" />  
  
</android.support.design.widget.CoordinatorLayout>  

context_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"  
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  
    tools:context="example.javatpoint.com.optionmenu.MainActivity"  
    tools:showIn="@layout/activity_main">  
  
   
 <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>  

menu_main.xml
<menu 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"  
    tools:context="example.javatpoint.com.optionmenu.MainActivity">  
  
    <item  android:id="@+id/item1"  
        android:title="Item 1"/>  
    <item  android:id="@+id/item2"  
        android:title="Item 2"/>  
    <item  android:id="@+id/item3"  
        android:title="Item 3"  
        app:showAsAction="withText"/>  
</menu>  

MainActivity.java
package example.javatpoint.com.optionmenu;  
  
import android.os.Bundle;  
import android.support.v7.app.AppCompatActivity;  
import android.support.v7.widget.Toolbar;  
import android.view.Menu;  
import android.view.MenuItem;  
import android.widget.Toast;  
  
public class MainActivity extends AppCompatActivity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
        setSupportActionBar(toolbar);  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.menu_main, menu);  
        return true;  
    }  
  
    @Override  
    public boolean onOptionsItemSelected(MenuItem item) {  
       int id = item.getItemId();  
        switch (id){  
            case R.id.item1:  
   Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show(); 
  return true;  
            case R.id.item2:  
   Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show(); 
                 return true;  
            case R.id.item3:  
   Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show(); 
                 return true;  
            default:  
                return super.onOptionsItemSelected(item);  
        }  
    }  
}  

OUTPUT
Output without clicking on the menu button
Output after clicking on the menu button.

You might also like