AppCompatActivity has changed my AlertDialog design - java

I have change my activity to appCompat Activity.
Also i changed AlertDialog to Android.Support.V7.App.AlertDialog.
But i have lost my previous alertdialog design.
This is how it was look like.
And this is how it looks now
My theme till now was
parent="#android:style/Theme.Holo.Light.DarkActionBar">
But i was enforced to changed it cause appCompat doesn't support Holo theme.
So i change it to
parent="Theme.AppCompat.Light.DarkActionBar">
How can i make alert dialog look like previous one?

Try this:
AlertDialog dialog= new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.Theme_Holo_Dialog));

if you want to make buttons could help user for easier press, you can build a dialog with custom buttons and style the Button(e.g.style the Button on drawable folder).You can refer to the following code:
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
AlertDialog alert = dialog.Create();
alert.SetTitle("Login Information");
//alert.SetMessage("Complex Alert");
//alert.SetIcon(Resource.Drawable.alert);
LayoutInflater inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
View view = inflater.Inflate(Resource.Layout.input_layout, null);
alert.SetView(view);
EditText editText_name = view.FindViewById<EditText>(Resource.Id.et_name);
EditText editText_pwd = view.FindViewById<EditText>(Resource.Id.et_pwd);
Button button1 = view.FindViewById<Button>(Resource.Id.button1);
Button button2 = view.FindViewById<Button>(Resource.Id.button2);
button1.Click += delegate {
Toast.MakeText(this,"press button1!",ToastLength.Short).Show();
};
button2.Click += delegate {
Toast.MakeText(this, "press button2!", ToastLength.Short).Show();
};
//alert.SetButton("OK", (c, ev) =>
//{
// // Ok button click task
// string name = editText_name.Text;
// string password = editText_pwd.Text;
// Toast.MakeText(this, "name = " + name + " password= " + password, ToastLength.Long).Show();
//});
//alert.SetButton2("CANCEL", (c, ev) => { });
alert.Show();
The input_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/et_name"
android:hint="please input name"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/et_pwd"
android:password="true"
android:hint="please input password"
/>
<LinearLayout
android:padding="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:text="button1"
android:textColor="#android:color/white"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/defaultbutton"
/>
<Button
android:id="#+id/button2"
android:layout_marginLeft="20dp"
android:text="button2"
android:textColor="#android:color/white"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/defaultbutton"
/>
</LinearLayout>
define a xml(e.g. defaultbutton.xml) in folder drawable
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#1E90FF" />
<!--<stroke
android:width="2dp"
android:color="#ffffff" />-->
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
Note:
1.define a xml( defaultbutton.xml) in folder drawable
2.use like this:
android:background="#drawable/defaultbutton"
The result is:

Related

Add custom borders to Alert Dialog Builder Layout

