Remove unnecessary margins from a custom dialog - android

I have a problem that I am designing a custom dialog for this. I am creating a xml for this as Framelayout is the root layout, and another framelayout with a gray background image is used for the contents, in which I have added a textview and two buttons Ok and Cancel and use all of this through dialog.setContentView(desired Xml Resource);
But when I generate that particular dialog then it shows extra spaces from each side, or we can say that extra margins are there but I don't know how it will removed? Please review the image attached with this question and suggest me the right solution.
Xml Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="wrap_content">
<FrameLayout android:id="#+id/rel"
android:layout_gravity="center_vertical" android:background="#drawable/dialog_box_bg" android:layout_width="wrap_content" android:layout_height="189dp">
<TextView android:id="#+id/tv_LogoutDialog_Text"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#424242"
android:text="Are you sure want to logout?" android:textSize="20dip" android:layout_gravity="center_vertical|center_horizontal"></TextView>
<Button android:id="#+id/btn_LogoutDialog_Cancel" android:background="#drawable/dialog_cancel_btn"
android:layout_marginLeft="20dip" android:layout_width="120dip" android:layout_height="42dip" android:layout_gravity="bottom|left" android:layout_marginBottom="15dip"></Button>
<Button android:id="#+id/btn_LogoutDialog_Ok"
android:background="#drawable/dialog_ok_btn_hover"
android:layout_width="120dip"
android:layout_height="42dip" android:layout_marginLeft="180dip" android:layout_gravity="bottom|right" android:layout_marginBottom="15dip" android:layout_marginRight="20dip"></Button>
</FrameLayout>
</FrameLayout>
Code:
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
dialog = new Dialog(HomeScreenActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.logoutdialog);
btn_cancel = (Button)dialog.findViewById(R.id.btn_LogoutDialog_Cancel);
btn_ok = (Button)dialog.findViewById(R.id.btn_LogoutDialog_Ok);
btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dismissDialog(0);
}
});
btn_logout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(HomeScreenActivity.this,LoginScreen.class);
startActivity(intent);
}
});
return dialog;
}
return null;
}
Thanks in advance.

Don't use
dialog.setContentView(R.layout.logoutdialog);
use
LayoutInflater class to set Dialog content view
Here is link (Check) you can get the idea May this helps you.

Change it by adding a parameter false like in the code below.
dialog.customView(R.layout.dialog_blueprint, false)
The second argument (false) is wrapTheDialogBoxInScrollView. If the content in the dialog box is small and does not require a ScrollView set it to false.

Related

How to custom ImageButton's image?

I allow the App's User to change the ImageButton's pictures from the drawable folders?
It's possible?
I want associate this action after onLongClickListener,
I put in the Drawable folder about 3 or 4 pictures(png) and the User can choose one for its ImageButton.
Yes, you can. On onLongClickListener click, you can pop up the option and then put a switch statement and put the following for each of the cases:
aButton.setImageResource(R.drawable.image2);
Here is the more detailed answer:
Put the following at the bottom of the layout (just before the last closing layout tag)
<FrameLayout
android:id="#+id/imagebuttonselectorlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:background="#android:color/black" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imgButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image1" />
<ImageButton
android:id="#+id/imgButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image2" />
</LinearLayout>
</FrameLayout>
Then, in the java class file, add the following lines:
FrameLayout mFrameLayout;
ImageButton mImageButton1;
ImageButton mImageButton2;
mFrameLayout = (FrameLayout)findViewById(R.id.imagebuttonselectorlayout);
mImageButton1 = (ImageButton)findViewById(R.id.imgButton1);
mImageButton2 = (ImageButton)findViewById(R.id.imgButton2);
For the onLongClick of the main image button
mImageButton1.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mFrameLayout.setVisibility(View.VISIBLE);
return true;
}
});
Add the following lines in the same file to complete the functionality:
mImageButton1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mainImageButton.setImageResource(R.drawable.image1);
mFrameLayout.setVisibility(View.GONE);
}
});
mImageButton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mainImageButton.setImageResource(R.drawable.image2);
mFrameLayout.setVisibility(View.GONE);
}
});

Android: many equal Buttons, onclick doesn't work for certain

