How to create a popup window programmatically in Android - java

I have made Activity which will popup on click of button. I have to add listener on Image Button. I have made the design in XMl but I want to design programmatically.
This is my image of xml design.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".PopupMenu">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:orientation="horizontal"
android:rowCount="1">
<ImageView
android:id="#+id/imageOnlineIcon"
android:layout_width="28dp"
android:layout_height="28dp"
app:srcCompat="#drawable/olnine_user"
android:layout_row="0"
android:layout_column="0" />
<TextView
android:id="#+id/textViewUserName"
android:layout_width="201dp"
android:layout_height="44dp"
android:text="Shyam"
android:textAlignment="center"
android:textSize="22sp" />
<ImageButton
android:id="#+id/imageVideoIcon"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#drawable/videonline" />
<ImageButton
android:id="#+id/imageMuteIcon"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#drawable/muted" />
</GridLayout>
<View style="#style/Divider"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:orientation="horizontal"
android:rowCount="1">
<ImageView
android:id="#+id/imageOnlineIcon1"
android:layout_width="28dp"
android:layout_height="28dp"
app:srcCompat="#drawable/olnine_user" />
<TextView
android:id="#+id/textViewUserName1"
android:layout_width="201dp"
android:layout_height="44dp"
android:text="Samir"
android:textAlignment="center"
android:textSize="22sp" />
<ImageButton
android:id="#+id/imageVideoIcon1"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#drawable/videonline" />
<ImageButton
android:id="#+id/imageMuteIcon1"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#drawable/muted" />
</GridLayout>
<View style="#style/Divider"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:orientation="horizontal"
android:rowCount="1">
<ImageView
android:id="#+id/imageOnlineIcon2"
android:layout_width="28dp"
android:layout_height="28dp"
app:srcCompat="#drawable/olnine_user" />
<TextView
android:id="#+id/textViewUserName2"
android:layout_width="201dp"
android:layout_height="44dp"
android:text="Vivek"
android:textAlignment="center"
android:textSize="22sp" />
<ImageButton
android:id="#+id/imageVideoIcon2"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#drawable/videonline" />
<ImageButton
android:id="#+id/imageMuteIcon2"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#drawable/muted" />
</GridLayout>
<View style="#style/Divider"/>
</LinearLayout>
I have to create the same design in java programatically. And Add the listener in Image Button so that i can handle the button.

I think, you told about AlertDialog
You can try to write this method:
public void entre () {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Something");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//what you need to do after click "OK"
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//what you need to do after click "Cancel"
}
});
alert.show();
}
And add it in onClickListner:
public void onButtonClick(View view) {
entre ();
}
You will get popup window with title "Something" and with two buttons: "Ok" and "Cancel"

Related

Android Java Set OnClick to a ImageView within multiple nested tags