I'm trying to add a custom layout with borders to my Alert Dialog builder, but this is the result I got:
I created a style for my dialog:
<style name="MyDialog" parent="#android:style/Theme.Dialog">
<item name="android:background">#drawable/dialog_background</item>
</style>
And this is how I defined it:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid
android:color="#00000000" />
<stroke
android:width="5dp"
android:background="#00ffffff" />
<corners android:radius="20dp" />
Then, in the layout I defined this as well:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="#dimen/_100sdp"
android:layout_height="#dimen/_80sdp"
android:gravity="center"
style="#style/MyDialog">
<CheckBox
android:id="#+id/myCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="#dimen/_10sdp"
android:text="Abilitare la palette di colori?" />
<EditText
android:id="#+id/nameEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/genderRadioGroup"
android:layout_alignParentLeft="true"
android:ems="10"
android:hint="Inserisci l'eta'">
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/genderRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/myCheckBox"
android:checkedButton="#+id/maleRadioButton">
<RadioButton
android:id="#+id/maleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Maschio" />
<RadioButton
android:id="#+id/femaleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/maleRadioButton"
android:layout_alignParentLeft="true"
android:text="Femmina" />
</RadioGroup>
1) Why the OK button is out of the shape?
2) How can I make the background transparent since I cannot use the getWindow().setBackgroundDrawableResource(android.R.color.transparent); method?
This is the Java code of the Alert:
new AlertDialog.Builder(MainActivity.this).setView(formElementsView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
int selectedId = genderRadioGroup.getCheckedRadioButtonId();
RadioButton selectedRadioButton = (RadioButton) formElementsView.findViewById(selectedId);
Intent myIntent = new Intent(MainActivity.this, PaintingActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (myCheckBox.isChecked()) myIntent.putExtra("palette", "yes");
else myIntent.putExtra("palette", "no");
myIntent.putExtra("gender", selectedRadioButton.getText());
String eta = nameEditText.getText().toString();
if (eta.length()!=0) myIntent.putExtra("eta", eta);
else myIntent.putExtra("eta", "0");
myIntent.putExtra("protocollo", "a");
myIntent.putExtra("cornice", "1" + "");
myIntent.putExtra("userLogged", userLogged);
myIntent.putExtra("first", "yes");
MainActivity.this.startActivity(myIntent);
dialog.cancel();
}
}).show().getWindow().setLayout(600, 600);
With the new Material Theme you can customize the shape of your component using the shapeAppearanceOverlay attribute.
Something like:
<!-- Alert Dialog -->
<style name="MyThemeOverlayAlertDialog" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="shapeAppearanceOverlay">#style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
</style>
<style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">8dp</item>
</style>
You can also define the style for all dialogs in you app adding this attribute in your theme:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
...
<item name="materialAlertDialogTheme">#style/MyThemeOverlayAlertDialog</item>
...
</style>
Why the OK button is out of the shape? -
Beacause you have used .setPositiveButton("OK",.. If you want an OK
button define it directly on the customized xml file.
How can I make the background transparent -
Set a background to the customized layout just like you have created
the shape and adding a color to it. Place it in the root layout.
A relatable example,
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#android:color/transparent">
<!--Transparent background for outermost layout-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/alert_background_xml"
android:layout_margin="20dp"
android:layout_centerInParent="true">
<!--alert_background_xml is your customized style -->
<!--Your views and buttons here including the OK Button-->
</RelativeLayout>
</RelativeLayout>

Adding ripple effect for View in onClick

Hello I am trying to add a ripple effect onClick method for View, but this one no working. All my items having an ID, but I don't know how to call it
Here is a code.
#Override
public void onClick(View v) {
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
int backgroundResource = typedArray.getResourceId(0, 0);
v.setBackgroundResource(backgroundResource);
switch (v.getId()) {
case ACTION_PLAY_ID:
Log.d(MainActivity.TAG, getString(R.string.detail_action_play));
v.setBackgroundResource(backgroundResource);
Intent intent = new Intent(getActivity(), PlayerActivity.class);
intent.putExtra(Video.VIDEO_TAG, videoModel);
startActivity(intent);
break;
case ACTION_BOOKMARK_ID:
if (bookmarked) {
v.setBackgroundResource(backgroundResource);
deleteFromBookmarks();
((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.star_outline));
} else {
v.setBackgroundResource(backgroundResource);
addToBookmarks();
((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.star));
}
break;
case ACTION_REMINDER_ID:
if (!isReminderSet) {
createReminderDialog((ImageView) v);
} else {
cancelReminder(liveTvProgram.getProgramId());
((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.alarm));
}
break;
}
}
For Lubomir
i have something like this but not working too:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.item_detail, container, false);
ButterKnife.bind(this, view);
View myView = view.findViewById(R.id.actions_container);
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
int backgroundResource = typedArray.getResourceId(0, 0);
myView.setBackgroundResource(backgroundResource);
loadImage();
init();
return view;
}
ImageViews(actionbuttons) is creating in java for LinearLayout actions_container
<?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">
<ImageView
android:id="#+id/header_image"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="#dimen/detail_image_1_state"
android:elevation="8dp"/>
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="#dimen/detail_bottom_margin"
android:layout_marginTop="#dimen/detail_top_margin"
android:background="#color/primary_color">
<LinearLayout
android:id="#+id/actions_container"
android:layout_width="match_parent"
android:layout_height="#dimen/detail_actions_height"
android:layout_alignParentTop="true"
android:background="#drawable/ripple_effect_image"
android:elevation="2dp"
android:orientation="horizontal"
android:paddingLeft="300dp"
android:paddingStart="300dp"/>
<LinearLayout
android:id="#+id/content_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/actions_container"
android:orientation="vertical"
android:paddingLeft="300dp"
android:paddingStart="300dp">
<TextView
android:id="#+id/title"
style="#style/TextTitleStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/subtitle"
style="#style/TextSubtitleStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/duration"
style="#style/TextSubtitleStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/season"
style="#style/TextDescriptionStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/episode"
style="#style/TextDescriptionStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/description"
style="#style/TextDescriptionStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="7"/>
</LinearLayout>
<FrameLayout
android:id="#+id/recommended_frame"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true">
<android.support.v17.leanback.widget.HorizontalGridView
android:id="#+id/recommendation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
</FrameLayout>
<TextView
android:id="#+id/recommended_text"
style="#style/TextHeaderStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/recommended_frame"
android:text="#string/related_programs"/>
</RelativeLayout>
</RelativeLayout>
Also my xml ripple effect file is like:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/dark_primary_color">
<item>
<color android:color="#color/dark_primary_color" />
</item>
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
Clickable Views
In general, ripple effect for regular buttons will work by default in API 21 and for other touchable views, it can be achieved by specifying
android:background="?android:attr/selectableItemBackground"
In code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
View myView = findViewById(R.id.myView);
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
int backgroundResource = typedArray.getResourceId(0, 0);
myView.setBackgroundResource(backgroundResource);
}
As stated in Lubomir Babev's answer, adding android:background="?android:attr/selectableItemBackground" does the trick.
However, if your view already has a background, you can use the same on the android:foreground attribute instead:
android:background="#color/anyColor"
android:foreground="?android:attr/selectableItemBackground"
android:foreground is only supported by API 23+ though.
create ripple background
view_background.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/blue" >
<item android:drawable="#drawable/view_normal">
</item>
</ripple>
view_noraml.xml //this is how you view appears in normal
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="#dimen/button_corner"/>
<solid
android:color="#android:color/transparent"/>
<stroke
android:width="0.5dp"
android:color="#color/white"/>
</shape>
now set the view_background to your view
example
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:foreground="#drawable/view_background"
android:clickable="true"
android:focusable="true"
>
<ImageView
android:id="#+id/grid_item_imageView"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_gravity="center"
android:scaleType="centerInside"
/>
</FrameLayout>
I know this is a pretty old thread but just in case the above answers didn't work, I'd recommend you to try the below code as it worked for me:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
The key thing to notice are the clickable and focusable.
The solution for this is simple easy in my side.
Here is ripple effect:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#BFB3F7">
<item android:id="#android:id/mask">
<shape android:shape="oval">
<solid android:color="#color/button_background_color" />
</shape>
</item>
</ripple>
and next on the class i need to search function
setBackground
Then i need declare a drawable item to it. something like this:
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setBackground(res.getDrawable(R.drawable.ripple_effect_for_buttons));
scrollContainer(false);
} else {
v.setBackground(null);
if (recommendation.getFocusedChild() != null) {
scrollContainer(true);
}
}
}
And YUPII its working
You can add:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:background="#drawable/ripple"/>
In Kotlin it could be easily done with an extension functions.
fun TypedArray.use(block: TypedArray.() -> Unit) {
try {
block()
} finally {
this.recycle()
}
}
fun Context.getStyledAttributes(#StyleableRes attrs: IntArray, block: TypedArray.() -> Unit) =
this.obtainStyledAttributes(attrs).use(block)
fun View.setClickableRipple() {
val attrs = intArrayOf(R.attr.selectableItemBackground)
context.getStyledAttributes(attrs) {
val backgroundResource = getResourceId(0, 0)
setBackgroundResource(backgroundResource)
}
}

Custom ListView item selection doesn't working

There is a problem with item selection in custom ListView.
There is no checkboxes, so no need in checkable=false. I've tried setup android:focusable="false" and android:focusableInTouchMode="false". Tried "Draw Selector On Top option". I can see standard touch animation, but items not highlighted.
My ListView definition (activity_main.xml):
<?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">
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
ListView creating in onCreate of main activity (MainActivity.java):
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (ListView) findViewById(R.id.listview);
view.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list = new ArrayList<String>();
currentDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
adapter = new ArrayAdapter<String>(this, R.layout.listviewitem, R.id.firstLine, list);
view.setAdapter(adapter);
updateFileList(currentDirectory);
view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View parent_view, int position, long id) {
String listItem = (String) view.getItemAtPosition(position);
Log.d("Hello", "file: " + listItem);
view.setSelected(true);
File tempFile = new File(currentDirectory + File.separator + listItem);
if (tempFile.isDirectory()) {
currentDirectory = currentDirectory + File.separator + listItem;
updateFileList(currentDirectory);
}
}
});
}
...
R.layout.listviewitem consists of ImageView and two TextViews (listviewitem.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="?android:attr/listPreferredItemHeight"
android:background="#drawable/menu_item_background_selector"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
</RelativeLayout>
Where selector menu_item_background_selector (menu_item_background_selector.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true"></item>
<solid android:color="#bfced4"></solid>
</selector>
the selector is wrong. The item should wrap the state and the color. And you should have a default item. E.g.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#bfced4" android:state_activated="true" />
<item android:drawable="#color/default_color" />
</selector>
Android will check the state from the top. So if android:state_activated is true, the corresponding color will be picked up. If it is false, it checks the next on the selector, until it reach the last one

Popup With Text Only onOptionsItemSelected

I have a tab "About" in my onOptionsItemSelected dropdown I want popup box to appear with a few lines of text info about the app when you click "About"
What is the best way to do this?
<?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"
android:padding="20dip">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="#string/app_descrip"
android:textColor="?android:attr/textColorPrimaryInverse" />
<TextView
android:id="#+id/about_credits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dip"
android:textSize="16sp"
android:text="#string/app_credits"
android:autoLink="web"
android:textColor="?android:attr/textColorPrimaryInverse" />
</LinearLayout>
Inflate the layout
protected void about() {
View messageView = getLayoutInflater().inflate(R.layout.about, null, false);
TextView textView = (TextView) messageView.findViewById(R.id.about_credits);
int defaultColor = textView.getTextColors().getDefaultColor();
textView.setTextColor(defaultColor);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.app_icon);
builder.setTitle(R.string.app_name);
builder.setView(messageView);
builder.create();
builder.show();
}
Call it on menu click

How to change the background of a android Dialog Box

How can I change the background color programatically of a dialog box?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage(R.string.tnc)
//.setTitle(R.string.tnc_title)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d(TAG, "OK Button Clicked");
dialog.dismiss();
finish();
}//end onClick
});
AlertDialog dialog = builder.create();
dialog.show();
This is the best way to do it !
Firstly make a dialog box programmatically .
public void onClick(View v) {
dialog1 = new Dialog(Page.this,R.style.myBackgroundStyle);
Window window = dialog1.getWindow();
window.setGravity(Gravity.CENTER);
window.setLayout(WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT);
window.setBackgroundDrawable(new ColorDrawable());
dialog1.setTitle(null);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog1.setContentView(R.layout.custom_dialog);
dialog1.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog1.setCanceledOnTouchOutside(true);
description.setText(DESCRIPTION);
heading_name.setText(PRODUCT_NAME);
Button abc = (Button)dialog1.findViewById(R.id.abc);
abc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button xyz = (Button) dialog1.findViewById(R.id.xyz);
retailer_btn.setOnClickListener(new View.OnClickListener() {
dialog1.dismiss();
}
});
dialog1.show();
}
});
and now create a new xml file and set this code in your xml (Dialog Layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/description_layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="45dp"
android:layout_gravity="top|right"
android:gravity="right|top"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/desription_layout2"
android:layout_width="160dp"
android:layout_height="90dp"
android:layout_marginRight="2dp"
android:padding="2dp"
android:background="#drawable/dropdown_white_background"
android:orientation="vertical" >
<Button
android:id="#+id/abc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:background="#drawable/curve_1"
android:text="abc"
android:textSize="18dp"
android:textColor="#000000"/>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:background="#ffffff" />
<Button
android:id="#+id/xyz"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:background="#drawable/curve_1"
android:text="xyz"
android:textSize="18dp"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
and if you want to do additional stuff with your dialog such as rounding the corners of the dialog of the button then create a new xml in the drawable and the add this as the background accordingly.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#ffffff" />
<gradient android:angle="-90" android:endColor="#ccffff" android:startColor="#ccffff" />
</shape>
</item>
<item android:state_focused="true"><shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#ffffff" />
<solid android:color="#66ffff" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#66ffff" />
<gradient android:angle="-90" android:endColor="#66ffff" android:startColor="#66ffff" />
</shape>
</item>
</selector>
Hope this helps :)
In that case you require to make a custom dialog box.
Refer this tutorial here which illustrates that well.
http://www.codeproject.com/Tips/659766/Android-Custom-DialogBox
Hop it helps.!!
Create a custom layout and set it as the layout for your dialog box.
You can use this code:
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_box);

Categories

Resources