I have seven equal Buttons in LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:weightSum="7" >
<Button
android:id="#+id/btn_mon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#color/white"
android:text="0" />
<Button
android:id="#+id/btn_tus"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_wen"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_thu"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_fri"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_sat"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_sun"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
</LinearLayout>
Their OnClickListener is also equal (and initializing too):
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.calendar_row, null);
}
final Button btn_mon = (Button)convertView.findViewById(R.id.btn_mon);
final Button btn_tus = (Button)convertView.findViewById(R.id.btn_tus);
final Button btn_wen = (Button)convertView.findViewById(R.id.btn_wen);
final Button btn_thu = (Button)convertView.findViewById(R.id.btn_thu);
final Button btn_fri = (Button)convertView.findViewById(R.id.btn_fri);
final Button btn_sat = (Button)convertView.findViewById(R.id.btn_sat);
final Button btn_sun = (Button)convertView.findViewById(R.id.btn_sun);
btn_mon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_mon, weekdays.get(0).data); }});
btn_tus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_tus, weekdays.get(1).data); }});
btn_wen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_wen, weekdays.get(2).data); }});
btn_thu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_thu, weekdays.get(3).data); }});
btn_fri.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_fri, weekdays.get(4).data); }});
btn_sat.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_sat, weekdays.get(5).data); }});
btn_sun.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_sun, weekdays.get(6).data); }});
onCalBtnClick method:
private void onCalBtnClick(Button btn, int day){
Log.d("debug", String.valueOf(day));
btn.setTextColor(mContext.getResources().getColor(R.color.orange));
//selectedYear, month are global
Intent intent = new Intent();
intent.putExtra("year", selectedYear);
intent.putExtra("month", month);
intent.putExtra("day", day);
setResult(RESULT_OK, intent);
finish();
}
However, if I put Log.d into onCalBtnClick method (it is called from each clicklistener), only middle three buttons work. Two buttons from left side (btn_mon, btn_tus) and two buttons from right side (btn_sat, btn_sun) don't react on user click. Middle buttons work fine.
This is similar question Android LinearLayout make button doesn't work but my layout file corresponds to pattern in the answer there and buttons don't work nevertheless
UPDATE
When I removed fixed button height and width in layout file (from 50dp to wrap_content), all buttons started to work!
However, now it doesn't look as needed. There's space needed between text's on buttons.
And main question: WHY?
If you using weights in your layout, you are telling that it should be filled with some objects with some proportions. It just opposite to "wrap_content". With weights outer layout defines size of inner views, while "wrap_content" means that outer layout size is defined by inner views.
Please decide what approach is better in that case - removing weights or fixed inner views sizes.
set your Linear Layout's Width and Height to FILL_PARENT .it will work for sure.
Try to declare all your setOnClickListener for buttons outside of your adapter class and after initialization of weekdays array.
That will solve your problem.
Or try to set condition like this for all buttons :
if (btn_mon != null && position < weekdays.size()) {
btn_mon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_mon, weekdays.get(0).data); }});
}

How to add layouts to dialog

