Reg
Reg

Reputation: 761

Alert dialog Android 2.3 issue

I have a problem with alert dialogs heres my code:

new AlertDialog.Builder(GestureShortcutsMainActivity.this)

.setTitle("Disclaimer")
.setIcon(R.drawable.alert_icon)
.setMessage(R.string.disclaimer)
.setPositiveButton("I Accept", 
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {


                            }
                        })
.setCancelable(false)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
})
.show();

This is what it looks like in android 4.0.4:

alert dialog 4.0.4

This is what it looks like in android 2.3.1:

alert dialog 2.3.1

How can I make it so it display correctly on both versions?

Upvotes: 0

Views: 1849

Answers (2)

ibrahimyilmaz
ibrahimyilmaz

Reputation: 2335

Your theme is not available in Android 2.3. (Ex: Android 2.3 not include Theme_Holo)

public void setThemeForAPIVersion()
{
    if((Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB))
    {
        setTheme(android.R.style.Theme_Holo);
    }
    else
    {
        setTheme(android.R.style.Theme_NoTitleBar);
    }
}

and in onCreate of your activity call

        setThemeForAPIVersion(); 

Upvotes: 1

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39718

I recommend you put the message inside of a vertical scroll bar. This question shows an example. Have the XML code look like this (This is incomplete...)

<ScrollView android:id="scrollDialog">
    <LinearLayout android:orientation="vertical"
            android:scrollbars="vertical"
            android:scrollbarAlwaysDrawVerticalTrack="true">
        <TextView android:id="text" />
        <Button />
    </LinearLayout>
</ScrollView>

Then in your code, find the TextView, and set the AlertDialog to use that layout

AlertDialog.Builder builder=new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout;
layout = inflater.inflate(R.layout.scrollDialog,null);
TextView text=(TextView) findViewById(R.id.text);
text.setText(R.string.disclaimer);
setContentView(layout);

Upvotes: 0

Related Questions