After create and testing my custom dialog I've noticed that my button does not show any visual sign of change (such as the ripple effect or highlighting) when clicked. Does anyone know what I'm doing wrong and how to resolve this issue?
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<Button
android:id="#+id/world"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:background="#color/green"
android:textColor="#color/white"
android:text="#string/world"
android:textAllCaps="false"
style="#android:style/TextAppearance.Medium"/>
</LinearLayout>
Java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_other_lines) {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_hello_world);
dialog.setTitle("Dialog");
Button world = (Button) dialog.findViewById(R.id.world);
world.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
return super.onOptionsItemSelected(item);
}
You override the default onClick effect by putting a background color.
android:background="#color/green"
You can do it by creating a custom background xml file on drawable, like this.
custom_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/Pressed_Color" />
<item android:state_activated="false" android:drawable="#color/green"/>
</selector>
then you call it on the button styling as like this:
android:background="#drawable/custom_background"
when you change your default appearance, you should handle different state if you want like this
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_sel" android:state_selected="true" />
<item android:drawable="#drawable/button_sel" android:state_pressed="true" />
<item android:drawable="#drawable/button_unsel" />
</selector>
save this as xml drawable and add as android:background
Related
I have a custom BottomSheetDialog and for its layout i created drawable which has top corners rounded rectangle. BottomSheetDialog layout background is a Linearlayout and i applied drawable to the layout.Top corners of the bottom sheet rounding correctly but there is a another layout bottom of the Linear Layout which is not rounded (square and white see below picture) and it is not in the layout.xml file. its like rounded rectangle on top of a square.I couldn't get rid of that square.
Below is my custom bottom sheet sample
public abstract class EmployeeBottomSheetDialog extends BottomSheetDialog {
private Context context;
private Activity activity;
private RecyclerView employeeRecyclerView;
private EditText searchEditText;
private DataBase dataBase;
private ArrayList<Employe> employeeList = new ArrayList<>();
private ArrayList<Employe> employeeSelectedList = new ArrayList<>();
private SelectEmployeeAdapter selectEmployeeAdapter;
private ImageButton closeSearchImageButton;
public EmployeeBottomSheetDialog(#NonNull Context context, List<Employe> selectedEmployeeList) {
super(context,R.style.BottomSheetDialogStyle);
this.context = context;
this.activity = (Activity) context;
if(!selectedEmployeeList.isEmpty()){
employeeSelectedList.clear();
employeeSelectedList.addAll(selectedEmployeeList);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.employee_bottom_sheet_dialog);
}
}
Below is my style
<style name="BottomSheetDialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="shapeAppearanceOverlay">#style/ShapeAppearanceOverlay</item>
<item name="backgroundTint">#android:color/transparent</item>
<item name="background">#android:color/transparent</item>
</style>
below is my bottom sheet layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/transparent"
app:layout_behavior="#string/bottom_sheet_behavior">
<LinearLayout
android:id="#+id/dialog_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/bottom_sheet_background"
android:padding="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ic_baseline_arrow_drop_down_24" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/between_two_views">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/search_wrapper"
style="#style/textInputHint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/search_employees">
<EditText
android:id="#+id/search"
style="#style/textInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<ImageButton
android:id="#+id/close_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:background="#null"
android:src="#drawable/ic_baseline_close_24"
android:text="Button" />
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/employee_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/between_two_views"/>
</LinearLayout>
</LinearLayout>
Below is my rounded corner drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:topLeftRadius="20dp" android:topRightRadius="20dp"/>
<solid android:color="#color/red" />
</shape>
To solve this, you need a transparent background color in your dialog style (BottomSheetDialogStyle):
<item name="android:colorBackground">#android:color/transparent</item>
For those looking for an easier way to implement rounded corners in all states, even EXPANDED state, then just define your BottomSheetDialog's background attribute to a shape drawable.
Here is the shape drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/sheet_gray"/>
<corners android:radius="24dp" />
</shape>
And this is my BottomSheetDialog :
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bs_bottomsheet"
android:background="#drawable/bottom_sheet_b"
app:behavior_hideable="false"
app:behavior_peekHeight="40dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
This will solve all the headaches about:
Setting a custom style and whatnot...
Wondering about how to round the corners in EXPANDED state
etc...
I found out the answer after hours of searching. i only needed to use below style
<style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="bottomSheetStyle">#style/AppModalStyle</item>
</style>
<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">#drawable/bottom_sheet_background</item>
</style>
<style name="BottomSheetStyle" parent="Theme.Design.BottomSheetDialog">
<item name="bottomSheetStyle">#style/Model</item>
</style>
<style name="Model" parent="Widget.Design.BottomNavigationView">
<item name="background">#drawable/bottom_sheet_layout</item>
</style>
I wish to create a simple dialog for the user with 2 buttons as follows:
Dialog Layout (dialog_layout.xml):
<?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:layout_margin="8dp"
android:orientation="vertical">
<Button
android:id="#+id/btn_select_choice_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Choice"
android:theme="#style/secondary_button_normal" />
<Button
android:id="#+id/btn_select_choice_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Choice"
android:theme="#style/secondary_button_normal" />
</LinearLayout>
secondary_button_normal:
<style name="secondary_button_normal" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/button_secondary_normal_background</item>
<item name="android:textColor">#color/button_secondary_normal_text</item>
<item name="android:textSize">#dimen/button_textSize</item>
<item name="android:padding">#dimen/button_padding</item>
</style>
Activity's onCreate:
final Dialog selection = new Dialog(this);
selection.setContentView(R.layout.dialog_layout);
Button selectFirstChoice = (Button)selection.findViewById(R.id.btn_select_choice_1);
Button selectSecondChoice = (Button)selection.findViewById(R.id.btn_select_choice_2);
selectFirstChoice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
selection.dismiss();
}
});
selectSecondChoice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
selection.dismiss();
}
});
selection.setTitle("Some Title");
selection.setCancelable(false);
selection.show();
The preview is alright:
Preview
It works well on Nougat, but when I run it on Lollipop (5.0 and 5.1.1), the buttons are without styling, although the same button styling worked on activity buttons on Lollipop:
App
I wonder what could be going wrong, I also tried moving the Dialog into a DialogFragment but I faced the same behavior.
Found the following solution:
In my Styles.xml, I added:
<style name="dialog_button" parent="Theme.AppCompat.Light.Dialog">
<item name="colorButtonNormal">#color/button_secondary_normal_background</item>
<item name="android:textColor">#color/button_secondary_normal_text</item>
<item name="android:textSize">#dimen/button_textSize</item>
<item name="android:padding">#dimen/button_padding</item>
</style>
And in my custom dialog layout, I used that style as a theme for my buttons:
<?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_margin="8dp"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_select_student"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/student"
android:theme="#style/dialog_button" />
<Button
android:id="#+id/btn_select_tutor"
android:layout_width="match_parent"
android:text="#string/tutor"
android:layout_height="wrap_content"
android:theme="#style/dialog_button" />
</LinearLayout>
I have this xml:
<com.me.view.button.SwipeableButton
android:id="#+id/mainMenuSwipeableButton"
android:layout_width="250dp"
android:layout_height="62dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
app:button="#drawable/main_menu_button"
android:visibility="visible" />
and:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/icon_menu_press" /> <!-- pressed -->
<item android:drawable="#drawable/icon_menu" /> <!-- default -->
</selector>
why isn't my click caught when i have this code?
setCloseToolTipClickListener(getMainLayout().findViewById(R.id.mainMenuSwipeableButton), tooltipView);
private void setCloseToolTipClickListener(View view, final LinearLayout tooltip) {
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tooltip.setVisibility(View.GONE);
}
});
}
I have layer, it is transparent. I can click on button only, I want to click through layer.
Here's my Manifest:
<activity android:name=".Test" android:theme="#style/Theme.Transparent"/>
Here's Transparent theme style:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Here's my Activity code:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
// v I can touch through app, but cant click on button
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
// ^ I can touch through app, but cant click on button
Button xclose = (Button) findViewById(R.id.buttonx);
xclose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
}
}
Here's my test.xml layout:
<?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" >
<Button android:id="#+id/buttonx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="X" />
</RelativeLayout>
Try this code and set click listener on the linear layout
<?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" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll">
<Button android:id="#+id/buttonx" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" android:text="X" />
</LinearLayout>
</RelativeLayout>
You already do the following here to set the click listener for the X button.
Button xclose = (Button) findViewById(R.id.buttonx);
xclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
Now all you need to do is the same thing but for the Layout which you have created that is transparent. So that when you click the layout in the area that your play button is, it listens to the click, rather than the button, as the button is out of reach.
i have this code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/fundo" /> <!-- pressed -->
<item android:drawable="#drawable/fundo2" /> <!-- default -->
</selector>
in other file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn"
/>
</FrameLayout>
and in the class
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
return true;
case MotionEvent.ACTION_UP:
mp.pause();
return true;
}
return true;
}
i don't know why, when the button is pressed the image don't change, what is the reason?
thanks!!
Instead of android:background="#drawable/btn" please use android:src="#drawable/btn".
It Works for me .
Inside ur xml, for the button background give ur selector xml file name.
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ur state change xml file name"
/>
You can use setBackgroundResource(ResId) for the change on button u can write this in the OnClick() method of the Button.