Adding a vertical scrollbar to an AlertDialog in Android? - android

I would like to add a vertical scrollbar to an AlertDialog because my text is too long to display on 1 screen:
I have tried to use :
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
but the scrollbars don't even display ?
Here's the xml layout file I'm using:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:id="#+id/instructions_view" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A LONG TEXT 1"/>
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A LONG TEXT 2"/>
</LinearLayout>
I call the AlertsDialog with :
public void onClick(View v) {
switch(v.getId()){
case R.id.Button_Instructions:
InstructionsDialog();
break;
case R.id.Button_Exit:
ExitDialog();
break;
}
}
public void InstructionsDialog(){
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setIcon(R.drawable.icon);
ad.setTitle("Instructions ...");
ad.setView(LayoutInflater.from(this).inflate(R.layout.instructions_dialog,null));
ad.setPositiveButton("OK",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
// OK, go back to Main menu
}
}
);
ad.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
// OK, go back to Main menu
}}
);
ad.show();
}
I found the answer now=> IT WORKS NOW WITH THIS :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:id="#+id/instructions_view" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A LONG TEXT 1"/>
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A LONG TEXT 2"/>
</LinearLayout>
</ScrollView>

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.

AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("YOUR_TITLE")
.setMessage("YOUR_MSG")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_info)
.show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setMaxLines(5);
textView.setScroller(new Scroller(this));
textView.setVerticalScrollBarEnabled(true);
textView.setMovementMethod(new ScrollingMovementMethod());

All code found here is deprecated.
You should now use (in the layout file for your dialog)
NestedScrollView
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:fillViewport="true"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Besides, it is no more recommanded to use AlertDialog alone. You have to implement your own DialogFragment by extending DialogFragment.
See : https://developer.android.com/guide/topics/ui/dialogs
Then you can call your dialog fragment :
MyDialogFragment dialogFragment = MyDialogFragment.newInstance();
dialogFragment.show(fragment.getFragmentManager(), MyDialogFragment.TAG);

Using this it works :
.setScrollable(true)
Use this in your AlertDialogBox or MaterialStyledDialog.
private void gettermsandconditions() {
final MaterialStyledDialog dialogHeader_1 =
new MaterialStyledDialog.Builder(this)
.setTitle("Terms and Conditions !")
// .setDescription("What can we improve? Your feedback is always welcome.")
.setDescription(R.string.Terms_and_condition)
.setIcon(R.drawable.bill_icon)
.setStyle(Style.HEADER_WITH_ICON)
.setHeaderColor(R.color.colorPrimary)
.withDarkerOverlay(true)
.setScrollable(true)
.onPositive(new MaterialDialog.SingleButtonCallback() {
#Override
public void onClick(#NonNull MaterialDialog dialog, #NonNull DialogAction which) {
// startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + context.getPackageName())));
}
})
.setNegativeText("Ok")
.build();
//.setStyle(Style.HEADER_WITH_TITLE)
dialogHeader_1.show();
}

On Android Q the scrollbars can be made always visible.
After creating and showing the dialog, find the TextView holding the message and modify it like this. (BTW the call to setMaxLines(5) is just to limit the number of visible lines to demonstrate the scrolling. Set it as you will, or remove it):
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setMaxLines(5);
textView.setVerticalScrollBarEnabled(true);
textView.setMovementMethod(new ScrollingMovementMethod());
textView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
textView.setScrollbarFadingEnabled(false);
textView.setVerticalScrollbarTrackDrawable(m_mainActivity.getResources().getDrawable(R.drawable.scrollbar_track_vertical));
textView.setVerticalScrollbarThumbDrawable(m_mainActivity.getResources().getDrawable(R.drawable.scrollbar_thumb_vertical));
}
Two drawables are needed.
For app/src/main/res/drawable/scrollbar_thumb_vertical.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="0"
android:endColor="#D8D8D8"
android:startColor="#F7F7F7" />
<corners android:radius="6dp" />
</shape>
And for app/src/main/res/drawable/scrollbar_thumb_vertical.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="0"
android:endColor="#757575"
android:startColor="#A0A0A0" />
<corners android:radius="6dp" />
</shape>

Related

AlertDialog custom view is not appearing