Hi everyone I'm trying to create a menu that comes down when clicking on the profile picture:
but as you can see that ImageView is inside a cardView which is in turn inside a tool bar and as the initial parent I have an App Bar layout:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryPopHome"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.bar"
>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerInside"
android:src="#drawable/poplogo"
/>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
app:cardCornerRadius="80dp">
<ImageView
android:id="#+id/img_profilomain"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:backgroundTint="#color/white"
android:scaleType="centerCrop"
/>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.appcompat.widget.ActionMenuView
android:id="#+id/mnuItem"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout"
android:background="#color/def2"
android:visibility="invisible"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/poplike"
android:backgroundTint="#color/redButton2"
app:borderWidth="0dp"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<!--Floating action button for add alarm-->
<!--Make sure that you are constraining this
button to the parent button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_alarm_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="#+id/add_fab"
app:layout_constraintEnd_toEndOf="#+id/add_fab"
app:layout_constraintStart_toStartOf="#+id/add_fab"
app:srcCompat="#drawable/ic_baseline_add_24"
app:borderWidth="0dp"
android:backgroundTint="#color/redButton2"
/>
<!--Action name text for the add alarm button-->
<!--Make sure that you are constraining this Text to
the add Alarm FAB button-->
<TextView
android:id="#+id/add_alarm_action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Crea Post"
app:layout_constraintBottom_toBottomOf="#+id/add_alarm_fab"
app:layout_constraintEnd_toStartOf="#+id/add_alarm_fab"
app:layout_constraintTop_toTopOf="#+id/add_alarm_fab" />
<!--Floating action button for add person-->
<!--Make sure that you are constraining this
button to the add Alarm FAB button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_person_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="#+id/add_alarm_fab"
app:layout_constraintEnd_toEndOf="#+id/add_alarm_fab"
app:layout_constraintStart_toStartOf="#+id/add_alarm_fab"
app:srcCompat="#drawable/ic_baseline_search_24"
app:borderWidth="0dp"
android:backgroundTint="#color/redButton2"
/>
<!--Action name text for the add person button-->
<!--Make sure that you are constraining this Text
to the add Person FAB button-->
<TextView
android:id="#+id/add_person_action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Cerca utenti"
app:layout_constraintBottom_toBottomOf="#+id/add_person_fab"
app:layout_constraintEnd_toStartOf="#+id/add_person_fab"
app:layout_constraintTop_toTopOf="#+id/add_person_fab" />
<Button
android:id="#+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="152dp"
android:text="Logout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/appBarLayout"
app:layout_constraintVertical_bias="0.111" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcViewPostMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout"
tools:layout_editor_absoluteX="-16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried to set an OnClick listener from the java code but when I go to run the app the onclick doesn't work when I click on the image:
ImageView img_profilo = findViewById(R.id.img_profilomain);
img_profilo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "d", Toast.LENGTH_SHORT).show();
mnuItem.setVisibility(View.VISIBLE);
}
});
I tried to do it with the toolbar too but it doesn't work anyway:
tlb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "sad", Toast.LENGTH_SHORT).show();
}
});
Edit:
It doesn't work with this either
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getItemId() == R.id.img_profilomain)
Toast.makeText(MainActivity.this, "wds", Toast.LENGTH_SHORT).show();
return true;
}
});
Would anyone know how to solve this problem?

How to access an item inside layout dialog?

Dialog was designed by the XML layer and named layout inside it contains a collection of spinner and TextView and some buttons are called from main activity class now I want to know which one of the spinner inside the
D1 () function
I want to define the elements within the layer layout , How do I do that?
//layout.xml
<?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="260dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="#f71717">
<LinearLayout
android:id="#+id/l_layout"
android:paddingTop="10dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:ignore="ObsoleteLayoutParam">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#ffffff"
android:text="وقت التسليم"
android:textSize="20dp"
tools:ignore="HardcodedText,SpUsage" />
<Spinner
android:id="#+id/tex"
android:layout_width="172dp"
android:paddingRight="40dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:drawSelectorOnTop="true"
android:popupBackground="#c853d7"
style="#style/spinner_style"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry,SpUsage"
android:entries="#array/day_"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/rl"
android:layout_width="250dp"
android:layout_height="129dp"
android:layout_below="#+id/l_layout"
android:background="#f71717"
android:layout_marginTop="0dp"
tools:ignore="ObsoleteLayoutParam">
<TextView
android:id="#+id/text_h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/spinner_minutes2"
android:paddingLeft="10dp"
android:text="ساعة"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />
<TextView
android:id="#+id/text_m"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/spinner_minutes2"
android:layout_alignBottom="#+id/spinner_minutes2"
android:layout_alignStart="#+id/button_holder"
android:paddingLeft="10dp"
android:text="دق"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />
<TextView
android:id="#+id/text_pam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="ص/م"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry"
android:layout_marginEnd="12dp"
android:layout_alignBaseline="#+id/spinner_minutes"
android:layout_alignBottom="#+id/spinner_minutes"
android:layout_alignEnd="#+id/spinner_minutes3" />
<Spinner
android:id="#+id/spinner_minutes"
android:layout_width="85dip"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
style="#style/spinner_style"
android:layout_alignStart="#+id/spinner_minutes2"
android:entries="#array/fruits" />
<Spinner
android:id="#+id/spinner_minutes2"
android:layout_width="85dip"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner_minutes"
android:layout_marginStart="16dp"
style="#style/spinner_style"
android:layout_toEndOf="#+id/text_m"
android:entries="#array/fruits" />
<Spinner
android:id="#+id/spinner_minutes3"
android:layout_width="85dip"
android:layout_height="wrap_content"
style="#style/spinner_style"
android:paddingRight="20dp"
android:entries="#array/apm"
tools:ignore="RtlHardcoded,RtlSymmetry"
android:layout_alignBaseline="#+id/spinner_minutes2"
android:layout_alignBottom="#+id/spinner_minutes2"
android:layout_toEndOf="#+id/spinner_minutes" />
<TextView
android:id="#+id/text_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone" />
<LinearLayout
android:id="#+id/button_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner_minutes"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"
android:layout_marginTop="20dip">
<Button
android:id="#+id/button_set"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="10dip"
android:text="Set"
tools:ignore="ButtonStyle,HardcodedText,RtlHardcoded" />
<Button
android:id="#+id/button_cancel"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginRight="10dip"
android:text="Cancel"
tools:ignore="ButtonOrder,ButtonStyle,HardcodedText,RtlHardcoded" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
// class: mainactivity
public class MainActivity extends AppCompatActivity {
Button buttonstartSetDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
buttonstartSetDialog.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
d1();
}});
}
public void d1(){
Dialog about_dlg = new Dialog(MainActivity.this);
about_dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
about_dlg.setContentView(R.layout.layout);
about_dlg.show();
//Spinner spinner = (Spinner) findViewById(R.id.spinner);
}
}
to access to Spinner in your layout :
public void d1(){
Dialog about_dlg = new Dialog(MainActivity.this);
about_dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
about_dlg.setContentView(R.layout.layout);
Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
about_dlg.show();
}

