Reputation: 1826
I am a beginner in android and making my first android app. my 'About' menu item, when clicked shows an AlertDialog with a really long message. I have been trying different methods to make it scrollable but I couldn't. I have tried reading different questions on StackOverflow but they didn't work for me. Here is my AlertDialog code.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Title");
alertDialog.setMessage("Here is a really long message.");
alertDialog.setButton("OK", null);
AlertDialog alert = alertDialog.create();
alert.show();
Can anybody explain to me in detail how to make it scrollable? Any help or suggestions would be appreciated!
Upvotes: 29
Views: 69593
Reputation: 34195
Android Scrollable AlertDialog
You can just use a default approach:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title").setMessage(message);
AlertDialog alert = builder.create();
alert.show();
You can notice that a message TextView is built-in ScrollView container in alert_dialog.xml
. It is a layout that is used.
The location of alert_dialog.xml
<some_path>/Android/sdk/platforms/android-<version>/data/res/layout/alert_dialog.xml
//e.g.
/Users/alex/Library/Android/sdk/platforms/android-30/data/res/layout/alert_dialog.xml
Upvotes: 3
Reputation: 541
Or you can do it programmatically. This is especially helpful if you want a non-standard dialog box. (e.g. in the below example, I want to add a EditText field in addition to my message.) You can also add buttons, as normal.
final ScrollView myScroll = new ScrollView(this);
final TextView myText = new TextView(this);
myText.setText("Put really long text here.");
myText.setPadding(30, 5, 30, 0); //or whatever you want
final LinearLayout myView = new LinearLayout(this);
myView.setOrientation(LinearLayout.VERTICAL);
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT );
myView.addView(myText);
myView.addView(input);
myScroll.addView(myView);
builder.setView(myScroll);
Upvotes: 1
Reputation: 4522
In that situation you can create your own layout.xml file containing a Text View under Scroll View. and set TextMessage in this Text View, Inflate this layout with your alert dialog box.
yourxmlfile.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textmsg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello" />
</LinearLayout>
</ScrollView>
In Activity Class
LayoutInflater inflater= LayoutInflater.from(this);
View view=inflater.inflate(R.layout.yourxmlfile, null);
TextView textview=(TextView)view.findViewById(R.id.textmsg);
textview.setText("Your really long message.");
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Title");
//alertDialog.setMessage("Here is a really long message.");
alertDialog.setView(view);
alertDialog.setButton("OK", null);
AlertDialog alert = alertDialog.create();
alert.show();
Upvotes: 13
Reputation: 3695
This solution is take from this post.
In order for a view to scrollable, it must be nested inside of a ScrollView container:
<ScrollView>
<LinearLayout android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TextView />
<Button />
</LinearLayout>
</ScrollView>
Note that a ScrollView container can only have one child layout view. It is not possible, for example, to place a TextView and Button in a ScrollView without the LinearLayout.
Upvotes: 39