I'm trying to show a full screen popup window from a fragment. I was starting the layout for the screen and it looked as expected in the AS screen preview, but it doesn't when it is executed in a device. Basically, it seems that the paddings are omitted. All of them, the paddings in the root of the layout, in the image view which I put to increase the clickable area, in the text input layout (added by theme).
On the other hand, the animation is not shown in some devices but in emulator is.
Here is the layout file ``:
<?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"
android:background="#color/dark_gray"
android:fitsSystemWindows="true"
android:paddingStart="#dimen/padding_start"
android:paddingTop="#dimen/padding_top"
android:paddingEnd="#dimen/padding_end"
android:paddingBottom="#dimen/padding_bottom">
<ImageView
android:id="#+id/bt_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/grid_size_x2_5"
android:src="#drawable/ic_close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_title"
style="#style/TextAppearance.Touche.H3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Product"
android:text="Product"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/bt_close"
tools:textColorHint="#color/white" />
<Button
android:id="#+id/bt_add_item"
style="#style/TextAppearance.Touche.Button.Primary"
android:layout_width="match_parent"
android:layout_height="#dimen/button_height"
android:text="#string/add_order_add__to_order"
app:layout_constraintBottom_toBottomOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/ti_quantity"
android:layout_width="#dimen/grid_size_x10"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/grid_size_x6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/bt_close">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/et_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:lines="1"
android:maxLength="50"
android:paddingStart="#dimen/padding_start"
android:paddingTop="#dimen/padding_top"
android:paddingEnd="#dimen/padding_end"
android:paddingBottom="#dimen/padding_bottom"
android:text="0" />
</com.google.android.material.textfield.TextInputLayout>
<ImageButton
android:id="#+id/bt_less"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:paddingStart="#dimen/padding_start"
android:paddingTop="#dimen/padding_top"
android:paddingEnd="#dimen/padding_end"
android:paddingBottom="#dimen/padding_bottom"
android:src="#drawable/ic_less_selector"
app:layout_constraintBottom_toBottomOf="#id/ti_quantity"
app:layout_constraintEnd_toStartOf="#id/ti_quantity"
app:layout_constraintTop_toTopOf="#id/ti_quantity" />
<ImageButton
android:id="#+id/bt_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:paddingStart="#dimen/padding_start"
android:paddingTop="#dimen/padding_top"
android:paddingEnd="#dimen/padding_end"
android:paddingBottom="#dimen/padding_bottom"
android:src="#drawable/ic_add_selector"
app:layout_constraintBottom_toBottomOf="#id/ti_quantity"
app:layout_constraintStart_toEndOf="#id/ti_quantity"
app:layout_constraintTop_toTopOf="#id/ti_quantity" />
</androidx.constraintlayout.widget.ConstraintLayout>
And here you have the kotlin code inside a fragment:
context?.let {
val customView: View = (it.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(R.layout.popup_add_item, null)
val popupWindow = PopupWindow(it)
popupWindow.contentView = customView
popupWindow.width = LinearLayout.LayoutParams.MATCH_PARENT
popupWindow.height = LinearLayout.LayoutParams.MATCH_PARENT
popupWindow.animationStyle = R.style.popup_window_animation // TODO Not working in some devices
popupWindow.elevation = 5f
popupWindow.showAtLocation(getBinding().rvDiscounts, Gravity.CENTER, 0, 0)
val btClose = customView.findViewById<ImageView>(R.id.bt_close)
val btAddToCart = customView.findViewById<Button>(R.id.bt_add_item)
val btAdd = customView.findViewById<ImageButton>(R.id.bt_add)
val btLess = customView.findViewById<ImageButton>(R.id.bt_less)
val etQuantity = customView.findViewById<TextInputEditText>(R.id.et_quantity)
btClose.setOnClickListener {
popupWindow.dismiss()
}
btAddToCart.setOnClickListener {
// TODO add to cart
popupWindow.dismiss()
}
btAdd.setOnClickListener {
val currentQuantity = etQuantity.text.toString().toInt()
etQuantity.setText(currentQuantity.inc().toString())
}
btLess.setOnClickListener {
val currentQuantity = etQuantity.text.toString().toInt()
if (currentQuantity > 0) etQuantity.setText(currentQuantity.dec().toString())
}
}
And finally the screenshots, the preview and expected first, the shown on the device second:
To the plus and minus button use a android:margin_start="8dp" and android:margin_end="8dp" respectively, to get some space between the button and the TextInputEditText box.
If you wish to center the zero within TextInputEditText then use android:gravity="center"
The #dimen/padding_start values in these might not be acceptable, so just hardcode the values using android:paddingHorizontal="8dp" and android:paddingVertical="8dp".
<style name="AppTheme.FullScreenDialog" parent="Theme.MaterialComponents.Light.Dialog">
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowBackground">#android:color/white</item>
<item name="actionMenuTextColor">#color/colorAccent</item>
</style>
Use this style while creating your Dialog in the fragment like this :
var fullScreenCameraDialog = Dialog(context, R.style.AppTheme_FullScreenDialog)
Related
I am facing a problem with the position of the error indicator of my EditText when calling editText.setError("...").
As you can see in the screenshot I am using a BottomSheetDialog with an EditText inside of it. When I display the error indicator, the text is completely out of place. It seems as if the dialog "thinks" that it is full-screen, while it is actually not.
This is my dialog layout file (phone_dialog.xml):
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:padding="#dimen/padding_layout_normal"
android:text="#string/dialog_title_edit_phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:inputType="phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvTitle"/>
<Button
android:id="#+id/btnSavePhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etPhone"/>
</android.support.constraint.ConstraintLayout>
My Activity layout file (activity_contacts.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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvContacts"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
This is how I show the dialog from my Activity:
PhoneBottomDialog dialog = new PhoneBottomDialog(Context);
dialog.show();
This is my PhoneBottomDialog class:
public class PhoneBottomDialog extends BottomSheetDialog {
public PhoneBottomDialog(Context context) {
super(context);
View view = getLayoutInflater().inflate(R.layout.phone_dialog, null);
setContentView(view);
// additional setup below this...
}
// ...
}
I am not performing any other layouting inside my custom PhoneButtomDialog. Changing the root layout of my dialog to RelativeLayout or LinearLayout as well as adding a ScrollView did not change anything. It's also not a device or specific Android version related issue as the problem occurs on all of my testing devices ranging from Android 5.0 to 7.1, it also occurs on the emulator.
Does anyone have an idea why this is happening?
You can use TextInputLayout and inside that you can define Edit Text.
I have done some modification inside your phone_dialog.xml.
phone_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="bottom"
android:background="#android:color/holo_blue_light"
android:padding="10dp"
app:behavior_hideable="true"
app:behavior_peekHeight="60dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:gravity="center"
android:padding="10dp"
android:text="dialog_title_edit_phone" />
<android.support.design.widget.TextInputLayout
android:id="#+id/inputPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextInputLayoutLabel">
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter phone"
android:imeOptions="actionDone"
android:inputType="phone"
android:maxLines="1"
android:textSize="20sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btnSavePhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/etPhone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:text="Save" />
</LinearLayout>
</FrameLayout>
Inside style.xml add this.
<style name="TextInputLayoutLabel" parent="Widget.Design.TextInputLayout">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#android:color/black</item>
<item name="android:textSize">15sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/colorPrimary</item>
<item name="colorControlNormal">#color/colorAccent</item>
<item name="colorControlActivated">#color/colorAccent</item>
</style>
PhoneBottomDialog.java
public class PhoneBottomDialog extends BottomSheetDialog {
TextInputLayout inputPhone;
EditText edtPhone;
Button btnSave;
public PhoneBottomDialog(Context context) {
super(context);
View view = getLayoutInflater().inflate(R.layout.phone_dialog, null);
setContentView(view);
// additional setup below this...
inputPhone = (TextInputLayout) view.findViewById(R.id.inputPhone);
edtPhone = (EditText) view.findViewById(R.id.etPhone);
btnSave = (Button) view.findViewById(R.id.btnSavePhone);
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!validatePhone())
return;
}
});
}
private boolean validatePhone() {
if (edtPhone.getText().toString().isEmpty()) {
inputPhone.setError("Please enter valid phone number with country code.");
requestFocus(edtPhone);
return false;
} else {
inputPhone.setErrorEnabled(false);
}
return true;
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
// ...
}
And inside Activity.
#SuppressLint("SetTextI18n")
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test24);
mContext = this;
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
PhoneBottomDialog dialog = new PhoneBottomDialog(mContext);
dialog.show();
}
Below you can see the output for this.
add compile 'com.android.support:design:24.2.0' in gradle dependencies
use TextInputLayout in xml.
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputServer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="36dp"
app:errorTextAppearance="#style/TextErrorAppearance">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
........./>
</android.support.design.widget.TextInputLayout>
From Lollipop(5.0) version android provide TextInputLayout to do this.
Use below xml and java code to show same type view.
abc.xml:
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
app:theme="#style/AppTheme">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:imeOptions="actionNext"
android:inputType="text"
android:singleLine="true"
android:textColor="#color/icons"
android:textSize="16sp" />
</android.support.design.widget.TextInputLayout>
Abc.java:
private TextInputLayout
textInputLayout_User = (TextInputLayout) findViewById(R.id.text_input_layout_user);
textInputLayout_User.setError(getString(R.string.valid_username));
Note : This is popup menu not popup window.So I request you all to read carefully.
I have implemented pop menu. It is displaying in half of the screen. I want to spread this to entire width of device. I tried to change its style by setting layout_width as match_parent but with no success.
Below is what I tried so far:
Style
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="popupMenuStyle">#style/PopupMenu</item>
</style>
<!-- Change Overflow Menu Background -->
<style name="PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">#888888</item>
<item name="android:layout_width">match_parent</item>
</style>
Below is my java code:
PopupMenu menu = new PopupMenu(getActivity(), tvnext);
for (int i = 0; i < array.size(); i++) {
menu.getMenu().add(1, i, 1, array.get(i).getAccountName());
}
menu.show();
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
setUpNextFunctionality(item.getItemId());
return false;
}
});
P.S : Please don't suggest me to use popup window. This is my last option if nothing work.
I got your question. Instead of modify something use new widget which can easily fulfill your necessary and compatible with newer version.
Google introduce new Material concept as Bottom-Sheet
To use it in android you can use git hub libraries like this.
I don't think you can do that without implementing of your own popup window similar to PopupMenu. If you will check MenuPopupHelper::createPopup:
#NonNull
private MenuPopup createPopup() {
final WindowManager windowManager = (WindowManager) mContext.getSystemService(
Context.WINDOW_SERVICE);
final Display display = windowManager.getDefaultDisplay();
final Point displaySize = new Point();
if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(displaySize);
} else if (Build.VERSION.SDK_INT >= 13) {
display.getSize(displaySize);
} else {
displaySize.set(display.getWidth(), display.getHeight());
}
final int smallestWidth = Math.min(displaySize.x, displaySize.y);
final int minSmallestWidthCascading = mContext.getResources().getDimensionPixelSize(
R.dimen.abc_cascading_menus_min_smallest_width);
final boolean enableCascadingSubmenus = smallestWidth >= minSmallestWidthCascading;
final MenuPopup popup;
if (enableCascadingSubmenus) {
popup = new CascadingMenuPopup(mContext, mAnchorView, mPopupStyleAttr,
mPopupStyleRes, mOverflowOnly);
} else {
popup = new StandardMenuPopup(mContext, mMenu, mAnchorView, mPopupStyleAttr,
mPopupStyleRes, mOverflowOnly);
}
// Assign immutable properties.
popup.addMenu(mMenu);
popup.setOnDismissListener(mInternalOnDismissListener);
// Assign mutable properties. These may be reassigned later.
popup.setAnchorView(mAnchorView);
popup.setCallback(mPresenterCallback);
popup.setForceShowIcon(mForceShowIcon);
popup.setGravity(mDropDownGravity);
return popup;
}
you will see that size of PopupMenu kind of hardcoded as per display size. So probably easy way is to check PopupMenu related source code and implement something similar, but with sizes which you'd like to have.
Trythis:
pwindow =
new PopupWindow(layoutt,LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,true);
Try to set minWidth in your style as below
<item name="android:minWidth">1000dp</item>
try this ,
inflater.inflate(R.layout.menu, menu);
or
menu.inflate(R.layout.popup_menu);
Make it translucent. I made same for pop up like this. Please check this one. May be it also work for you.
In your manifest:
<activity
android:name=".ActivityInviteFriend"
android:theme="#style/Theme.TransparentInfo">
</activity>
In style: main theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/LightappActionBarColor</item>
<item name="colorPrimaryDark">#color/appColor</item>
<item name="colorAccent">#color/btn_color</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
and your custom theme:
<color name="semiTransparentBlack">#00000000</color>
<style name="Theme.TransparentInfo" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#color/semiTransparentBlack</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
Your Activity:
package com.gc.naifizzy;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class ActivityInviteFriend extends AppCompatActivity {
Button btn_send;
Snackbar snackbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.activity_invite_friend);
btn_send = (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//snack("Invitation sent successfully...");
Toast.makeText(ActivityInviteFriend.this, "Invitation sent successfully", Toast.LENGTH_SHORT).show();
finish();
}
});
}
public void snack(String data) {
snackbar = Snackbar
.make(findViewById(android.R.id.content), data, Snackbar.LENGTH_LONG)
.setAction("Dismiss", new View.OnClickListener() {
#Override
public void onClick(View view) {
snackbar.dismiss();
}
});
snackbar.show();
}
}
and finally your xml layout :
<?xml version="1.0" encoding="utf-8"?>
<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="#502f2f2f">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="#android:color/white"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<RelativeLayout
android:id="#+id/dds"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#112f2f2f"
android:paddingTop="10dp"
android:visibility="visible">
<TextView
android:id="#+id/txt_what"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="center_vertical|left"
android:paddingLeft="10dp"
android:text="Invite people to join your tree on Naifizzy"
android:textColor="#color/appColor"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_below="#id/dds"
android:layout_marginTop="3dp"
android:alpha="0.5"
android:background="#color/appColor"
android:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/view"
android:layout_marginTop="20dp"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<TextView
android:id="#+id/txt_share"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Invite people by sharing this link "
android:textColor="#992f2f2f"
android:textSize="15sp" />
<EditText
android:id="#+id/edt_user_link"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#id/txt_share"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/layout_border"
android:editable="false"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="http://naifizzy.com/#parik_dhakan"
android:textColor="#992f2f2f"
android:textSize="15sp" />
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/edt_user_link"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:alpha="0.5"
android:background="#color/appColor" />
<TextView
android:id="#+id/txt_share2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/view1"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Send by email "
android:textColor="#992f2f2f"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="30dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1">
<LinearLayout
android:id="#+id/ed_l"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:background="#drawable/layout_border"
android:orientation="horizontal"
android:visibility="visible">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="#+id/edl_current"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:paddingTop="5dp"
android:textColorHint="#992f2f2f"
android:textSize="15sp">
<EditText
android:id="#+id/edt_mail"
style="#style/StyledTilEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:hint="Enter recipients' Email seprated by commas "
android:inputType="textEmailAddress"
android:paddingLeft="10dp"
android:textColor="#992f2f2f"
android:textColorHint="#992f2f2f"
android:textSize="15sp" />
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="#+id/img_show"
android:layout_width="30dp"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:scaleType="centerInside"
android:src="#drawable/ic_eye"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
<EditText
android:id="#+id/edt_user_link2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/txt_share2"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/layout_border"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="http://naifizzy.com/#parik_dhakan"
android:textColor="#992f2f2f"
android:textSize="15sp"
android:visibility="gone" />
<Button
android:id="#+id/btn_send"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="0.8"
android:background="#drawable/signupbg"
android:gravity="center_vertical|center_horizontal"
android:text="Send\nInvites"
android:textColor="#android:color/white"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
What style is causing the grey bar in the bottom of an AlertDialog?
Either I need to change the rest of the dialog to match the color or vice versa. I've tried modifying #android:buttonStyle and #android:buttonBarStyle. That helps but there's still some grey peeking out from the edges of the region.
Here are my current styles:
<style name="MyAlertDialog" parent="#android:style/Theme.Dialog">
<item name="#android:background">#FF000000</item>
<item name="#android:buttonBarStyle">#style/MyButtonBar</item>
</style>
<style name="MyButtonBar" parent="#android:style/ButtonBar">
<item name="#android:background">#FF000000</item>
</style>
And it looks like this:
Here is a working solution, based on Rod_Algonquin's idea of using a custom layout.
private void showCustomAlert (String message)
{
// build dialog
LayoutInflater inflater = getLayoutInflater();
View alertView = inflater.inflate (R.layout.custom_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder (this, R.style.CustomAlertDialog);
builder.setView (alertView);
final AlertDialog alert = builder.create();
// message
((TextView)alertView.findViewById (R.id.message)).setText (message);
// ok button
alertView.findViewById (R.id.cancel).setOnClickListener (new View.OnClickListener()
{
#Override public void onClick(View v) { alert.dismiss(); }
});
// show
alert.show();
}
Here is a layout that works with this code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FF000000">
<TextView android:id="#+id/alertTitle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:gravity="center"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:textAppearance="#android:style/TextAppearance.Large"
android:text="#string/custom_alert_title" />
<TextView android:id="#+id/message"
style="#android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:textAppearance="#android:style/TextAppearance.Medium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:background="#android:color/black"
android:measureWithLargestChild="true">
<Button android:id="#+id/cancel"
style="?android:attr/buttonBarButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:minHeight="50dp"
android:minWidth="100dp"
android:textSize="14sp"
android:text="#android:string/ok" />
</LinearLayout>
</LinearLayout>
Finally, here's the styles, from styles.xml, I referenced; you may not need them depending on your dialog coloring.
<style name="CustomAlertDialog" parent="#android:style/Theme.Dialog">
<item name="#android:background">#FF000000</item>
<item name="#android:buttonBarStyle">#style/CustomButtonBar</item>
</style>
<style name="CustomButtonBar" parent="#android:style/ButtonBar">
<item name="#android:background">#FF000000</item>
</style>
I have several issues with styling a spinner
I need to change the drawable of the spinner(selected view)
when the user opens the dropdown list(I tried several selectors
with different states but nothing works properly)
I can`t change the text color on the items in dropdown list (I can change
color but I need different color for the selected value) you can see in more
detail in links below.
I can`t remove the dividers from the dropdown.
I don`t want to use custom layout with button and a list to simulate the functionality of spinner(but if there is no solution I will do that).
The style.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<item name="android:background">#android:color/transparent</item>
<item name="android:windowBackground">#android:color/white</item>
<item name="android:colorBackground">#android:color/white</item>
<item name="android:dropDownSpinnerStyle">#style/SpinnerTheme</item>
</style>
<style name="SpinnerAppTheme" parent="android:Widget.Holo.Light.Spinner">
<item name="android:background">#drawable/spinner_background_holo_light</item>
<item name="android:dropDownSelector">#drawable/list_selector_holo_light</item>
<item name="android:dropDownListViewStyle">#style/mySpinnerStyle</item>
</style>
<style name="mySpinnerStyle" parent="android:Widget.ListView.DropDown">
<item name="android:divider">#null</item>
<item name="android:dividerHeight">0px</item>
</style>
<style name="Theme_Dialog_Translucent" parent="android:Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:background">#android:color/white</item>
<item name="android:windowTitleStyle">#style/dialog_title_style</item>
</style>
<style name="Theme_Dialog_Measurment_Data_Dialog" parent="android:Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:background">#color/general_background_color</item>
<item name="android:windowTitleStyle">#style/dialog_title_style</item>
</style>
<style name="dialog_title_style" parent="android:Widget.TextView">
<item name="android:textColor">#android:color/black</item>
</style>
<Spinner
android:id="#+id/res_spinner"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="60"
android:background="#drawable/ref_spinner_selector"
android:dropDownVerticalOffset="1dp"
android:dropDownWidth="60dp"
android:gravity="center"
android:popupBackground="#null"
android:spinnerMode="dropdown"/>
how the spinner needs to be look like
how the spinner looks like now
Thanks
Update:
if anyone will have problems to customize spinners
I manage to fix all the issues using ListPopupWindow
private void initPopup()
{
_lp = new ListPopupWindow(getActivity());
_lp.setAnchorView(_resTitle);
ColorDrawable cd = new ColorDrawable(getResources().getColor(android.R.color.transparent));
_lp.setBackgroundDrawable(cd);
_lp.setOnDismissListener(new OnDismissListener()
{
#Override
public void onDismiss()
{
_resTitle.setSelected(false);
}
});
_listPopupAdapter = new DataSetListAdapter(getActivity(), _resData);
_lp.setAdapter(_listPopupAdapter);
}
onclick
case R.id.res_title:
if (!_lp.isShowing())
{
_resTitle.setSelected(true);
_lp.setOnItemClickListener(this);
_lp.show();
_lp.getListView().setDivider(null);
_lp.getListView().setDividerHeight(0);
}
else
{
_resTitle.setSelected(false);
_lp.dismiss();
}
break;
Android does not permit make customization of spinner UI.So
If you want to modify your spinner UI,Then use android popup view where you can put your style UI with effect like drop-down menu
example
spinner=(EditText)findViewById(R.id.txt_Spinner);
spinner.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
p = new Point();
p.x = location[0]+(v.getHeight());
p.y = location[1]+v.getHeight();
if (p != null)
showPopup(statusActivity.this, p);
System.out.println("show popup");
}
});
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
int popupWidth = 300;
int popupHeight = 500;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 00;
int OFFSET_Y = 00;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
((TextView)layout.findViewById(R.id.textView2)).setClickable(true);
((TextView)layout.findViewById(R.id.textView3)).setClickable(true);
((TextView)layout.findViewById(R.id.textView4)).setClickable(true);
((TextView)layout.findViewById(R.id.textView5)).setClickable(true);
((TextView)layout.findViewById(R.id.textView6)).setClickable(true);
((TextView)layout.findViewById(R.id.textView7)).setClickable(true);
((TextView)layout.findViewById(R.id.textView8)).setClickable(true);
((TextView)layout.findViewById(R.id.textView9)).setClickable(true);
}
and popup.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/popup_bg"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/text_orange_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Status"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Sleeping"
android:text="Sleeping" />
<TextView
android:id="#+id/textView3"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Available"
android:text="Available" />
<TextView
android:id="#+id/textView4"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Busy"
android:text="Busy" />
<TextView
android:id="#+id/textView5"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="At work"
android:text="At work" />
<TextView
android:id="#+id/textView6"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Battery charge low"
android:text="Battery charge low" />
<TextView
android:id="#+id/textView7"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="In meeting"
android:text="In meeting" />
<TextView
android:id="#+id/textView8"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="TMS me later"
android:text="TMS me later" />
<TextView
android:id="#+id/textView9"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="At the toilet"
android:text="At the toilet" />
<EditText
android:id="#+id/textCustomize"
style="#style/text_blue_contains"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:tag="Customize"
android:text="Customize" />
I am trying to use
android:background="?android:attr/selectableItemBackground"
to get my button to do appropriate effects for each android version like rippling, etc.
But this produces a button with a grey color when I need a different color.
How can I override this default color ?
If you read the source code of Button.java then you will see that it is a subclass of TextView.java. I have made a simple workaround for the problem in question.
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:background="#1f5050">
<TextView android:layout_width="some_dp"
android:layout_height="some_dp"
android:id="#+id/button"
android:background="?android:selectableItemBackground" />
</LinearLayout>
In code:
button.setOnClickLisetener(new Onclicklistener() {
// do your stuff here
}
It would be much better if someone can extend the TextView class and make a custom Button with the feature in question.
Note: my minsdk is 14. also, the lollipop ripple effect works just fine
Just move your desired color in outer/parent level, e.g. "#color/Red" is button color:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/Red"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:text="Hello"
android:textColor="#color/White"/>
</LinearLayout>
Using this method, you can customize ripple effect color. First, you have to create an xml file in your drawable resource directory. Create a ripple_effect.xml file and add following code. I use background colour as Red.
res/drawable/ripple_effect.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#af0c0e"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#af0c0e" />
</shape>
</item>
</ripple>
And set background to above drawable resource file. Final code of xml layout activity looks like this.
res/layout/ripple_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:paddingBottom="30dp"
app:cardBackgroundColor="#e7e7e7"
android:id="#+id/cardview"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#drawable/ripple_effect">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:paddingBottom="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pending"
android:layout_weight="1"
android:layout_marginBottom="2dp"
android:textSize="25dp"
android:textColor="#d11010"
android:id="#+id/txtpending" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Assigned to:"
android:layout_marginLeft="20dp"
android:textColor="#ff000000"
android:id="#+id/txtassigned"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:layout_marginLeft="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Student Name"
android:id="#+id/txtstudentname"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Student Number"
android:id="#+id/txtstudentnumber"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Parent Name"
android:id="#+id/txtparentname"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Parent Number"
android:id="#+id/txtparentnumber"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Transfer Status"
android:id="#+id/txttransfer"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Now,ripple effect is Red colour.
if you want to do it programmatically:
fun setSelectableBgWithColor(view: View, bgColor: Int? = null) {
val bgSelectable = getDrawableResCompat(view.context, android.R.attr.selectableItemBackground)
val bg = if (bgColor == null) bgSelectable else LayerDrawable(
arrayOf(ColorDrawable(color), bgSelectable)
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.background = bg
} else {
view.setBackgroundDrawable(bg)
}
}
fun getDrawableResCompat(context: Context, #AttrRes id: Int): Drawable? {
return TypedValue()
.also { context.theme.resolveAttribute(id, it, true) }
.let {
val resId = if (it.resourceId != 0) it.resourceId else it.data
ContextCompat.getDrawable(context, resId)
}
}
Edit: this is now possible using AppCompat and backgroundTint
android:backgroundTint="#color/yourColor"
Previous solution:
I had the same problem and ended up doing this programmatically:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ColorStateList colors = new ColorStateList(new int[][]{
new int[]{android.R.attr.state_enabled},
}, new int[]{pressed});
GradientDrawable item = new GradientDrawable();
item.setCornerRadius(radius);
item.setColor(normal);
RippleDrawable ripple = new RippleDrawable(colors, item, null);
button.setBackgroundDrawable(ripple);
} else {
StateListDrawable stateListDrawable = new StateListDrawable();
GradientDrawable item;
item = new GradientDrawable();
item.setCornerRadius(radius);
item.setColor(pressed);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, item);
item = new GradientDrawable();
item.setCornerRadius(radius);
item.setColor(normal);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, item);
button.setBackgroundDrawable(stateListDrawable);
}
Instead of using ?android:attr/selectableItemBackground you can create a xml in drawable in folder with following content.
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<item
android:state_pressed="true"
android:drawable="#color/orange"/>
<item
android:state_enabled="false"
android:drawable="#color/default"/>
<item
android:drawable="#color/default"/>
</selector>
Updated: so you save this files as btn_drawable.xml in drawable folder.
Simply replace ?android:attr/selectableItemBackground with #drawable/btn_drawable.xml