I have an app that uses two buttons and a number picker. Everything works fine when running an emulator with SDK 22 but when running SDK 23 the dialog is significantly smaller and does not include the leftmost button.
Here is the xml for the dialog:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberPicker1"
android:layout_marginLeft="20dp"
android:layout_marginTop="98dp"
android:layout_toRightOf="#+id/numberPicker1"
android:text="Cancel" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="#+id/numberPicker1"
android:text="Set" />
</RelativeLayout>
Here is the code I use to initialize the dialog:
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("Delay");
d.setContentView(R.layout.dialog);
Button b1 = (Button) d.findViewById(R.id.button1);
Button b2 = (Button) d.findViewById(R.id.button2);
final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);
np.setMaxValue(100);
np.setMinValue(0);
np.setWrapSelectorWheel(false);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeDelay(np.getValue(), user);
d.dismiss();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
d.show();
Try the layout below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:layout_marginBottom="98dp"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberPicker1"
android:layout_alignParentRight="true"
android:text="Cancel" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberPicker1"
android:layout_alignParentLeft="true"
android:text="Set" />
</RelativeLayout>
Some problems, are:
android:layout_alignBottom will cause the RelativeLayout to expand until the bottom of the screen. This is not good for dialog. You can find details here
You defined some huge margins. Your dialog in landscape mode is not displayed properly... Some contents become hidden.
But I'm not sure why it was working fine on API 22 and not on API 23
Related
I am inflating a layout inside a dialog, which consist of a scrollview but the layout is not scrolling. I have referred so many questions from stackoverflow but then also its not working. My layout.xml is given below:
<?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="match_parent"
android:layout_margin="5dp"
android:background="#color/White"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:text="Update available"
android:textColor="#000000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:text="#string/upgrade_msg"
android:textColor="#000000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_margin="3dp"
android:layout_weight="0.02"
android:src="#drawable/updateimg2" />
<Button
android:id="#+id/btn_upgrade"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_forget_pwd"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/White"
android:gravity="center"
android:text="Upgrade Now"
android:textColor="#color/Black" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="#drawable/line_y_h" />
<Button
android:id="#+id/btn_remind"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_forget_pwd"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/White"
android:gravity="center"
android:text="Remind Me Later"
android:textColor="#color/Blue" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="#drawable/line_y_h" />
</LinearLayout>
</ScrollView>
Dialog code
public void show_Alert_version_custom(String str, final String url,
final String exitStatus)// use for
{
final Dialog dialog = new Dialog(Splash.this);
dialog.setContentView(R.layout.version_update);
dialog.setTitle(getString(R.string.alert_title));
Button dialogButton2 = (Button) dialog.findViewById(R.id.btn_remind);
dialogButton = (Button) dialog.findViewById(R.id.btn_upgrade);
ImageView viewLine = (ImageView) dialog.findViewById(R.id.imageView2);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {}
});
// if button is clicked, close the custom dialog
dialogButton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {}
});
dialog.show();
}
android:fillViewport="true" in ScrollView solved my problem. Now its scrolling even inside dialog.
You can use simply below code on ScollView. it's working fine.
<ScrollView>
<LinearLayout android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
</LinearLayout>
</ScrollView>
In my android app i need to use alertdialog programmatically with two images in first row and another two images in second row. i seen some example like,
CharSequence colors[] = new CharSequence[] {"red", "green", "blue", "black"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(colors, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// the user clicked on colors[which]
}
});
builder.show();
in the above example have only one option for every row like red in first row, green in next row, blue in third row like that. I need to implement 2 option in every row, pls guide me,,
Dialog mDialog;
mDialog=new Dialog(SplashScreen.this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.yourcustomlayout);
TextView ok,cancel;
ok=(TextView) mDialog.findViewById(R.id.dialogyes);
cancel=(TextView) mDialog.findViewById(R.id.dialogno);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mDialog.cancel();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
mDialog.cancel();
}
});
mDialog.show();
yourcustomlayout.xml -
<?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="wrap_content"
android:background="#android:color/white" >
<TextView
android:id="#+id/dialogtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Internet Connection"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textColor="#android:color/black" />
<View
android:id="#+id/view1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/dialogtitle"
android:layout_marginTop="25dp"
android:background="#C83437" />
<TextView
android:id="#+id/dialogMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/view1"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="You are not connected to internet.\nDo you want to on connection?"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textColor="#android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="150dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:id="#+id/dialogyes"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginBottom="10dp"
android:layout_weight="0.5"
android:gravity="center"
android:text="YES"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/dialogno"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginBottom="10dp"
android:layout_weight="0.5"
android:gravity="center"
android:text="NO"
android:textColor="#android:color/black" />
</LinearLayout>
</RelativeLayout>
in mDialog.setContentView() add your custom layout.
I have a problem with a custom layout.
When running it on emulators, the popup adjusts itself to the size of my custom layout, yet when I run it on my Atrix, this happens:
As you can see, the layout is not centered.
Now, this is my custom layout:
<TextView
android:id="#+id/txtVersus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:padding="10dp"
android:text="#string/popup_unlock"
android:textColor="#FFFFFFFF"
android:textSize="20sp"
android:visibility="visible" />
<ImageView
android:id="#+id/imgPopup"
android:layout_width="#dimen/imagen_config_width"
android:layout_height="#dimen/imagen_config_height"
android:layout_gravity="center"
android:background="#drawable/char_cersei"/>
<net.rahl.rahlutils.CustomFontTextView
android:id="#+id/txtPopupName"
style="#style/ConfigText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="3dp"/>
<net.rahl.rahlutils.CustomFontTextView
android:id="#+id/txtPopupCasa"
style="#style/ConfigText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="3dp"/>
<Button
android:id="#+id/btnCerrarPopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="#string/ok" />
And this is my part of the code where I set and show it:
final Dialog dialog = new Dialog(con);
dialog.setTitle("Felicidades!");
dialog.setContentView(R.layout.unlockdialog);
((ImageView) dialog.findViewById(R.id.imgPopup)).setBackgroundResource(con.getResources().getIdentifier("char_" + name.toLowerCase(), "drawable", con.getPackageName()));
((TextView) dialog.findViewById(R.id.txtPopupName)).setText(name);
((TextView) dialog.findViewById(R.id.txtPopupCasa)).setText(casa);
((Button) dialog.findViewById(R.id.btnCerrarPopup)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.cancel();
}
});
dialog.setCanceledOnTouchOutside(false);
dialog.show();
Do you know how to fix this?
Hi
I am new in android.
I have many Buttons in view in android and i want to show them part by part by clicking another button.
Set button visibility to GONE (button will be completely "removed" -- the buttons space will be available for another widgets) or INVISIBLE (button will became "transparent" -- its space will not be available for another widgets):
View b = findViewById(R.id.button);
b.setVisibility(View.GONE);
or in xml:
<Button ... android:visibility="gone"/>
Ref By: LINK
use android:visibility="visible" or android:visibility="gone" or android:visibility="invisible"
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="91dp"
android:layout_toLeftOf="#+id/textView1"
android:text="Button"
android:visibility="visible" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignLeft="#+id/textView1"
android:text="Button"
android:visibility="invisible" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:text="Button"
android:visibility="invisible" />
let say you have 2 buttons : in xml write this:Make button 2 invisible like below
<Button android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button 2"
android:layout_below="#+id/btn1"
android:visibility="invisible"/>
In your oncreate method write this: when you click on button1 , button2 will appear.
Button btn1 = (Button)findViewById(R.id.btn1);
final Button btn2 = (Button)findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn2.setVisibility(View.VISIBLE);
}
});
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="91dp"
android:layout_toLeftOf="#+id/textView1"
android:text="Button"
android:visibility="gone" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignLeft="#+id/textView1"
android:text="Button"
android:visibility="gone" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:text="Button"
android:visibility="gone" />
First make the above xml layout having all your buttons and set them to GONE because the buttons will not occupy space in the layout by default so saves you space and time at initialization.
Now in your code on click event just set the visibility of these buttons to VISIBLE.
Steps for changing the visibility
HashMap map=new HashMap();
map.put(R.id.button1,new Integer[]{R.id.button2,R.id.button3,R.id.button4});
public void onClick(View v){
Integer[] buttonsToShow=map.get(R.id.v.getId());
if(buttonsToShow!=null)
for(int button:buttonsToShow){
findViewById(button).setVisibility(View.VISIBLE);
}
}
I wrote following code and solved my problem. maybe it might be useful to anyone
next = (Button)findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(i<movie_list.length-3)
{
movie_list[i].setVisibility(View.GONE);
movie_list[i+3].setVisibility(View.VISIBLE);
i++;
}else{
Toast.makeText(getApplicationContext(), "That is the end of the buttons ", Toast.LENGTH_SHORT).show();
}
}
});
I need to create a dialog that contains a matrix of simple square ImageButtons (like icons), in unknown number, and a Cancel button. The dialog should be inflatable programmatically and scrollable.
I really am confused: should I create a custom layout and apply it to AlertDialog? And how could I intercept the clicks? Should I use PopUpWiew? Should I make it as Activity or not?
And if I create a new Activity... should I use a Runnable?
Please don't provide me a complete source, I just need to understand what is the correct direction to go for this need.
Thank you in advance.
You can use a Dialog and set your layout file to it. A dialog is something like a mini activity which can be called onto an actual activity and dismissed at will.
Let this be the custom layout file - customLayout.xml
<RelativeLayout 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:background="#drawable/bg"
tools:context=".AutoMode" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_above="#+id/relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/blue_gradient"
android:layout_alignRight="#+id/relativeLayout1"
android:layout_alignTop="#+id/relativeLayout1"/>
<Button
android:id="#+id/button2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/black_gradient"
android:layout_alignTop="#+id/relativeLayout1"
android:layout_toRightOf="#+id/button1"/>
<Button
android:id="#+id/button3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/button2"
android:background="#drawable/red_gradient" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<Button
android:id="#+id/button4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/yellow_gradient"
android:layout_alignRight="#+id/relativeLayout2"
android:layout_alignTop="#+id/relativeLayout2" />
<Button
android:id="#+id/button5"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/metallic_gradient"
android:layout_alignTop="#+id/relativeLayout2"
android:layout_toRightOf="#+id/button4" />
<Button
android:id="#+id/button6"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/purple_gradient"
android:layout_alignTop="#+id/relativeLayout2"
android:layout_toRightOf="#+id/button5"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_below="#+id/relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/button7"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/green_gradient"
android:layout_alignRight="#+id/relativeLayout3"
android:layout_alignTop="#+id/relativeLayout3" />
<Button
android:id="#+id/button8"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/orange_gradient"
android:layout_alignTop="#+id/relativeLayout3"
android:layout_toRightOf="#+id/button7" />
<Button
android:id="#+id/button9"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/violet_gradient"
android:layout_alignTop="#+id/relativeLayout3"
android:layout_toRightOf="#+id/button8" />
</RelativeLayout>
Here's how you can set this layout to a dialog box in the activity where you want to call it:
final Dialog customDialog = new Dialog(this);
customDialog.setTitle("Matrix");
disclaimer.setContentView(R.layout.customLayout);
Button b1 = (Button) customDialog.findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do whatever you want when button1 is clicked
}
});
.
.//same for other buttons
.
Button b9 = (Button) customDialog.findViewById(R.id.button9)
b9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do whatever you want when button9 is clicked
}
});
Button close = (Button) customDialog.findViewById(R.id.dismiss);
customDialog.show();
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
disclaimer.dismiss();
}
});