buttons in sliding drawer are not responding

I am creating an project of android application by linking MySQL database using PHP. I am facing a problem that the click event on a button in sliding drawer is not working.
layout of sliding drawer
<SlidingDrawer
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/sliding"
android:handle="#+id/handle"
android:content="#+id/content"
android:clickable="true"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/handle"
android:background="#drawable/up"
/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/content"
android:clickable="true"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/proftv"
android:background="#drawable/menubuttonshape"
android:text="My Profile"
android:textColor="#e692818a"
android:layout_marginLeft="120dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/foodmenutv"
android:layout_marginLeft="120dp"
android:text="Foodmenu"
android:background="#drawable/menubuttonshape"
android:textColor="#e692818a"
/>
<Button
android:layout_marginLeft="120dp"
android:background="#drawable/menubuttonshape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/packagetv"
android:text="Packages"
android:textColor="#e692818a"
android:onClick="pkbt"/>
<Button
android:layout_marginLeft="120dp"
android:background="#drawable/menubuttonshape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/balancetv"
android:text="My Balance"
android:textColor="#e692818a"
android:onClick="balbt"/>
<Button
android:background="#drawable/menubuttonshape"
android:layout_marginLeft="120dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/reviewtv"
android:text="My Review"
android:textColor="#e692818a"
android:onClick="revbt"/>
<Button
android:layout_marginLeft="120dp"
android:background="#drawable/menubuttonshape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/logouttv"
android:text="Logout"
android:textColor="#e692818a"
android:onClick="logbt"/>
</LinearLayout>
</ScrollView>
</SlidingDrawer>
Java code:
fmenu=(Button) findViewById(R.id.foodmenutv);
handle=(Button) findViewById(R.id.handle);
sliding=(SlidingDrawer) findViewById(R.id.sliding);
sliding.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
handle.setBackgroundResource(R.drawable.down);
//sliding.unlock();
}
});
sliding.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
handle.setBackgroundResource(R.drawable.up);
}
});
fmenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"hell",Toast.LENGTH_LONG).show();
// setContentView(R.layout.activity_foodmenu);
}
});
Please try after removing the clickable="true" property in your Layout.May be that causes the problem
`
`

How to add a 'Click for more details' expandable field to AlertDialog in android

