You are on page 1of 8

Mohammadshafe Pahochiya 18BECE30537

Aim : Implement the concept of fragment for calculator application.

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="wrap_content"
tools:context=".MainActivity">

<fragment
android:id="@+id/fragment"
android:name="com.example.calcfrag.BlankFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<fragment
android:id="@+id/fragment2"
android:name="com.example.calcfrag.BlankFragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fragment" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.calcfrag;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText editText1,editText2;
Button add,sub,div,mul,per;
@Override
protected void onCreate(Bundle savedInstanceState) {
Mohammadshafe Pahochiya 18BECE30537

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editText1=findViewById(R.id.editTextTextPersonName);
editText2=findViewById(R.id.editTextTextPersonName2);
add=findViewById(R.id.button2);
sub=findViewById(R.id.button3);
div=findViewById(R.id.button4);
mul=findViewById(R.id.button5);
per=findViewById(R.id.button6);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String et1=editText1.getText().toString();
String et2=editText2.getText().toString();
int a=Integer.parseInt(et1);
int b=Integer.parseInt(et2);
int sum=a+b;

Toast.makeText(getApplicationContext(),String.valueOf(sum),Toast.LENGTH_SHORT).sho
w();
}
});

sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String et1=editText1.getText().toString();
String et2=editText2.getText().toString();
int a=Integer.parseInt(et1);
int b=Integer.parseInt(et2);
int sub=a-b;

Toast.makeText(getApplicationContext(),String.valueOf(sub),Toast.LENGTH_SHORT).sho
w();
}
});

mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String et1=editText1.getText().toString();
String et2=editText2.getText().toString();
int a=Integer.parseInt(et1);
int b=Integer.parseInt(et2);
int mul=a*b;

Toast.makeText(getApplicationContext(),String.valueOf(mul),Toast.LENGTH_SHORT).sho
w();
}
});

div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String et1=editText1.getText().toString();
String et2=editText2.getText().toString();
int a=Integer.parseInt(et1);
Mohammadshafe Pahochiya 18BECE30537

int b=Integer.parseInt(et2);
int div=a/b;

Toast.makeText(getApplicationContext(),String.valueOf(div),Toast.LENGTH_SHORT).sho
w();
}
});

per.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String et1=editText1.getText().toString();
String et2=editText2.getText().toString();
int a=Integer.parseInt(et1);
int b=Integer.parseInt(et2);
int per=a%b;

Toast.makeText(getApplicationContext(),String.valueOf(per),Toast.LENGTH_SHORT).sho
w();
}
});
}
}

BlankFragment.java
package com.example.calcfrag;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class BlankFragment extends Fragment {

// TODO: Rename parameter arguments, choose names that match


// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters


private String mParam1;
private String mParam2;

public BlankFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BlankFragment.
*/
Mohammadshafe Pahochiya 18BECE30537

// TODO: Rename and change types and number of parameters


public static BlankFragment newInstance(String param1, String param2) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}

BlankFragment2.java
package com.example.calcfrag;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class BlankFragment2 extends Fragment {

// TODO: Rename parameter arguments, choose names that match


// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters


private String mParam1;
private String mParam2;

public BlankFragment2() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
Mohammadshafe Pahochiya 18BECE30537

* @param param2 Parameter 2.


* @return A new instance of fragment BlankFragment2.
*/
// TODO: Rename and change types and number of parameters
public static BlankFragment2 newInstance(String param1, String param2) {
BlankFragment2 fragment = new BlankFragment2();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank2, container, false);
}
}

fragment_blank.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:id="@+id/Constraint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".BlankFragment">

<!-- TODO: Update blank fragment layout -->

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:ems="10"
android:hint="Enter Number 1"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/editTextTextPersonName2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
Mohammadshafe Pahochiya 18BECE30537

app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:ems="10"
android:hint="Enter Number 2"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName"
app:layout_constraintVertical_bias="0.044" />

</androidx.constraintlayout.widget.ConstraintLayout>

fragment_blank2.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="wrap_content"
tools:context=".BlankFragment2">

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="41dp"
android:text="+"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="72dp"
android:text="-"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
Mohammadshafe Pahochiya 18BECE30537

android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="41dp"
android:text="/"
app:layout_constraintEnd_toStartOf="@+id/button5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="72dp"
android:text="*"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button4"
app:layout_constraintTop_toBottomOf="@+id/button3" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="84dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="234dp"
android:layout_marginBottom="20dp"
android:text="%"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />
</androidx.constraintlayout.widget.ConstraintLayout>

Output :
Mohammadshafe Pahochiya 18BECE30537

You might also like