Assignment 2
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
<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"};
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Handle nothing selected if needed
}
});
}
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();
}
}
Fragment_Technology.xml
<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;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_technology, container, false);
return view;
}
}
<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;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_science, container, false);
return view;
}
}
Activity Detailed xml
<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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed);
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.";
}
}
}
<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;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
return view;