Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How do I make this alert dialog scrollable?

Tags:

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!

like image 652
ashu Avatar asked Jun 06 '13 06:06

ashu


People also ask

How do I make a scrollable dialogue?

Builder alertDialog = new AlertDialog. Builder(this); alertDialog. setTitle("Title"); alertDialog. setMessage("Here is a really long message."); alertDialog.

What is the difference between an alert and an alert dialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

How can I make alert dialog fill 100% of screen size?

According to Android platform developer Dianne Hackborn in this discussion group post, Dialogs set their Window's top level layout width and height to WRAP_CONTENT . To make the Dialog bigger, you can set those parameters to MATCH_PARENT . Demo code: AlertDialog.


2 Answers

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.

like image 76
blganesh101 Avatar answered Sep 18 '22 17:09

blganesh101


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(); 
like image 21
Mukesh Kumar Singh Avatar answered Sep 18 '22 17:09

Mukesh Kumar Singh