When switching to MODE_NIGHT_FOLLOW_SYSTEM mode, the last used mode is applied:
Application started in follow system mode: app style is light.
I'm switching to night mode: app style is dark.
I'm switchng back to follow system mode: app style is dark.
I'm switching to day mode: app style is light.
I'm switchng back to follow system mode: app style is light.
SdkVersion: 28, tested on Android 4.0.3 (emulator) and 6.0.1 (real device).
My code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView currentText = findViewById(R.id.current);
currentText.setText("Current: " + AppCompatDelegate.getDefaultNightMode());
}
private void changeNightMode(int nightMode) {
AppCompatDelegate.setDefaultNightMode(nightMode);
recreate();
}
public void onSystem(View view) {
this.changeNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
public void onNo(View view) {
this.changeNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
public void onYes(View view) {
this.changeNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
public void onAuto(View view) {
this.changeNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
Activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/system"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSystem"
android:text="System"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/current" />
<Button
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onNo"
android:text="No"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/system" />
<Button
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onYes"
android:text="Yes"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/no" />
<Button
android:id="#+id/auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onAuto"
android:text="Auto"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/yes" />
</android.support.constraint.ConstraintLayout>
and styles:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
What am I doing wrong?
This was a bug in AppCompat. They marked it fixed in 1.1.0-alpha03: https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0-alpha03
Related
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)
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>
I am pretty much following this tutorial and want to adapt the layout of the AlertDialog to my needs. Now I need to get rid of the divider between the two buttons in the ButtonBar. I changed the layout of my Button like this:
<style name="Widget.Sphinx.Button.Borderless.Small" parent="#android:style/Widget.Holo.Button.Borderless.Small">
<item name="android:textColor">#color/button_textcolor</item>
<item name="android:background">#drawable/button</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">normal</item>
<item name="android:dividerVertical">#android:color/transparent</item>
</style>
I tried adapting the ButtonBar layout as well:
<style name="Sphinx.ButtonBar.AlertDialog" parent="#android:style/Holo.ButtonBar.AlertDialog">
<item name="android:background">#android:color/transparent</item>
<item name="android:dividerHorizontal">#null</item>
<item name="android:dividerVertical">#null</item>
</style>
I tried setting to #null and #android:color/transparent without success. What is the proper way to do this?
As Hussein said, it is not possible to achieve it with Theme styling. So I created a custom AlertDialog like this:
1) Create an Activity and set it's Theme to Dialog in the AndroidManifest.xml
<activity
android:name=".BSADialogActivity"
android:label="#string/title_activity_dialog"
android:theme="#android:style/Theme.Holo.Light.Dialog" >
</activity>
2) Create custom layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Edit Topic"
android:id="#+id/textView2"
android:textColor="#color/purple"
android:background="#color/green_light"
android:gravity="center" />
<EditText
android:id="#+id/et_topic"
android:inputType="text"
android:textColor="#color/purple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="#+id/bt_cancel"
android:background="#drawable/button"
android:src="#android:drawable/ic_menu_close_clear_cancel"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="#+id/bt_ok"
android:background="#drawable/button"
android:src="#android:drawable/ic_menu_save"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
3) Start the Dialog
Intent dialogIntent = new Intent(getActivity(), BSADialogActivity.class);
dialogIntent.putExtra(BSAMainActivity.EXTRA_TOPIC, mTopic);
getActivity().startActivityForResult(dialogIntent, SAMainActivity.REQUEST_DIALOG);
4) Have a look at the result
5) Handle ImageButton events in the DialogActivity
ImageButton ibSave = (ImageButton) findViewById(R.id.bt_ok);
ibSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent();
i.putExtra(BSAMainActivity.EXTRA_TOPIC, etTopic.getText().toString());
setResult(RESULT_OK, i);
finish();
}
});
6) Handle results in MainAcitivty
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == REQUEST_DIALOG) {
if (resultCode == RESULT_OK) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(BSA_CHAT_TAG);
if (fragment instanceof BSABluetoothChatFragment) {
((BSABluetoothChatFragment) fragment).updateTopic(data.getStringExtra(EXTRA_TOPIC));
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
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>