0

I got a android app with java version(not flutter and not kotlin). There is a AlertDialog,normally with title(setTitle) and listview(setItems). Now I would add a editable textbox between title and listview (Or let the title become the textbox).

How can I do??

Thx for help~

Jamie
  • 21
  • 4

2 Answers2

0

Here is your answer, custom layout for dialog

https://developer.android.com/guide/topics/ui/dialogs#CustomLayout

Phantom Lord
  • 572
  • 3
  • 13
0

If you want an editable textbox between title and list view , then you need to create custom layout. Here is full code and its working fine as shown in imagehere.

public class DialogActivity extends AppCompatActivity {


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

    String names[] ={"A","B","C","D"};
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(DialogActivity.this);
    LayoutInflater inflater = getLayoutInflater();
    View convertView = (View) inflater.inflate(R.layout.list_item, null);
    alertDialog.setView(convertView);
    alertDialog.setTitle("List");
    ListView lv = (ListView) convertView.findViewById(R.id.lv);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,names);
    lv.setAdapter(adapter);
    alertDialog.show();
}}

Here is custom list_item layout

<?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="wrap_content">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Edit Text Here"
    android:id="@+id/edit_text"/>
<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/edit_text"/>
 </RelativeLayout>
Mohammad Arman
  • 508
  • 4
  • 13