简体   繁体   中英

How can I create a popup menu to be shown when I click on an item in listview(android studio)?

I'm new to android developing... I've created a listview using Strings and Array Adapter in setting.java:

public class setting extends Activity {
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting_layout);
    String[] settingOptions = new String[]{getString(R.string.settingInterface), getString(R.string.settingLanguages), getString(R.string.settingInfo)};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>
            (this, android.R.layout.simple_list_item_1, android.R.id.text1, settingOptions);
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }

    });



}
}

and this is my setting_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true" />
</RelativeLayout>

setting_layout Activity(listView)

I want to create a popup menu for Languages item that when I click on it,shows some other languages translations. I've created a string.xml in other languages but I don't know how to use it in popup menu. 1:How can I make a popup menu shown when I click on Languages item? 2:How to put other languages in popup menu? Thanks in advance

for pop up you can create a dialog using dialog builder, it can be a custom one(xml layout), or can be created dynamically (programmatically using java) check out this code for a pop up view :)

for example I have this function that I call inside the onClick:

private  void showFlavorDialog(){

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        View promptView;
        LayoutInflater layoutInflater = LayoutInflater.from(myActivity.this); //this gets the custom layout inflater 
        promptView = layoutInflater.inflate(R.layout.dialog_flavors_ar, null); // here you put the custom xml leyout name
        final EditText editText = (EditText) promptView.findViewById(R.id.editText_note); //my layout has an edit text and button in it and I want to use it, so call the findviewbyid 
        Button add_note = (Button) promptView.findViewById(R.id.button_note);
        editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); //this removes full screen keyboard, so the keyboard doesnt take the whole screen.
        add_note.setOnClickListener(new View.OnClickListener() { //onClick for the button
            @Override
            public void onClick(View v) {
                otherNote_b = true;
                flavor = editText.getText().toString();
                new httpSetFlavor().execute();
                alertDialogFL.cancel();
            }
        });
        if (selectedflavorNameEn.size() > 1) {
            flavorsGrid = (GridView) promptView.findViewById(R.id.gridView_flavors); // I also have a gridview in the layout 
            flavorsGridAdapter adapter = new flavorsGridAdapter(OrderActivityAr.this,
                    selectedflavorNameEn);
            flavorsGrid.setAdapter(adapter); // this is a custom adapter for the grid which you wont need
        }

        alertDialogBuilder.setTitle(R.string.flavor_ar); //title of the popup window 
        alertDialogBuilder.setView(promptView); //set view of the popup to your custom view 
        alertDialogBuilder.setCancelable(true); //this makes the popup cancelable on back button pressed 
        alertDialogBuilder.setPositiveButton(R.string.delete_flavor_ar, new DialogInterface.OnClickListener() { //this is a button for the popup if you want one
            public void onClick(DialogInterface dialog, int id) {
                deleteFlavor = true;
                new httpSetFlavor().execute();
            }
        });
        alertDialogBuilder.setNegativeButton(R.string.back_ar, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
       AlertDialog alertDialogFL = alertDialogBuilder.create(); //here you create the dialog using its builder 
      alertDialogFL.show(); // show it on screen 

    }

and this is my custom layout xml file, so you can just put any layout you want in it and use it later :)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:id="@+id/gridView_flavors"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:numColumns="3"
        android:horizontalSpacing="2dp"
        android:verticalSpacing="2dp" />



    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/gridView_flavors"
        android:layout_marginTop="5dp">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:paddingRight="10dp"
            android:paddingLeft="10dp"
            android:layout_width="wrap_content"
            android:singleLine="true"
            android:layout_height="wrap_content"
            android:text="@string/add_note_ar"
            android:id="@+id/button_note"
            android:layout_below="@+id/gridView_flavors"
            android:textSize="20dp"
            android:background="@drawable/button_plain_green"
            android:textColor="#000"
            android:layout_weight="0" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText_note"
            android:layout_below="@+id/gridView_flavors"
            android:layout_toRightOf="@+id/other_text"
            android:layout_marginTop="2dp"
            android:layout_toLeftOf="@+id/button_note"
            android:layout_toStartOf="@+id/button_note"
            android:layout_weight="1"
            android:gravity="right" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/other_note_ar"
            android:id="@+id/other_text"
            android:layout_below="@+id/gridView_flavors"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignBottom="@+id/editText_note"
            android:gravity="center"
            android:textSize="20dp"
            android:layout_weight="0"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" />

        <com.andexert.library.RippleView
            android:layout_width="wrap_content"
            android:id="@+id/ripple_button"
            android:layout_weight="0"
            android:layout_height="wrap_content"></com.andexert.library.RippleView>
    </TableRow>
</RelativeLayout>

this is the look of the layout:

在此处输入图片说明

and in your case you can use listview in the custom layout as well :) but with custom list item for example a radio button.

as for displaying other languages saved in xml well you should have them saved in res/strings.xml you can use the strings in an array of strings like this:

String strArray[]={
getResources().getString(R.string.string1),
getResources().getString(R.string.string2),
getResources().getString(R.string.string3),
getResources().getString(R.string.string4)

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM