Set Google style bottomsheet Dialog in Android Studio App? - android

This is my code that gives a BottomSheet Dialog. After implementation it gives me a normal BottomSheet Dialog, how can i make this Bottom Sheet look like Google BottomSheet Dialog with rounded corners and something like a middle gray line at the top?
Even if it doesn't look exactly like the Bottomsheet Facebook and Google uses, I just want to have a rounded corner for this code.
What do I change or add in my code?
MainActivity.Java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
}
private void openDialog() {
View view = getLayoutInflater().inflate(R.layout.sheet_main, null);
dialog = new BottomSheetDialog(this);
dialog.setContentView(view);
TextView camera_sel = (TextView) view.findViewById(R.id.camera);
TextView gallery_sel = (TextView) view.findViewById(R.id.gallery);
camera_sel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takePhotoFromCamera();
dialog.dismiss();
}
});
gallery_sel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takePhotoFromGallery();
dialog.dismiss();
}
});
dialog.show();
}
sheet_main.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:orientation="horizontal"
android:padding="4dp">
<TextView
android:id="#+id/camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:drawablePadding="8dp"
android:drawableTop="#drawable/ic_camera"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="Camera"
android:textSize="18sp" />
<TextView
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:drawablePadding="8dp"
android:drawableTop="#drawable/ic_panorama"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="Gallery"
android:textSize="18sp" />
</LinearLayout>
UPDATE:
I was able to achieve this by adding these to my style.xml and setting it on the theme used for my dialog activity. Thanks!
<style name="HomeTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="bottomSheetDialogTheme">#style/ThemeOverlay.Demo.BottomSheetDialog</item>
</style>
<style
name="ThemeOverlay.Demo.BottomSheetDialog"
parent="#style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">#style/Widget.Demo.BottomSheet</item>
</style>
<style name="Widget.Demo.BottomSheet" parent="Widget.MaterialComponents.BottomSheet">
<item name="shapeAppearanceOverlay">#style/ShapeAppearanceOverlay.Demo</item>
</style>
<style name="ShapeAppearanceOverlay.Demo" parent="">
<item name="cornerSize">18dp</item>
<item name="cornerFamily">rounded</item>
</style>

Related

Include layout inside popup menu items [duplicate]

This question already has answers here:
How to make an overflow menu like in Chrome?
(2 answers)
Closed 4 years ago.
I try to add a popup menu in my service, for some features. I want to add some horizontal icons. I tried to add them in a layout and after include that layout in a menu item. But nothing.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_id"
android:title="TEST"
app:showAsAction="always"
app:actionLayout="#layout/menu_navigation"/>
<item android:id="#+id/downloads"
android:title="#string/downloads"
android:icon="#drawable/ic_download"
app:showAsAction="always|withText"/>
<item android:id="#+id/settings"
android:title="#string/settings"
android:icon="#drawable/ic_settings"
app:showAsAction="always|withText" />
</menu>
try to use Popup Window use custom layout, this way you will be able to add icons
LayoutInflater layoutInflater = (LayoutInflater)
MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.pop_window_layout,null);
Button but1 = (Button)customView.findViewById(R.id.popwinitem1);
Button but2 = (Button)customView.findViewById(R.id.popwinitem2);
Button but3 = (Button)customView.findViewById(R.id.popwinitem3);
//instantiate popup window
final PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//display the popup window below whatever widget you want
popupWindow.showAsDropDown(text1);
//popupWindow.showAtLocation(holder.text1, Gravity.RIGHT, 0, 0);
//close the popup window on button click
but1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// yourfunction();
popupWindow.dismiss();
}
});
but2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// yourfunction();
popupWindow.dismiss();
}
});
but3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
pop_window_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/colorWhite"
android:padding="10dp">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/popwinitem1"
android:padding="5dp"
android:textSize="15sp"
android:text="#string/save"
android:textColor="#color/colorBlack"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/popwinitem2"
android:text="#string/delete"
android:textSize="15sp"
android:padding="5dp"
android:textColor="#color/colorBlack"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/popwinitem3"
android:text="#string/cancel"
android:padding="5dp"
android:textSize="15sp"
android:textColor="#color/colorBlack"/>

Android Error in Text_Input_Layout Edittext

Does anyone know how I can change the position of the mentioned part of error floating label (as in the attached image) ? In the picture it is on the left but I want it to be on the right side of the edittext (because of my native language) . I tried gravity and layout gravity but none of them worked out . Thanks in advance .
<android.support.design.widget.TextInputLayout
android:id="#+id/numid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
app:errorEnabled="true"
app:errorTextAppearance="#style/myerror"
android:layout_gravity="center"
app:hintTextAppearance="#style/TextAppearance.AppCompat.TextInputLayout"
style="#style/EditScreenTextInputLayoutStyle">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/tie"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="number"
android:gravity="right"
android:textAlignment="gravity"
android:hint="#string/hint2"
android:textDirection="rtl"
/>
<style name="TextAppearance.AppCompat.TextInputLayout" parent="#android:style/TextAppearance">
<item name="android:textColor">#ff0000</item>
<item name="android:layout_gravity">right</item>
<item name="android:textColorHint">#736f6e</item>
<item name="android:gravity">right</item>
<style name="EditScreenTextInputLayoutStyle" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#000</item>
<item name="colorControlActivated">#000</item>
<item name="colorControlHighlight">#ff0000</item>
<item name="colorAccent">#ffff02</item>
<item name="android:textColorHint">#0000ff</item>
</style>
<style name="myerror" parent="TextAppearance.Design.Error">
<item name="android:textColor">#ff0000</item>
<item name="android:background">#000000</item>
<item name="android:gravity">center</item>
</style>
Try this:
Drawable err_indiactor = ContextCompat.getDrawable(this,R.drawable.indicator_input_error);
yourEditText.setCompoundDrawablesWithIntrinsicBounds(null, null, err_indiactor, null);
this is the Drawable indicator_input_error
The code below works for u
public class MainActivity extends Activity {
EditText use, pass;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
use = (EditText) findViewById(R.id.edituse);
pass = (EditText) findViewById(R.id.editpas);
button = (Button) findViewById(R.id.click);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final String use1 = use.getText().toString();
final String pass1 = pass.getText().toString();
if (!use1.matches("") && !pass1.matches("")) {
Toast.makeText(getApplicationContext(), "Succes",
Toast.LENGTH_SHORT).show();
use.setError(null);
pass.setError(null);
} else if (use1.matches("")) {
use.setError("This field can not be blank");
} else if (pass1.matches("")) {
pass.setError("This field can not be blank");
}
}
});
}
}
And the Xml file
<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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test"
android:textSize="25sp"
android:gravity="center" />
<EditText
android:layout_below="#+id/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/edituse"/>
<EditText
android:layout_below="#+id/edituse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editpas"
/>
<Button
android:id="#+id/click"
android:layout_below="#+id/editpas"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click"/>
</RelativeLayout>
Hope this helps...and for English this works.For ur language its a right to left writing,then it may be the problem for the left alignment of the error msg.... ...Don't worry try this.