I'm building a simple AlertDialog with custom layout from XML. Here is how it's supposed to look (taken from Eclipse): It's simply 4 columns:
Here is the xml of the image above:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1.0" >
<ImageView
android:id="#+id/colorpicker_dialog_color1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#FFF"
android:tag="0xFFF" />
<ImageView
android:id="#+id/colorpicker_dialog_color2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#FFDD66"
android:tag="0xFFDD66" />
<ImageView
android:id="#+id/colorpicker_dialog_color3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#66CCFF"
android:tag="0x66CCFF" />
<ImageView
android:id="#+id/colorpicker_dialog_color4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#B6C0D2"
android:tag="0xB6C0D2" />
</LinearLayout>
Here is the code of the costume Alert Dialog:
public void showColorPickerDialog() {
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater
.inflate(R.layout.color_picker_dialog, null);
ImageView clr1 = (ImageView) layout
.findViewById(R.id.colorpicker_dialog_color1);
clr1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// .. code
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("cccc");
builder.setView(layout)
.setCancelable(true)
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
}).show();
}
The problem is that in my dialog I dont see any column.
Try using Views instead of ImageViews in your layout. The problem using ImageViews in your case is that since you do not specify the src attribute for the ImageView, it will display a 0x0-sized image, which leaves your views invisible.

Button under ListView in AlertDialog is hidden

I want to show a list with a button under it in an AlertDialog. The Button is to close the Dialog itself. I extended AlertDialog and added some LayoutParams to the Window to extend the Dialog of the whole screen width:
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
ok. But if the list is long (display is full of it), I can scroll the ListView but the button is not shown under it. Here's the ContentView of the Dialog:
<?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/choose_equipment_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_below="#+id/choose_equipment_list"
android:id="#+id/btn_choose_equipment_close"
style="#style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close" />
</RelativeLayout>
What can I do to show the button ALWAYS under the list, no matter how long it is? (I read a lot warnings to put a ListView inside a ScrollView, tried it anyway and failed...)
Try it:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/choose_equipment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/btn_choose_equipment_close"
style="#style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close" />
</LinearLayout>
If not work, remove the style="#style/custom_button" line. Some style configuration could change the view style and hide the button.
I want to show a list with a button under it in an AlertDialog.
So What you have to do is :
Remove this from from your Button:
android:layout_below="#+id/choose_equipment_list"
Then put this in Button:
android:layout_alignParentBottom="true"
And Put this in ListView:
android:layout_above="#+id/btn_choose_equipment_close"
You are Done.
Hmm. Maybe try to use Fragments. I coded sample for you. Tell me if it is what you are looking for:)
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
findViewById(R.id.open_dialog_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SampleDialogFragment dialogFragment = new SampleDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "dialogFragment");
}
});
}
}
main_activity.xml
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/open_dialog_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open dialog"
android:layout_gravity="center" />
</FrameLayout>
SampleDialogFragment.java
public class SampleDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Timer");
alert.setPositiveButton("OK", null);
View view = View.inflate(getActivity(), R.layout.dialog, null);
String[] array = {"Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
((ListView) view.findViewById(R.id.listView)).setAdapter(adapter);
alert.setView(view);
return alert.create();
}
}
dialog.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I think it could be possible to get what you need declaring in the XML your button first located in the bottom and then the listview above it with height set to fill_parent
This is should be what you want...
<?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/choose_equipment_list"
android:layout_above="#+id/btn_choose_equipment_close"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_choose_equipment_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="close" />
</RelativeLayout>

How to keep DialogFragment positive/negative buttons above soft keyboard

