Note - Double Click to Copy Code Contact Us!

Android App for Tourist Guide

Tech Doubility
Android App for Tourist Guide



First, let's create the XML layout file activity_tourist_guide.xml for the activity:

XML Code
  1. <!-- activity_tourist_guide.xml -->
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. android:padding="16dp"
  8. tools:context=".TouristGuideActivity">
  9.  
  10. <ListView
  11. android:id="@+id/attractions_list_view"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent" />
  14.  
  15. </LinearLayout>
  16.  
Next, let's create the Java code for the activity:

Java Code
  1. import androidx.appcompat.app.AppCompatActivity;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.AdapterView;
  5. import android.widget.ArrayAdapter;
  6. import android.widget.ListView;
  7. import android.widget.Toast;
  8.  
  9. public class TouristGuideActivity extends AppCompatActivity {
  10.  
  11. private ListView attractionsListView;
  12. private String[] attractions;
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_tourist_guide);
  18.  
  19. // Initialize views
  20. attractionsListView = findViewById(R.id.attractions_list_view);
  21.  
  22. // Get attractions from resources
  23. attractions = getResources().getStringArray(R.array.attractions);
  24.  
  25. // Set up the list view
  26. ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
  27. android.R.layout.simple_list_item_1, attractions);
  28. attractionsListView.setAdapter(adapter);
  29.  
  30. // Set item click listener
  31. attractionsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  32. @Override
  33. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  34. String selectedAttraction = attractions[position];
  35. showAttractionDetails(selectedAttraction);
  36. }
  37. });
  38. }
  39.  
  40. private void showAttractionDetails(String attraction) {
  41. // Perform an action based on the selected attraction
  42. // For demonstration purposes, a toast message is shown
  43. Toast.makeText(this, attraction, Toast.LENGTH_SHORT).show();
  44. }
  45. }
  46.  

In this example, the TouristGuideActivity class extends AppCompatActivity.

Inside the onCreate method, the layout view is initialized, and the attractions are retrieved from the resources as an array of strings.

The list view is set up using an ArrayAdapter to display the attractions. Each item in the list is clickable, and when an item is clicked, the showAttractionDetails method is called.

The showAttractionDetails method performs an action based on the selected attraction. In this example, a toast message is shown with the selected attraction name.

Make sure to add the attractions string array resource to the res/values/strings.xml file:


XML Code
  1. <!-- strings.xml -->
  2. <resources>
  3. ...
  4. <string-array name="attractions">
  5. <item>Eiffel Tower</item>
  6. <item>Taj Mahal</item>
  7. <item>Great Wall of China</item>
  8. <item>Machu Picchu</item>
  9. <item>Pyramids of Giza</item>
  10. </string-array>
  11. ...
  12. </resources>
  13.  

    Remember to replace the attractions array with the actual attractions you want to display in your tourist guide app.

    This example provides a basic structure for a tourist guide app. You can customize it by adding more features such as displaying attraction details, images, maps, or integrating with external APIs for additional information.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.