Android dialog has no padding

I have a problem with AlertDialog - dialog has no padding (like on screenshot - dialog with no padding inside).
My Activity has theme "NoActionBar"
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
My Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDial();
}
});
}
private void showDial() {
new AlertDialog.Builder(this)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton("tak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.show();
}
Does anyone know how to fix it?
You must use custom view in your dialog alert
private void showDial() {
View content = this.getLayoutInflater().inflate(R.layout.custom_dialog_alert, null);
((TextView)content.findViewById(R.id.dialogTitle)).setText("Delete entry");
((TextView)content.findViewById(R.id.dialogText)).setText("Are you sure you want to delete this entry?");
AlertDialog alert = new AlertDialog.Builder(Test.this, R.style.no_title_dialog)
.setCancelable(true)
.setPositiveButton("tak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setView(content)
.create();
alert.show();
}
custom_dialog_alert.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="wrap_content"
android:orientation="vertical"
android:background="#android:color/holo_red_light">
<TextView
android:id="#+id/dialogTitle"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingTop="10dp"
android:ellipsize="end"
android:gravity="left"
android:lines="1"
android:paddingLeft="26dp"
android:paddingRight="10dp"
android:text="Dialog title"
android:textColor="#android:color/black"
android:background="#android:color/background_light"
android:textSize="20sp" />
<RelativeLayout
android:id="#+id/contentText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:minHeight="40dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/dialogText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="left"
android:maxHeight="300dp"
android:text="Dialog text"
/>
</RelativeLayout>
no_title_dialog in styles.xml
<style name="no_title_dialog" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
I hope it helps
Add this style to your dialog.
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(StartUpActivity.this
,R.style.AlertDialogCustom));
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:fitsSystemWindows">false</item>
</style>

How to center an image in an Android Alert Dialog Title?

I am trying to center the image that I am adding to my Alert Dialog in an Android app as follows:
android.app.AlertDialog.Builder appInfoDialog = new AlertDialog.Builder(this);
appInfoDialog.setIcon(R.drawable.image);
appInfoDialog.setTitle(" ");
appInfoDialog.setMessage("App Info...");
appInfoDialog.setCancelable(true);
AlertDialog dialog = appInfoDialog.create();
dialog.show();
But how can I center this image? Currently, it is left-aligned.
I cannot find a gravity or layout_align property for it!
You can not do this, but You can create Your own Dialog like this:
/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
</resources>
/layout/custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dip"
android:text="wpisz tekst by wyszukać piosenkę"
android:textColor="#5E636D" />
<EditText
android:id="#+id/value_mini"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40.0dip"
android:background="#drawable/input_text"
android:paddingLeft="10px" />
<Button
android:id="#+id/search_mini"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dip"
android:text="szukaj" />
</RelativeLayout>
in method:
Dialog dialog = new Dialog(Start.this, R.style.Dialog);
dialog.setContentView(R.layout.custom_dialog);
Button button = (Button) dialog.findViewById(R.id.search_mini);
final EditText et = (EditText) dialog.findViewById(R.id.value_mini);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
dialog.show();

Progress Dialog in android of our own

How to create a progress dialog with blue color background and my own spinner style in android? Please help me out.
Dialog dialog = new Dialog(MainActivity.this, R.style.mydialogstyle);
dialog.setContentView(R.layout.dialogmain);
dialog.setCancelable(false);
ProgressBar pbar = (ProgressBar) dialog.findViewById(R.id.progressBar);
Button btncancel = (Button) dialog.findViewById(R.id.cancelbtn);
btncancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
R.layout.dialogmain--->>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:background="#0000FF" android:layout_height="wrap_content">
<ProgressBar style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="#android:drawable/progress_horizontal"//You can change drawable of spinner
android:layout_marginTop="10dip" android:layout_width="250dip"
android:id="#+id/progressBar" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"></ProgressBar>
<Button android:layout_width="wrap_content" android:background="#drawable/cancel"
android:layout_height="wrap_content" android:text="" android:id="#+id/cancelbtn"
android:layout_marginTop="2dip" android:layout_below="#+id/progressBar"
android:layout_centerHorizontal="true"></Button>
</RelativeLayout>
Res->values-->style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="mydialogstyle" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">false</item>
</style>
</resources>

Categories

Resources