I want to create a shadow for my custom dialog is that possible ?
GhazalActivity.public void viewShareMenu() {
Dialog share=new Dialog(this,R.style.shareDialogStyle);
share.setContentView(R.layout.share_popup_layout);
LayoutParams params = share.getWindow().getAttributes();
params.y = this.getResources().getDimensionPixelSize(R.dimen.topbar_height);
params.gravity=(Gravity.RIGHT|Gravity.TOP);
share.getWindow().setAttributes(params);
share.show();
}
styles.xml :
<style name="shareDialogStyle" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:fadeEnabled">true</item>
<item name="android:fadeDuration">1500</item>
<item name="android:shadowColor">#color/temp</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">5</item>
<item name="android:shadowRadius">10</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
share_popup_layout.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dip"
android:background="#color/bg_Ghazal_share_menu"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
style="#style/shareDialogButtons"/>
</LinearLayout>
is there any solution for doing that ?
I have created my own custom dialog with a dropped shadow, you can use it as your desire, in the first step you should create a Android shape for both shadow and dialog frame. Here is what I have supplied (dialog_frame_shadow.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle"> <!-- this shape is used as shadow -->
<padding android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp"/>
<solid android:color="#44000000"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle"> <!-- this is for dialog frame -->
<corners android:radius="5dp"/>
<stroke android:color="#ff272727" android:width="2dp" />
<gradient android:angle="90"
android:startColor="#ffa7a7a7"
android:centerColor="#ff6a6a6a"
android:endColor="#ffa7a7a7"
android:type="linear"/>
</shape>
</item>
</layer-list>
In the next step you should change your dialog theme as what is in the following:
<style name="my_dialog_theme">
...
<item name="android:windowBackground">#drawable/dialog_frame_shadow</item>
...
</style>
Now you are done, just create a new instance of the Dialog class and apply this theme to it (in Dialog constructor):
Dialog dialog = new Dialog(this, R.style.my_dialog_theme);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.mdialog);
dialog.show();
Here is its screenshot:
try this, create an xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#ffffff"
android:centerColor="#d3d7cf"
android:endColor="#2e3436"
android:angle="90" />
</shape>
make a View and set above xml to its backgroung like:
<View android:id="#+id/divider" android:background="#drawable/black_white_gradient"
android:layout_width="match_parent" android:layout_height="5sp"
<!--greater the height, more wider the shadow-->
/>
now if you want to drop shadow to the left of your view, align this view to you views left, if you want shadow on bottom, align this view under you view ...hope you get it now
Related
I want textColor as black even if textView is disabled. For it I use a selector but its not working, its giving me default grey textcolor in disabled state.
Selector file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true" android:color="#color/black" />
<item android:state_enabled="false" android:state_focused="false" android:color="#color/black" />
<item android:state_enabled="true" android:state_focused="true" android:color="#color/black"/>
<item android:state_enabled="true" android:state_focused="false" android:color="#color/black"/>
<item android:color="#color/black"/>
</selector>
TextView in which selector is set is as follows:
<com.rengwuxian.materialedittext.MaterialTextView
android:id="#+id/policyEndDateText"
style="?android:attr/spinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="#color/textview_color_selector"
android:enabled="false"
android:hint="New Policy End Date"
android:textSize="#dimen/insurance_text_size"
app:met_floatingLabelTextSize="#dimen/insurance_floating_label_textSize"
app:met_hideUnderline="true"
app:met_floatingLabel="highlight"
app:met_primaryColor="#color/material_text"/>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary_purple"/>
<item android:drawable="#color/colorAccent_purple" android:state_pressed="true"/>
<item android:drawable="#color/colorAccent" android:state_focused="true"/>
</selector>
i use LayoutInflater to create dynamically ui with xml files but resualt is not suitable.
like inflater not set layout params?
my sample code:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.linearlayoutdef, null);
ImageView iv = (ImageView) inflater.inflate(R.layout.imageviewdef, null);
iv.setImageBitmap(op.getImage());
TextView tv = (TextView) inflater.inflate(R.layout.textviewdef, null);
//tv.setText(op.getType().toString());
tv.setText("Call");
Button be = (Button) inflater.inflate(R.layout.buttondef, null);
be.setText("Edit");
Button bd = (Button) inflater.inflate(R.layout.buttondef, null);
bd.setText("Delete");
ll.addView(iv);
ll.addView(tv);
ll.addView(be);
ll.addView(bd);
my sample xml files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/linearLayout" >
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="linearLayout" >
<item name="android:orientation">horizontal</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">40pt</item>
<item name="android:background">#color/white</item>
<item name="android:layout_marginLeft">3pt</item>
<item name="android:layout_marginRight">3pt</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/imageViewStyle" android:contentDescription="#string/todo"
android:src="#drawable/stop"
>
</ImageView>
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="imageViewStyle" parent="#android:style/Widget">
<item name="android:layout_width">30pt</item>
<item name="android:layout_height">30pt</item>
<item name="android:layout_gravity">center</item>
<item name="android:layout_marginLeft">3pt</item>
<item name="android:background">#drawable/imageview_background</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<stroke android:width="0.9pt" android:color="#808080"/>
</shape>
</item>
</layer-list>
witch result is:
but right result must be :
what can i do to get right result?
You need to set layout params to each view you add to your view group.
Example :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
view.setParams(params);
I want to create round corner button. This is what I tried:
I have defined the button's background drawable button_bg.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#color/grey">
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#color/dark_grey"
>
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
<item
android:state_enabled="true"
android:drawable="#color/blue"
>
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
</selector>
Then, I apply it to my button in layout file:
<Button
android:text="#string/press_me"
android:layout_margin="12dp"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#drawable/button_bg"
/>
But my button is still showing sharp corner instead of the round one, why??
(Please don't just throw me a link about how to make round corner, my above code followed those tutorials, I just want to know where am I wrong. Thanks)
remove android:drawable from the item node have it like this.
<item android:state_pressed="true"
android:state_enabled="true">
EDIT : (How can I specify the color from colors.xml for item then?)
Create a stateful color drawable for your button text color.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#000000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#000000" />
<item android:color="#ffffff" />
</selector>
and then set this as your text color for your button .
android:textColor="#drawable/button_text_color"
Remove all android:drawable from item as below then it will work.
When you add the attribute android:drawable in item then this drawable overlapped your created shape. That's why you can't see your rounded shape.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"><shape>
<solid android:color="#ef4444" />
<stroke android:width="1dp" android:color="#992f2f" />
<corners android:radius="16dp" />
</shape></item>
<item android:state_enabled="true" android:state_pressed="true"><shape>
<solid android:color="#ef4444" />
<stroke android:width="1dp" android:color="#992f2f" />
<corners android:radius="16dp" />
</shape></item>
<item android:state_enabled="true"><shape>
<solid android:color="#ef4444" />
<stroke android:width="1dp" android:color="#992f2f" />
<corners android:radius="16dp" />
</shape></item>
</selector>
Is it possible to achieve the following as a drawable (for a background) using a Layer-List ???
Thanks!
Try this xml :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="10dp"
android:right="10dp">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#0000FF" />
</shape>
</item>
<item
android:right="10dp"
android:top="10dp">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FF0000" />
</shape>
</item>
<item
android:left="10dp"
android:top="10dp">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFF00" />
</shape>
</item>
<item
android:bottom="10dp"
android:left="10dp">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00FF00" />
</shape>
</item>
<item
android:bottom="10dp"
android:left="10dp"
android:right="10dp">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#0000FF" />
</shape>
</item>
<item
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#3f3f3f" />
</shape>
</item>
</layer-list>
I have the following selector for a button (with 2 states, regular and pressed):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#3498DB" />
<stroke
android:width="1dp"
android:color="#2980B9" />
<corners
android:radius="0dp" />
<padding
android:left="12dp"
android:top="12dp"
android:right="12dp"
android:bottom="12dp" />
</shape>
</item>
<item>
<shape>
<solid
android:color="#2980B9" />
<stroke
android:width="1dp"
android:color="#2980B9" />
<corners
android:radius="0dp" />
<padding
android:left="12dp"
android:top="12dp"
android:right="12dp"
android:bottom="12dp" />
</shape>
</item>
</selector>
My button has the following which specifies the background as the above selector:
<Button
android:id="#+id/button_LaunchShiftsGame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/ShiftsMode"
android:background="#drawable/selector_Button"
style="#style/Button_Text" />
I need to access and change the colors for both states from code when the Activity loads.
How can I do this?
StateListDrawable gradientDrawable = (StateListDrawable) inflatedView.getBackground();
DrawableContainerState drawableContainerState = (DrawableContainerState) gradientDrawable.getConstantState();
Drawable[] children = drawableContainerState.getChildren();
LayerDrawable selectedItem = (LayerDrawable) children[0];
LayerDrawable unselectedItem = (LayerDrawable) children[1];
GradientDrawable selectedDrawable = (GradientDrawable) selectedItem.getDrawable(0);
GradientDrawable unselectedDrawable = (GradientDrawable) unselectedItem.getDrawable(0);
selectedDrawable.setStroke(STORKE_SIZE, NOTIFICATION_COLOR);
unselectedDrawable.setStroke(STORKE_SIZE, NOTIFICATION_COLOR);
I used this to change the stroke, it can be helpfull.
This is the Kotlin version of the #Borja's answer with drawable resource
val drawableRes: Drawable? = ResourcesCompat.getDrawable(mContext.resources, R.drawable.drawable_res_id, null)
val drawableContainerState: DrawableContainer.DrawableContainerState = drawableRes?.constantState as DrawableContainer.DrawableContainerState
val children: Array<Drawable> = drawableContainerState.children
val selectedItem: GradientDrawable = children[0] as GradientDrawable
val unselectedItem: GradientDrawable = children[1] as GradientDrawable
selectedItem.setStroke(2, colorState)
unselectedItem.setStroke(2, colorState)
and yes! Many thanks to #Borja. I found this working well as in 2022.
I have below selector round_circle_blue.xml shape xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<size android:width="#dimen/_50sdp" android:height="#dimen/_50sdp" />
<solid android:color="#color/color_blue" />
<stroke android:width="#dimen/_2sdp" android:color="#color/white" />
</shape>
</item>
</selector>
And I have set it as background in text view as below
<TextView
android:id="#+id/tvActivityicon"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_40sdp"
android:textColor="#color/white"
android:gravity="center"
android:text="A"
android:textSize="#dimen/_14sdp"
android:background="#drawable/round_circle_blue" />
and in kotlin file i have use below code to change the color.
val gradientDrawable = tvActivityicon.background as StateListDrawable
val drawableContainerState = gradientDrawable.constantState as DrawableContainer.DrawableContainerState
val children = drawableContainerState!!.children
val selectedItem = children[0] as GradientDrawable
selectedItem.setColor(Color.parseColor("#FD00DF"))