I want to create a scrollable dialog box? [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Adding a vertical scrollbar to an AlertDialog in Android?
In my project, there is some button.Whenever I click on one button ,it shows a alert dialog box with the description of that button.i have wrote description in string.xml with . whenever i click the button, it fetch the description from string.xml and shows us.But my problem is that my message( which will show in dialog box)is too long.so,i want scrollable alertdialog box.with out creating another xml file with, how is it possible in java code.

You are able to customize the dialog box by using the setView method and apply a custom view to the dialog box.
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
}}
);
I hope this is helpful.

this is an example.you can change according to your requirement
you can add this on button event
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.alert);
Spinner spin = (Spinner)dialog.findViewById(R.id.spinner1);
Spinner spin2 = (Spinner)dialog.findViewById(R.id.spinner2);
EditText t1 =(EditText)dialog.findViewById(R.id.editText1);
EditText t2 =(EditText)dialog.findViewById(R.id.editText2);
EditText t3 =(EditText)dialog.findViewById(R.id.editText3);
EditText t4 =(EditText)dialog.findViewById(R.id.editText4);
dialog.show();
alert.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/widget54"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="211dp"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="11dp"
>
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="208dp"
android:layout_height="wrap_content"
android:layout_x="19dp"
android:layout_y="76dp"
/>
<EditText
android:id="#+id/editText3"
android:layout_width="208dp"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="138dp"
/>
<EditText
android:id="#+id/editText4"
android:layout_width="212dp"
android:layout_height="wrap_content"
android:layout_x="18dp"
android:layout_y="196dp"
/>
<Spinner
android:id="#+id/spinner1"
android:layout_width="222dp"
android:layout_height="wrap_content"
android:layout_x="5dp"
android:layout_y="254dp" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="310dp" />
</LinearLayout>
</ScrollView>

Related

Need help in xamarin.android with timepicker

I am new in developing android apps using Xamarin. Am developing a app which contains many dialog and activities, one of which dialog has layout like this, a timepicker, two buttons labeled as "CANCEL" and "OK" and a textview. I want to do something like this, when user click on "OK" button the time selected in timepicker will display on textview. (or i want to extract value of timepicker in button click event)
It's pretty simple. Check this out:
Your Main.axml(layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TimePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/picker" />
<Button
android:text="OK"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnOK" />
<Button
android:text="Cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnCancel" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt"
android:textSize="40dp" />
</LinearLayout>
and your OnCreate() method must contains this:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button OK = FindViewById<Button>(Resource.Id.btnOK);
Button Cancel = FindViewById<Button>(Resource.Id.btnCancel);
TimePicker picker = FindViewById<TimePicker>(Resource.Id.picker);
TextView txt = FindViewById<TextView>(Resource.Id.txt);
OK.Click += (sender, e) =>
{
//getting values from TImePicker via CurrentHour/CurrentMinutes
txt.Text = String.Format("{0} : {1}",picker.CurrentHour,picker.CurrentMinute);
};
}

How to centralize buttons on android?

