0% found this document useful (0 votes)
2 views

Assignment 2

The document outlines an Android application that allows users to select a topic from a Spinner, which dynamically loads a corresponding Fragment displaying a brief description. Each Fragment includes a 'More Info' button that launches a DetailedActivity, providing in-depth information about the selected topic. The application includes XML layouts and Java code for the main activity, fragments, and detailed activity, covering topics such as History, Science, and Technology.

Uploaded by

Alok Ahirwar
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)
2 views

Assignment 2

The document outlines an Android application that allows users to select a topic from a Spinner, which dynamically loads a corresponding Fragment displaying a brief description. Each Fragment includes a 'More Info' button that launches a DetailedActivity, providing in-depth information about the selected topic. The application includes XML layouts and Java code for the main activity, fragments, and detailed activity, covering topics such as History, Science, and Technology.

Uploaded by

Alok Ahirwar
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/ 10

Assignment 2

Problem:The app allows the user to select a topic (e.g., History, Science, or
Technology) from a Spinner. Based on the selected topic, a Fragment is dynamically
loaded to show a brief description. A "More Info" button in the fragment launches a
new Activity (DetailedActivity) to provide detailed information about the selected topic

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"
android:orientation="vertical"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Spinner
android:id="@+id/topicSpinner"
android:layout_width="match_parent"
android:layout_height="242dp"
android:layout_margin="16dp" />
</FrameLayout>

</LinearLayout>

Main Activity

package com.example.ass;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity {

Spinner topicSpinner;
FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

topicSpinner = findViewById(R.id.topicSpinner);
fragmentManager = getSupportFragmentManager();

// Array of topics
String[] topics = {"History", "Science", "Technology"};

// Set up the spinner adapter


ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, topics);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
topicSpinner.setAdapter(adapter);

// Handle spinner item selection


topicSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedTopic = topics[position];
loadFragment(selectedTopic);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// Handle nothing selected if needed
}
});
}

private void loadFragment(String topic) {


Fragment fragment = null;

switch (topic) {
case "History":
fragment = new HistoryFragment();
break;
case "Science":
fragment = new ScienceFragment();
break;
case "Technology":
fragment = new TechnologyFragment();
break;
}

if (fragment != null) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragmentContainer, fragment); // Replace the fragment
container
transaction.commit();
}
}

public void launchDetailedActivity(String topic) {


Intent intent = new Intent(this, DetailedActivity.class);
intent.putExtra("topic", topic); // Pass the topic to the detailed activity
startActivity(intent);
}
}

Fragment_Technology.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/technologyDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Technology Description"
android:textSize="16sp" />

<Button
android:id="@+id/technologyMoreInfoButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="More Info" />

</LinearLayout>
Technology Fragment

package com.example.ass;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class TechnologyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_technology, container, false);

TextView descriptionTextView = view.findViewById(R.id.technologyDescription);


descriptionTextView.setText("Brief Technology description here."); // Set your
description

Button moreInfoButton = view.findViewById(R.id.technologyMoreInfoButton);


moreInfoButton.setOnClickListener(v -> {
MainActivity activity = (MainActivity) getActivity();
if (activity != null) {
activity.launchDetailedActivity("Technology");
}
});

return view;
}
}

Fragment Science xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/scienceDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Science Description"
android:textSize="16sp" />

<Button
android:id="@+id/scienceMoreInfoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="More Info" />

</LinearLayout>

Fragment Science

package com.example.ass;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class ScienceFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_science, container, false);

TextView descriptionTextView = view.findViewById(R.id.scienceDescription);


descriptionTextView.setText("Brief Science description here."); // Set your description

Button moreInfoButton = view.findViewById(R.id.scienceMoreInfoButton);


moreInfoButton.setOnClickListener(v -> {
MainActivity activity = (MainActivity) getActivity();
if (activity != null) {
activity.launchDetailedActivity("Science");
}
});

return view;
}
}
Activity Detailed xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/detailedInfoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rotationX="0"
android:textSize="16sp" />

</LinearLayout>

Detailed Activity

package com.example.ass;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class DetailedActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed);

TextView detailedInfoTextView = findViewById(R.id.detailedInfoTextView);

Intent intent = getIntent();


String topic = intent.getStringExtra("topic");

if (topic != null) {
// Load detailed information based on the topic
String detailedInformation = getDetailedInformation(topic); // Implement this method
detailedInfoTextView.setText(detailedInformation);
}
}
private String getDetailedInformation(String topic) {
// Replace with your actual data retrieval logic (e.g., from a database, API, or resources)
switch (topic) {
case "History":
return
"History is the study of past events, particularly those related to human
societies, civilizations, and cultures. It involves analyzing records, documents, artifacts, and
other sources to understand how people, ideas, and events have shaped the world over
time. History helps in learning from past successes and mistakes, providing insight into
present and future developments..";
case "Science":
return
"Science is the systematic study of the natural world through observation,
experimentation, and analysis. It seeks to understand the principles and laws governing the
universe, using evidence-based methods to explain phenomena and make predictions.
Science is divided into various disciplines, such as physics, chemistry, biology, and earth
sciences, each focusing on specific aspects of nature..";
case "Technology":
return
"Technology is the application of scientific knowledge to create tools,
systems, and processes that solve problems and improve human life. It encompasses a
wide range of fields, including information technology, engineering, robotics, and
communication. Technology evolves continuously, shaping industries, societies, and
everyday activities by enhancing efficiency, convenience, and innovation..";
default:
return "No information available.";
}
}
}

Fragment history xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/historyDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="History Description"
android:textSize="16sp" />

<Button
android:id="@+id/historyMoreInfoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="More Info" />

</LinearLayout>
History Fragment

package com.example.ass;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class HistoryFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);

TextView descriptionTextView = view.findViewById(R.id.historyDescription);


descriptionTextView.setText("Brief history description here."); // Set your description

Button moreInfoButton = view.findViewById(R.id.historyMoreInfoButton);


moreInfoButton.setOnClickListener(v -> {
MainActivity activity = (MainActivity) getActivity();
if (activity != null) {
activity.launchDetailedActivity("History");
}
});

return view;

You might also like