I am using a DialogFragment. I want the positive and negative buttons to remain above the keyboard when the user interacts with an edit text like in the example pics below from a screen in the Gmail tablet application.
In my attempt which doesn't work, here's my Dialog Fragments onCreateDialog method
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater factory = LayoutInflater.from(getActivity());
final View textEntryView = factory.inflate(R.layout.my_layout, null);
return new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string.paypal))
.setView(textEntryView)
.setPositiveButton(R.string.dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}
)
.setNegativeButton(R.string.dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}
)
.create();
}
and here's the R.layout.my_layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
<ImageView android:id="#+id/paypal_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
/>
<EditText
android:id="#+id/edit_paypal_email"
style="#style/GeneralEditText"
android:hint="#string/contact_info_text_email"
android:inputType="textEmailAddress"
/>
<View
android:id="#id/horizontal_line"
style="#style/HorizontalLine"
android:layout_marginTop="#dimen/contact_info_line_margin_top" />
<!-- Notes -->
<TextView
android:text="#string/paypal_info1"
style="#style/ContactInfoNotes"
android:paddingBottom="5dip"
/>
<TextView
android:text="#string/paypal_info2"
style="#style/ContactInfoNotes"
android:paddingBottom="5dip"
/>
<TextView
android:text="#string/paypal_info3"
style="#style/ContactInfoNotes"
android:paddingBottom="5dip"
/>
<TextView
android:text="#string/paypal_info4"
style="#style/ContactInfoNotes"
android:paddingBottom="5dip"
/>
</LinearLayout>
</ScrollView>
In my implementation, the soft keyboard comes up and covers the dialogs positive and negative buttons. What am I missing to allow the buttons to remain above the keyboard?
Thanks in advance.
Just add this on your onViewCreated:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
super.onViewCreated(view, savedInstanceState);
}
I had the same problem, and i found a solution. The essential part of the layout file:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<!-- Form items -->
</ScrollView>
<!-- Include the layout which contains the buttons -->
<include layout="#layout/fragment_dialog_button_bar" android:layout_weight="0" />
So you need to set the buttons' layout_weight 0, while the scrollview's weight is 1 or bigger.
I hope this will help.
I believe this has to do with the LinearLayout and ScrollView. Remove the fillViewport part from the ScrollView and show the scrollbar in the LinearLayout.
<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="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<!-- Stuff -->
</LinearLayout>
</ScrollView>
Have you tried adding the following attribute to the activity tag in your manifest?
<activity android:name=".SampleActivity"
android:windowSoftInputMode="adjustResize"
... >
</activity>
This is what worked for me.
public class Fragment1 extends DialogFragment{
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
super.onViewCreated(view, savedInstanceState);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_weight="1"
android:layout_height="300dp" >
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inputType="textMultiLine|textAutoCorrect|textCapSentences"
/>
</ScrollView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>
</LinearLayout>

Android : How to add an item to a listview onclick of a button?

I have a listview and a button in my layout file. I want to add items to listview on click of that button. The listview should be empty when the activity is started but it should grow by adding the items to it. On click of "AddItem" button i'am showing one AlertDialog to take item/input from user. How do i add that item to listview? Please help me with code. Which adapter should i use to use my own layout inside ListView? Should i extend ListActivity ?
Here is my layout file : my_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#android:color/black"
android:dividerHeight="2dp" >
</ListView>
<Button
android:id="#+id/addItemButtom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_weight="1"
android:text="Add Item" />
</LinearLayout>
Here is my row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView android:layout_weight = "1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Newly Added Item : "
android:textColor="#android:color/black"
android:textSize="17dp" />
<TextView android:layout_weight = "1"
android:id="#+id/itemTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Here is my java file :
public class MyList extends Activity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_list);
findViewById(R.id.addItemButtom).setOnClickListener(this);
}
#Override
public void onClick(View v)
{
switch (v.getId()) {
case R.id.enterInverter:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText et = new EditText(this);
et.setHint("Enter Item");
et.setMaxLines(1);
et.setTextSize(17);
alert.setTitle("Enter Item");
alert.setView(et);
alert.setInverseBackgroundForced(true);
alert.setPositiveButton("Add", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String item = et.getText().toString().trim();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.cancel();
}
});
alert.show();
break;
default:
break;
}
}
}
The ArrayAdapter might be the point to start. Of course it would change according to your list, but I suggest you to use ArrayAdapter. Get the reference of your list, define a new adapter for it and set it with setAdapter method. Then, you can add/remove items to your adapter.
where is your listview?
when you add your list item in your list(Array or Arraylist or...)
your_adapter.notifyDataSetChanged();
it makes draw your fresh listview

Android problem: Custom dialog layout is used instead of main.xml for root activity

I was following this guide to make a custom AlertDialog. I want to make a dialog with buttons and a title like an AlertDialog, but also with an EditText box, which a regular AlertDialog can't use. So I made the following custom layout containing the EditText box in a file called add_player_dialog.xml in res/layout, which will be called by AlertDialog.Builder's setView():
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/add_player_dialog_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<EditText
android:id="#+id/player_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:textSize="20sp"
/>
</LinearLayout>
Then in the activity (not the root activity, btw) that starts the dialog, I have the following in a switch in onCreateDialog():
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_player_dialog,
(ViewGroup) findViewById(R.id.add_player_dialog_layout));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter player's name")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), "yes", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setView(layout);
AlertDialog alert = builder.create();
The problem is that the activity that is first used when the application is launched uses add_player_dialog.xml for its layout, not main.xml. How do I avoid this?
=================
EDIT: I have found a programmatic way to accomplish this, if anyone is interested:
import android.R.drawable;
EditText et = new EditText(this);
et.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
et.setBackgroundResource(drawable.editbox_background);
et.setTextSize(20);
=================
If you're interested, here is main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="40dip"
>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center"
>
<Button
android:id="#+id/new_game_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/new_game_button"
/>
<Button
android:id="#+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/continue_button"
/>
</LinearLayout>
</LinearLayout>
Did you try/have
setContentView(R.layout.main);
in the onCreate method?
If so, sorry, hopefully someone else can better chime in.
Try deleting gen/R.java and letting the build system regenerate it, I have seen that sometimes when you add IDs it gets out of sync and needs to be forcefully regenerated.

Categories

Resources