I'm having troubles to create a layout like that, the Dialog on left side of image. But I want to create it using horizontal orientation. I want that my layout have this appearance, but only with two buttons horizontally.
My problem is the layout xml file, I don't know how to start this layout... =(
The real problem is how to edit the xml file to the buttons get centralized, I tried a lot of thinks, padding, orientation, align etc... But I cant align that. My current layout is that. How can I centralize the buttons?
Can someone help me?
And here is my xml file code...
<?xml version="1.0" encoding="utf-8"?>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:id="#+id/textView6"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:id="#+id/textView7"
/>
Can someone help me?
you can do somethnig like this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="#+id/button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="#+id/button2" />
</LinearLayout>
</RelativeLayout>
First, you need to create your desired layout with the buttons that you mentioned (I figured you can do this yourself). Then, you would use that layout to inflate an AlertDialog as I have done below:
View alertView = getLayoutInflater().inflate(
R.layout.your_custom_layout,
(ViewGroup) findViewById(R.id.alertViewLayout));
new AlertDialog.Builder(this)
.setTitle(title)
.setView(alertView)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// handle click here
// dismiss AlertDialog
dialog.dismiss();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// handle click here
// dismiss AlertDialog
dialog.dismiss();
}
}).show();
You could access the views in the AlertDialog's layout by referencing the parent view (alertView);
ImageButton button1 = (ImageButton) alertView.findViewById(R.id.button1);
ImageButton button2 = (ImageButton) alertView.findViewById(R.id.button2);
Edit
This is a sample layout file with 2 ImageButtons placed side-by-side.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/alertViewLayout"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:layout_weight=".5"
android:background="#+drawable/imageButton1"
android:text="Button 1" />
<ImageButton
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_weight=".5"
android:background="#+drawable/imageButton2"
android:text="Button 2" />
</LinearLayout>
Edit 2
Since you want your buttons to have custom backgrounds, I suggest you use an ImageButton and set the background property to your choice background.
If you want more details on how to create a layout file, you can check Android Developer documentation.
Let me know if this helps.
Could you be more clear in your question please? I don't know if I got it right but I will try to explain it.
First, if you want to create a custom layout for you dialog, you should create a .xml file and then inflate it with this specific layout.
But I think that is not your case, what you want to do it's to simply create a custom dialog, right? The screen orientation doesn't matter (I don't know if you meant it).
I don't think I should explain you the whole process or just paste the code, it's better to you to get used with android official documentation, so take a look at this link: Dialogs | Android Development
Hope it can help you.
Simplest answer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation = "horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:id="#+id/textView6"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:id="#+id/textView7"/>
</LinearLayout>

android check box and radio group on custom dialog

Hi I have written simple custom dialog . which has few check boxs and one submitt button .
whenever I tried to read the checkbox apllication throws Nullpointer exception .. can somebody helps to solve this , below is my custom dailog code
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
CheckBox chk1= (CheckBox) findViewById(R.id.chkbox1);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(chk1.isChecked())
dialog.dismiss();
}
});
dialog.show();
}
NullPointerException because you didn't instantiated with dialog.findViewById() and set OnClickListener for the CheckBox. Place it as below:
CheckBox chk1= (CheckBox)dialog.findViewById(R.id.chkbox1);
chk1.setOnClickListener(new OnClickListener() {
//do something here
});
Change:
CheckBox chk1= (CheckBox) findViewById(R.id.chkbox1);
to:
CheckBox chk1= (CheckBox) dialog.findViewById(R.id.chkbox1);
Remember that if you're simply using findViewById(), you're calling it for Activity in which you currently are, but as far as I see, you want to find this CheckBox in R.layout.custom which is set for dialog.
I see that you're properly loading dialogButton, so you probably just forgot to do the same with chk1.
When you have populated a layout for dialog then you need to access it through dialog. But you are accessing it through parent view. Anyway just call it through dialog.findViewById(R.id.chkbox1)
I am trying add radio group to the custom dialog .. It comes when dialog is loaded , but how to add the action lister to that radio group in the dialog .. below is my custom layout xml .
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="#android:style/Theme.Light">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
/>
<CheckBox
android:id="#+id/chksmart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMARTAPPLIANCE "
android:layout_below="#+id/editText"
/>
<CheckBox
android:id="#+id/meter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Device"
android:layout_below="#+id/chksmart"
/>
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/android:list"
android:textColor="#android:color/black" >
<RadioButton
android:id="#+id/radioGet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="GET"
android:textColor="#android:color/background_dark" />
<RadioButton
android:id="#+id/radioPut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="PUT"
android:textColor="#android:color/background_dark" />
<RadioButton
android:id="#+id/radioPost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="POST"
android:textColor="#android:color/background_dark" />
<RadioButton
android:id="#+id/radioDelete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="DELETE"
android:textColor="#android:color/background_dark" />
<RadioButton
android:id="#+id/radioevent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="ADDEVENT"
android:textColor="#android:color/background_dark" />
</RadioGroup>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" SUBMIT "
android:textColor="#00000f"
android:textSize="25px"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/radiogroup"
/>

Showing dialog boxes with four buttons

hy ! i am using dialog im my option . i want four button in that dialog box . i have added three buttons one is
myDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener()
2nd is
myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
and the 3rd one is myDialog.setNeutralButton("Add", new DialogInterface.OnClickListener() but im unable to add 4th one as i tried one more setPositiveButton but its showing only one .how i can add 4th one plz give me hint.
thank you.
If you just want to use a notmal dialog something like this should work
Dialog settingsDialog = new Dialog(this);
settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.button_layout
, null));
settingsDialog.show();
button_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="OK" android:onClick="dismissListener"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="OK" android:onClick="dismissListener"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="OK" android:onClick="dismissListener"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="OK" android:onClick="dismissListener"/>
</LinearLayout>
You can create the layout according to your requirement.
You can't. In that case you must create a custom view for your Dialog and put four buttons at the bottom of that view (at the top, you put a TextView with the message).

AlertDialog in android

How can I show in AlertDialog two EditText and 2 TextView?
custom_dialog.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="horizontal"
android:id="#id/layout_root" android:padding="10.0dip"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Libellé"
android:id="#+id/Text_Libelle"
/>
<EditText android:id="#+id/Edit_Libelle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<EditText android:id="#+id/Edit_Url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
On click on button I want to show this interface in alert dialog.
You basically have the layout created so all you have to do is assign it to your custom dialog like so..
Set the custom layout to the dialog after initializing..
Dialog dialog = new Dialog(someContext);
dialog.setContentView(R.layout.customdialoglayout); //Set dialog view to custom layout
To wire up your handlers you will have to do something to the effect of..
EditText myText = (EditText)dialog.findViewById(R.id.Edit_Libelle);
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

Categories

Resources