Mobile Application Development Practical 18

Question 1
Write a program to create a text field and a button “Navigate”. When you enter “www.google.com” and press navigate button it should open google page.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/editText"
            android:layout_centerHorizontal="true"
            android:text="Nevigate" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:ems="10"
            android:hint="Enter url" />
    </RelativeLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical18;

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

public class MainActivity extends AppCompatActivity {
    EditText ed;
    Button b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed=findViewById(R.id.editText);
        b=findViewById(R.id.button);

        View.OnClickListener cl=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url=ed.getText().toString();
                Intent i=new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }
        };
        b.setOnClickListener(cl);
    }
}
Output
Screenshot_20230319_194932
Screenshot_20230319_194947
Question 2
Write a program to create button “Start Dialer”. When u click on this button it should open the phone dialer.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/editText"
            android:layout_centerHorizontal="true"
            android:text="Start Dialer" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:ems="10"
            android:hint="Enter number" />
    </RelativeLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical18_2;

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

public class MainActivity extends AppCompatActivity {
    EditText ed;
    Button b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed=findViewById(R.id.editText);
        b=findViewById(R.id.button);

        View.OnClickListener cl=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url=ed.getText().toString();
                Intent i=new Intent(Intent.ACTION_DIAL);
                i.setData(Uri.parse("tel:"+url));
                startActivity(i);
            }
        };
        b.setOnClickListener(cl);
    }
}
Output
Screenshot_20230319_194421
Screenshot_20230319_194437
Question 3
Write a program to create two screens. First screen will take one number input from user. After click on Factorial button, second screen will open and it should display factorial of the same number. Also specify which type of intent you will use in this case.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/editText"
            android:layout_centerHorizontal="true"
            android:text="Factorial" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:inputType="number"
            android:ems="10"
            android:hint="Enter number" />
    </RelativeLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
activity_factorial.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Factorial">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/factorial"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp"/>

    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical18_3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    EditText ed;
    Button b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed=findViewById(R.id.editText);
        b=findViewById(R.id.button);

        View.OnClickListener cl=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=ed.getText().toString();
                Intent i=new Intent(getApplicationContext(),Factorial.class);
                i.putExtra("num",str);
                startActivity(i);
            }
        };
        b.setOnClickListener(cl);
    }
}
Factorial.java
package com.example.practical18_3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Factorial extends AppCompatActivity {
    TextView tv;
    int fact=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_factorial);
        tv=findViewById(R.id.factorial);
        Intent i1=getIntent();
        String str=i1.getStringExtra("num");
        int n=Integer.parseInt(str);
        while(n>=1)
        {
            fact=fact*n;
            n--;
        }
        tv.setText("Factorial is: "+fact);
    }
}
Output
Screenshot_20230319_211553
Screenshot_20230319_211606

Leave a Comment

Your email address will not be published. Required fields are marked *