Mohammadshafe Pahochiya 18BECE30537
Aim : Implement the concept of fragment for calculator application.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment"
android:name="[Link]"
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="[Link].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" />
</[Link]>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText editText1,editText2;
Button add,sub,div,mul,per;
@Override
protected void onCreate(Bundle savedInstanceState) {
Mohammadshafe Pahochiya 18BECE30537
[Link](savedInstanceState);
setContentView([Link].activity_main);
editText1=findViewById([Link]);
editText2=findViewById([Link].editTextTextPersonName2);
add=findViewById([Link].button2);
sub=findViewById([Link].button3);
div=findViewById([Link].button4);
mul=findViewById([Link].button5);
per=findViewById([Link].button6);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String et1=[Link]().toString();
String et2=[Link]().toString();
int a=[Link](et1);
int b=[Link](et2);
int sum=a+b;
[Link](getApplicationContext(),[Link](sum),Toast.LENGTH_SHORT).sho
w();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
String et1=[Link]().toString();
String et2=[Link]().toString();
int a=[Link](et1);
int b=[Link](et2);
int sub=a-b;
[Link](getApplicationContext(),[Link](sub),Toast.LENGTH_SHORT).sho
w();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
String et1=[Link]().toString();
String et2=[Link]().toString();
int a=[Link](et1);
int b=[Link](et2);
int mul=a*b;
[Link](getApplicationContext(),[Link](mul),Toast.LENGTH_SHORT).sho
w();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
String et1=[Link]().toString();
String et2=[Link]().toString();
int a=[Link](et1);
Mohammadshafe Pahochiya 18BECE30537
int b=[Link](et2);
int div=a/b;
[Link](getApplicationContext(),[Link](div),Toast.LENGTH_SHORT).sho
w();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
String et1=[Link]().toString();
String et2=[Link]().toString();
int a=[Link](et1);
int b=[Link](et2);
int per=a%b;
[Link](getApplicationContext(),[Link](per),Toast.LENGTH_SHORT).sho
w();
}
});
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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();
[Link](ARG_PARAM1, param1);
[Link](ARG_PARAM2, param2);
[Link](args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](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 [Link]([Link].fragment_blank, container, false);
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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();
[Link](ARG_PARAM1, param1);
[Link](ARG_PARAM2, param2);
[Link](args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](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 [Link]([Link].fragment_blank2, container, false);
}
}
fragment_blank.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
fragment_blank2.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
Output :
Mohammadshafe Pahochiya 18BECE30537