Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
45 views6 pages

Create A New Project Android Studio

The document provides 9 steps to create an Android app that implements fragments. It involves creating a new project, defining the layout with two fragments, adding code to the main activity and fragment classes, and setting up styles. The left fragment contains buttons that interact with the right fragment by updating a text view or displaying a toast/dialog. The steps set up the necessary project structure and code to display two fragments within a single activity layout.

Uploaded by

T Dhee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views6 pages

Create A New Project Android Studio

The document provides 9 steps to create an Android app that implements fragments. It involves creating a new project, defining the layout with two fragments, adding code to the main activity and fragment classes, and setting up styles. The left fragment contains buttons that interact with the right fragment by updating a text view or displaying a toast/dialog. The steps set up the necessary project structure and code to display two fragments within a single activity layout.

Uploaded by

T Dhee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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