I have an idea in mind but im not sure about how to implement it
first i have a dialog
final Dialog dialog = new Dialog(mContext);
i also have a layout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textViewDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textViewWhen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
what i want is to add this layout in the dialog, i may also want to add more of the same layout right under it inside that dialog
how can i do that?
for example how can i add two of this layout in one dialog?
something like
Dialog Title
Large Text
Small Text
Medium Text
Large Text
Small Text
Medium Text
Something like this:
LayoutInflater li = LayoutInflater.from(SomeActivity.this);
someLayout = (LinearLayout)li.inflate(R.layout.some_layout, null);
alert = new AlertDialog.Builder(SettingsActivity.this);
alert.setView(someLayout);
This is an example from my application:
public class ConfirmDialog extends DialogFragment {
public static String TAG = "Confirm Dialog";
public interface ConfirmDialogCompliant {
public void doOkConfirmClick();
public void doCancelConfirmClick();
}
private ConfirmDialogCompliant caller;
private String message;
public ConfirmDialog(ConfirmDialogCompliant caller, String message){
super();
this.caller = caller;
this.message = message;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.confirm_dialog, container, false);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
((TextView) view.findViewById(R.id.textview_confirm)).setText(message);
((Button) view.findViewById(R.id.ok_confirm_button)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
caller.doOkConfirmClick();
}
});
((Button) view.findViewById(R.id.cancel_confirm_button)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
caller.doCancelConfirmClick();
}
});
return view;
}
}
where the inflated layout is confirm_dialog.xml.
You inflate your layout in the onCreateView method.
In this case I used DialogFragment (which I suggest you to use...see the support library so that you don't have to worry about your target SDK) but the same applies to Dialog.
Hope it helps you!
You can check this documentation page which explain how to add a custom layout on dialog
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
The key is the setContentView method:
dialog.setContentView(R.layout.custom_dialog);
Check out this one :
how to get customized alert dialog , like the one shown in image?
Refer the answer which I've given (Aamir Shah)
Use a DialogFragment, which allows you to, just like any other Fragment, completely customize the layout. It is available in the v4 support library.
http://developer.android.com/reference/android/app/DialogFragment.html
http://developer.android.com/tools/extras/support-library.html

Dialog box not renderd properly in android

i have a dialog box, but some how there is an unknown background image. How can i remove that image. Please guide me.
You have to extends Dialog Class, build your xml File for your dialog something like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Do you Want to bookmark?"
android:gravity="center"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
<Button
android:id="#+id/button_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes" />
</LinearLayout>
</LinearLayout>
of course you can make some customization
for your custom dialog class you can do like this
public class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;
public CustomizeDialog(Context context) {
super(context);
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
yesButton = (Button) findViewById(R.id.button_yes);
yesButton.setOnClickListener(this);
noButton = (Button) findViewById(R.id.button_no);
noButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button_yes:
dismiss();
//doSomething
break;
case R.id.button_no:
dismiss();
//doSomethingElse
break;
}
}
Hope this will help you
you have to post your code to figure out why these background box appear for you but acting like i mention should resolve the problem for you
That is probably happening because you use the standard AlertDialog and set a content view + no title(although you don't set a title the space for it will remain in the dialog).
Extend the Dialog class instead and build your dialog like you want. Also if you want to use your own background for the Dialog then implement a theme that extends Theme.Dialog and override:
<item name="android:windowBackground">#android:drawable/panel_background</item>
with your own drawable.

Determinate ProgressDialog in Android

I want to have a ProgressDialog with determinate progress bar instead of default spinner. It seems to be easy: ProgressDialog has method setIndeterminate, and method show accepts a boolean to indicate indeterminateness. But these way don't work for me! The dialog is still indeterminate with a spinner. How to change the behaviour?
You need call:
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Check out the official dev guide Showing a progress bar with the example source code attached to that section.
Oh Sorry.. Try this
1) Create a layout file with below code
<?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="wrap_content" android:id="#+id/RelativeLayout_DownloadProgress" android:paddingLeft="20dp" android:paddingBottom="20dp" android:paddingRight="10dp" android:paddingTop="20dp">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/downloading_video_tv" android:text="#string/downloading_video" android:textStyle="bold" android:textColor="#color/COLOR_WHITE"></TextView>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/download_progress_tv" android:text="#string/zero_percent" android:layout_below="#+id/downloading_video_tv" android:textColor="#color/COLOR_WHITE"></TextView>
<Button
android:id="#+id/download_cancel_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/downloading_video_tv"
android:text="#string/Cancel"
android:textColor="#color/COLOR_CANCEL_BTN" android:layout_marginLeft="5dp">
</Button>
<ProgressBar
android:id="#+id/download_progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="13dp"
android:layout_below="#id/download_progress_tv"
android:layout_toLeftOf="#id/download_cancel_btn"
android:indeterminateOnly="false"
android:max="100"
android:progressDrawable="#drawable/dwnld_progress" >
</ProgressBar>
</RelativeLayout>
2) In your code do the following
i) in oncreate() method add the following lines..
//Download progress layout inflate
RelativeLayout relativelayout_DownloadProgress = (RelativeLayout)_inflater.inflate(R.layout.downloadprogress, null);
TextView downloadProgress_PercentageTV = (TextView)relativelayout_DownloadProgress.findViewById(R.id.download_progress_tv);
Button downloadProgress_CancelBtn = (Button)relativelayout_DownloadProgress.findViewById(R.id.download_cancel_btn);
ProgressBar download_progressBar = (ProgressBar)relativelayout_DownloadProgress.findViewById(R.id.download_progressbar);
downloadProgress_CancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dismissDialog(0);
}
});
ii) //Creating Dialogs i.e., below onCreate() method
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(_activity)
.setView(relativelayout_DownloadProgress)
.create();
}//switch end
return null;
}//onCreateDialog()
iii) on any click event add the following lines.
showDialog(0);download_progressBar.setProgress(0);
Sorry i know its too long code but its working code in application...Try it might need modifications like variables must be declared in class scope.
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Registering. Please wait...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
Try this code should work fine...

Categories

Resources