You are on page 1of 6

Assignment-5

1. Create an Android app to implement fragments.

Step 1: Create a new project and name it Fragments.

In this step we create a new project in android studio by filling all the necessary details of the app
like app name, package name, api versions etc.
Select File -> New -> New Project and Fill the forms and click "Finish" button.

Step 2: Now open res -> layout ->activity_main.xml and write following code:

activity_main.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=".MainActivity"
android:orientation="horizontal">
<fragment
android:id="@+id/fragmentLeft"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.example.fragment.FragmentLeft"/>
<fragment
android:id="@+id/fragmentRight"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:name="com.example.fragment.FragmentRight"/>
</LinearLayout>

Step 3: Now open app -> java -> first fold par click-> open MainActivity.java and write the
below code.

package com.example.fragment;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("dev2qa.com - Multiple Fragments In One Activity Example");
}
}

Step 4: Open res -> layout ->right click-> New->Layout Resouces File-> Name of file is
fragment_left->ok and write following code:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/fragmentLeftButtonAndroid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Android"
android:textSize="20dp"
android:textAllCaps="false"/>
<Button
android:id="@+id/fragmentLeftButtonIos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="IOS"
android:textSize="20dp"
android:textAllCaps="false"/>
<Button
android:id="@+id/fragmentLeftButtonWindows"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Windows"
android:textSize="20dp"
android:textAllCaps="false"/>
</LinearLayout>

Step 5: Open res -> layout ->right click-> New->Layout Resouces File-> Name of file is
fragment_right->ok and write following code:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/fragmentRightTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="60dp"
android:textColor="@android:color/holo_red_light"
android:gravity="center"/>
</LinearLayout>

Step 6: Now open app -> java -> first fold par right click-> New Class Name is FragmentLeft
and write the below code.

package com.example.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import org.jetbrains.annotations.Nullable;
public class FragmentLeft extends Fragment {
// This method will be invoked when the Fragment view object is created.
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View retView = inflater.inflate(R.layout.fragment_left, container);
// Get Fragment belonged Activity
final FragmentActivity fragmentBelongActivity = getActivity();
if(retView!=null)
{
// Click this button will show the text in right fragment.
Button androidButton =
(Button)retView.findViewById(R.id.fragmentLeftButtonAndroid);
androidButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Do not use fragmentBelongActivity.getFragmentManager() method
which is not compatible with older android os version. .
FragmentManager fragmentManager = ((FragmentActivity)
fragmentBelongActivity).getSupportFragmentManager();
// Get right Fragment object.
Fragment rightFragment =
fragmentManager.findFragmentById(R.id.fragmentRight);
// Get the TextView object in right Fragment.
final TextView rightFragmentTextView =
(TextView)rightFragment.getView().findViewById(R.id.fragmentRightTextView);
// Set text in right Fragment TextView.
rightFragmentTextView.setText("You click Android button.");
}
});
// Click this button will show a Toast popup.
Button iosButton =
(Button)retView.findViewById(R.id.fragmentLeftButtonIos);
iosButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(fragmentBelongActivity, "You click IOS button.",
Toast.LENGTH_SHORT).show();
}
});
// Click this button will show an alert dialog.
Button windowsButton =
(Button)retView.findViewById(R.id.fragmentLeftButtonWindows);
windowsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog alertDialog = new
AlertDialog.Builder(fragmentBelongActivity).create();
alertDialog.setMessage("You click Windows button.");
alertDialog.show();
}
});
}
return retView;
}
}

Step 7: Now open app -> java -> first fold par right click-> New Class Name is
FragmenRight and write the below code.

package com.example.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import org.jetbrains.annotations.Nullable;
/**
* Created by Jerry on 12/23/2017.
*/
public class FragmentRight extends Fragment {
// This method will be invoked when the Fragment view object is created.
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View retView = inflater.inflate(R.layout.fragment_right, container);
if(retView!=null) {
TextView fragmentRightTextView =
(TextView)retView.findViewById(R.id.fragmentRightTextView);
fragmentRightTextView.setText("This is the default right fragment.");
}
return retView;
}
}

Step 8: Open res -> Open values ->dobule click on styles.xml and write following code:

<resources>

<!-- Base application theme. -->


<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

Step 9: app->manifest->Open AndroidManifest.xml file and make your splashactivity.java


class as Launcher activity and mention the Main Activity as another activity.

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fragment">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output
Mrs. Hritika Vaishnav
(Assistant Professor, Dept. Of CS)

You might also like