Stathis001 / Tourist_Guide

0 stars 0 forks source link

Δημιουργία layout Button που θα είναι το κουμπί όπου θα ανοίγει ένα μικρό μενού επιλογών (φίλτρων). #36

Closed Stathis001 closed 7 months ago

Stathis001 commented 7 months ago

Το μενού επιλογών είναι από μόνο του άλλο αρχείο xml το οποίο πρέπει να γραφτεί ξεχωριστά . Κάθε ένα από αυτά τα αρχεία πρέπει να έχει και τον λίγο κώδικα που θα τα καταστεί λειτουργικά. (Δεν είναι κάτι πολύ δύσκολο και η δουλειά μπορεί να μοιραστέι ανάμεσα σε πολλά άτομα).

Argiris2000 commented 7 months ago

Για να δημιουργήσετε ένα κουμπί που θα ανοίγει ένα μικρό μενού επιλογών (popup menu) στο Android, μπορείτε να χρησιμοποιήσετε το PopupMenu. Ακολουθεί ένα παράδειγμα που μπορείτε να προσθέσετε στο XML σας:

Ανανεωμένο start_screen.xml: <RelativeLayout 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" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".MainActivity">

<!-- Background Image View -->
<ImageView
    android:id="@+id/backgroundImageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/received_663824995734600"
    android:scaleType="centerCrop"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Your existing content -->
    <Button
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start"
        android:textSize="50sp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50sp"/>

    <!-- New Button for Filters -->
    <Button
        android:id="@+id/filtersButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Φίλτρα"
        android:layout_below="@id/startButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"/>
</RelativeLayout>

Ενημερωμένος κώδικας στην MainActivity.java για το PopupMenu: import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.PopupMenu; import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

    Button filtersButton = findViewById(R.id.filtersButton);

    filtersButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showPopupMenu(v);
        }
    });
}

private void showPopupMenu(View view) {
    PopupMenu popupMenu = new PopupMenu(this, view);
    popupMenu.getMenuInflater().inflate(R.menu.filters_menu, popupMenu.getMenu());

    // Προσθέστε ακροατές γεγονότων για τις επιλογές του μενού
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.filter_option_1:
                    // Εκτελέστε κάποια ενέργεια για την επιλογή 1
                    return true;
                case R.id.filter_option_2:
                    // Εκτελέστε κάποια ενέργεια για την επιλογή 2
                    return true;
                // Προσθέστε περισσότερες περιπτώσεις αν χρειάζεται
                default:
                    return false;
            }
        }
    });

    popupMenu.show();
}

} Δημιουργία αρχείου res/menu/filters_menu.xml για τις επιλογές του μενού: Δημιουργήστε ένα νέο φάκελο "menu" στον φάκελο "res" και στη συνέχεια δημιουργήστε ένα αρχείο XML με το όνομα "filters_menu.xml". Εδώ είναι ένα παράδειγμα περιεχομένου:

Αυτός ο κώδικας δημιουργεί ένα Button με το κείμενο "Φίλτρα" και όταν πατηθεί, εμφανίζει ένα μικρό μενού επιλογών με δύο επιλογές. Μπορείτε να προσαρμόσετε τον κώδικα και το περιεχόμενο του μενού σύμφωνα με τις ανάγκες σας.

Stathis001 commented 7 months ago

public void showPopup(View filter) { PopupMenu popup = new PopupMenu(this, filter); popup.setOnMenuItemClickListener(this); popup.inflate(R.menu.popup_filters_menu); popup.show(); }

@Override
public boolean onMenuItemClick(MenuItem item) {
   /* switch (item.getItemId()) {
        case R.id.item1:
            Toast.makeText(this, "Item 1 clicked", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.item2:
            Toast.makeText(this, "Item 2 clicked", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.item3:
            Toast.makeText(this, "Item 3 clicked", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.item4:
            Toast.makeText(this, "Item 4 clicked", Toast.LENGTH_SHORT).show();
            return true;
        default:
            return false;
    }*/
    int id = item.getItemId();
    if (id==R.id.item1){
        Toast.makeText(this, "Item 1 clicked", Toast.LENGTH_SHORT).show();
        return true;
    } else if (id==R.id.item2) {
        Toast.makeText(this, "Item 2 clicked", Toast.LENGTH_SHORT).show();
        return true;
    } else if (id==R.id.item3) {
        Toast.makeText(this, "Item 3 clicked", Toast.LENGTH_SHORT).show();
        return true;
    } else if (id==R.id.item4) {
        Toast.makeText(this, "Item 4 clicked", Toast.LENGTH_SHORT).show();
        return true;
    }
    else {
        return false;
    }
}
Stathis001 commented 7 months ago

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> tools:cotext="com.example.touristguide.MainActivity"

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="showPopup"
    android:text="Filters"
    app:layout_constraintBaseline_toBottomOf="parent"
    app:layout_constraintBaseline_toLeftOf="parent"
    app:layout_constraintBaseline_toRightOf="parent"
    app:layout_constraintBaseline_toTopOf="parent"
    tools:layout_editor_absoluteX="318dp"
    tools:layout_editor_absoluteY="7dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Stathis001 commented 7 months ago

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

Stathis001 commented 7 months ago

Θέλουμε τα filter1-4 να γίνουν Μουσεία , Εκκλησίες, Πάρκα , και ότι άλλο θες απλώς κάνεις τον κώδικα του μάριου Copy Paste και το βάζεις. Η πρώτη συνάρτηση είναι να μπει μέσα στο MapsActivity ενώ τα άλλα 2 είναι ξεχωριστά καινούργια αρχέια xml μέσα στο res/layout.