I have a AlertDialog that is shown when there is an error and it prints 'Error, could not be added/whatever", I also have the result of the exception that I'd like to parse but not shown to all users, only to those that want to click on 'details' and read the exception.
AlertDialog.Builder dialogo1 = new AlertDialog.Builder(activity);
dialogo1.setTitle("Error");
dialogo1.setMessage("Could not update movie: " + result);
dialogo1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogo1, int id) {
activity.finish();
}
});
dialogo1.show();
There is my code, pretty simple, right? I haven't been able to find this answer anyway and I'm starting to think it is just not possible
what you want is a custom alert dialog.
so for that you will have to us a layout and define your dialog.Then initialize your listeners etc like it was a normal view that you are defining
dialog_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/header_logo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="#string/app_name" />
<TextView
android:id="#+id/error"
andorid:text="Error Message"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/moreDetailsButton"
android:text="Click for more details"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/moreDetailsView"
android:text="Click for more details"
android:inputType="textPassword"
android:layout_width="match_parent"
android:visibility="gone";
android:layout_height="wrap_content"/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/moreDetailsButton"
/>
</RelativeLayout>
and in your class
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
TextView error=(TextView)dialog.findViewById(R.id.error);
Button errorButton=(Button)dialog.findViewById(R.id.moreDetailsButton);
Button okButton=(Button)dialog.findViewById(R.id.ok);
//Do whatever you want with the rest of the dialog
// initialize all the onclick listeners a usual
errorButton.setOnClickListener(new View.onClickListener(){
#Override
public void onClick(View v){
TextView errorDetails=(TextView)dialog.findViewById(R.id.moreDetailsView);
errorDetails.setText(detailedErrorMessage) //add the details to this text view;
errorDetails.setVisibility(View.VISIBLE);
}
});
dialog.show();
Sorry if there are any syntactically errors if the ctrl+c,ctrl+v of this doesnt work.My IDE decided to misbehave today.
Follow this for details on a custom alert dialog.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/error"
android:text="Error Message"
android:layout_width="match_parent"
android:layout_height="37dp"
android:layout_toEndOf="#+id/moreDetailsView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/moreDetailsView"
android:text="No existen detalles para este tipo de error."
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="wrap_content" />
<Button
android:id="#+id/moreDetailsButton"
android:text="Click for more details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/moreDetailsView"
android:layout_below="#+id/error"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="139dp"
android:layout_height="wrap_content"
android:text="OK"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/moreDetailsView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>

Android - Customizing Dialog using an Xml Layout

I am trtying to create this layout below, but I can seem to get the right
layout that I need. What I'm trying to accomplish is have a customize dailog box,
using a layout. I tried edit the xml below but if this is shown as a dialog the
defined layout is a mess. Please help me understand what I need to do for
this. THanks and looking forward. They dialog is SHOWING but the layout is not met.
This layout is what I am trying to accomplish:
Dialog:
View checkBoxView = View.inflate(this, R.layout.display_item_dialog, null);
CheckBox checkBox = (CheckBox)checkBoxView.findViewById(R.id.checkBox1);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Save to shared preferences
}
});
checkBox.setText("Search All Images");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Image Preferences");
builder.setMessage(" Select from below ")
.setView(checkBoxView)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
display_item_dialog.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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Map Category: BEVERAGES" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="14dp"
android:src="#drawable/sunny" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginTop="28dp"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageView1"
android:layout_marginRight="20dp"
android:layout_marginTop="47dp"
android:text="+" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignLeft="#+id/button1"
android:layout_marginBottom="14dp"
android:text="-" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_alignRight="#+id/button2"
android:layout_below="#+id/button1"
android:ems="10"
android:clickable="false" >
<requestFocus />
</EditText>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:layout_marginTop="33dp"
android:text="CheckBox" />
</RelativeLayout>
You can refer to this tutorials
http://www.mkyong.com/android/android-custom-dialog-example/
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
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) {
dialog.dismiss();
}
});
dialog.show();
Try this:
<?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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Map Category: BEVERAGES" />
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="200dip"
android:layout_weight="0.6"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="+" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:clickable="false" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="-" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
</ScrollView>

Categories

Resources