How to make the switch button center in dialog - android

I want to switch button center in dialog,but it's failed...
Why is this?
Switch sw = new Switch(MainActivity.this);
sw.setTextOn("start");
sw.setTextOff("close");
sw.setGravity(Gravity.CENTER_HORIZONTAL);
AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this);
myDialog.setTitle("title");
myDialog.setMessage("message");
myDialog.setView(sw);

Try with below code
Switch sw = new Switch(MainActivity.this);
sw.setTextOn("start");
sw.setTextOff("close");
LinearLayout linearLayout = new LinearLayout(MainActivity.this);
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
linearLayout.addView(sw);
AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this);
myDialog.setTitle("title");
myDialog.setMessage("message");
myDialog.setView(linearLayout);
myDialog.show();

Use the custom_layout instead.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use LayoutInflater to inflate the layout in dialog
View v = LayoutInflater.from(getActivity()).inflate(R.layout.custom_layout, null);
myDialog.setView(v);

Use RelativeLayout instead, set android:centerInParent=true for SwitchButton

Related

Views not found for Alert Dialog in fragments

I am trying to populate an Alert Dialog (with a layout file having an EditText & Button) within a fragment of Activity.
The components are not being found by calling findViewById() in the fragment's corresponding class. I have written this code inside the class :
AlertDialog.Builder builder=new AlertDialog.Builder(context);
View v=getLayoutInflater().inflate(R.layout.layout_alert_dialog,null);
final EditText txtAddNew=v.findViewById(R.id.txtAddNew); //null
final TextView txtErr = v.findViewById(R.id.txtErr); //null
Button btnAdd=v.findViewById(R.id.btnAdd); //null
I think the problem raises from this line of code:
View v=getLayoutInflater().inflate(R.layout.layout_alert_dialog,null);
The alertDialog XML code is below:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/txtAddNew"
android:hint="Category name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtErr"
/>
<Button
android:id="#+id/btnAdd"
android:text="Add Category" />
</LinearLayout>
Can anyone find what the actual problem is? Thanks!
Yes, that's correct
Do something like this :
LayoutInflater layout = LayoutInflater.from(this);
final View view = layout.inflate(R.layout.dialog_layout, null);
final AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(view);
dialog.show();
You can do it like below.
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
alertBuilder.setView(getLayoutInflater().inflate(R.layout.layout_alert_dialog,null));
AlertDialog alertDialog = alertBuilder.create();
EditText txtAddNew = alertDialog.findViewById(R.id.txtAddNew);
TextView txtErr = alertDialog.findViewById(R.id.txtErr);
Button btnAdd = alertDialog.findViewById(R.id.btnAdd);
alertDialog.show();
Try using dialog.setContentView(v)
See the difference between setView(v) and setContentView(v) here:
What is difference between Dialog.setContentView( View ) & AlertDialog.setView( View )

Inflate image on top of a popupview object

Currently for inflating images (zoom in animation) we use this library.
while on top of regular activities it looks fine (Simple layout), on top of a popupview the image is "behind" the popup.
Tried to change the elevation parameters, but no luck there either.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:elevation="2dp"
tools:context="com.MapActivity">
<com.vatsal.imagezoomer.ImageZoomButton
android:id="#+id/bt_picture"
android:layout_width="20dp"
android:layout_height="17dp"
android:layout_alignTop="#id/tx_description"
android:layout_marginTop="1dp"
android:layout_alignLeft="#+id/bt_date_time"
android:layout_marginLeft="-25dp"
android:elevation="10dp"
android:background="#drawable/ic_picture" />
</RelativeLayout>
And this is how the popupview and windows are inflated:
popupView = getLayoutInflater().inflate(R.layout.job, null);
popupWindow = new PopupWindow(popupView,
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
Should we try and use a different approach? Mabye ditch the library and inflate it differently?
Thanks
We've followed mmdreza's advice, here's a snippet of the implementation.
As the documantation states, the alertdialog will be on top of the popupview object.
myHolder.jobImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(v.getContext());
LayoutInflater inflater =
(LayoutInflater)v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mView = inflater.inflate(R.layout.inflated_picture_dialog, null);
Picasso.get().load(jobView.getJobImageURL()).fit().into(((ImageButton)mView.findViewById(R.id.inflated_picture)));
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
}
});

How to change the text gravity on alertdialog

I've been searching for a while and I haven't found the solution to this problem of mine yet, I have a alertdialog with a few items (Here it is)
But as you can see, the text is aligned to the left. Is there a way to align the items text to the center?
Here's a bit of my code
chosenItem.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose a category");
// add a list
String[] items = {"Hey", "stack", "overflow", "please", "help", "Medical", "Ammo"};
// String[] items = {"Weapons", "Tools", "Items", "Clothing", "Components", "Medical", "Ammo"};
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
reciclador(3, rSlotFrame, rSlotText, rSlotProbabilidade, 30, 50, 1, 0, 100, 100, 100, 0, "cloth_icon", "metalfrags_icon", "sewingkit_icon", "");
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getWindow().setLayout(800, 700);
}
});
}
Thanks in advance.
you can use custom dialog for that like this create a custom layout like this
<?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="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="title"
android:textColor="#000" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Lorem Ipsum is simply dummy text " />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="description" />
</LinearLayout>
now create custom dailog llike below code
final Dialog dialog = new Dialog(LoginActivity.this);
dialog.setContentView(R.layout.custom_dialog_layout);
Textview tvTitle;
tvTitle = (Textview) dialog.findViewById(R.id.tvTitle);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
tvTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// perform your action here
}
});
dialog.show();
You can use custom view in AlertDialog.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(YourActivity.this);
View alertView = getLayoutInflater().inflate(R.layout.fragment_dialog, null, false);
alertDialog.setView(alertView);
Add your custom requirement of your text in your layout file.
From documenation:
If you want a custom layout in a dialog, create a layout and add it to an AlertDialog by calling setView() on your AlertDialog.Builder object.
you need to apply theme for the dialog on the initial state
AlertDialog dialog = new AlertDialog(context, R.theme.DialogTheme);
and declare theme in your theme.xml or styles.xml
<style name="DialogTheme" parent="Animation.AppCompat.Dialog">
<item name="android:itemTextAppearance">#style/DialogTextAppear</item>
</style>
<style name="DialogTextAppear" parent="TextAppearance.AppCompat">
<item name="android:gravity">center</item>
</style>
You can find AlertDialog Title view by this code and set gravity manual but first you must create a AlertDialogBuilder and one Alert dialog like in code
MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(this);
dialogBuilder
.
.
.
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
LinearLayout titleTemplate = alertDialog.findViewById(R.id.title_template);
titleTemplate.setGravity(Gravity.RIGHT);
You can find all view in alert_dialog.xml and find in code and use it.

Find a View of an AlertDialog

I want to get a View from an AlertDialog:
Button R = (Button) findViewById(R.id.toggleButtonA);
But if I do this, it always is null. What can I do to get and edit the view?
Here are the relevant parts of code:
What creates the Dialog:
public void openSelection(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialayout, null));
AlertDialog dialog = builder.create();
dialog.show();
}
The dialayout.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/toggleButtonA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:onClick="select"
android:text="All"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toggleButtonM" />
</android.support.constraint.ConstraintLayout>
</LinearLayout>
You need to assign your inflated view to a variable first. Example:
public void openSelection(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.activity_main, null); // this line
builder.setView(v);
AlertDialog dialog = builder.create();
dialog.show();
Button RR = (Button) v.findViewById(R.id.toggleButtonA); // this is how you get a view of the button
}
Please reneame your button variable name R to another name perhaps because R is a reserved word.
Inflate the parent layout first,
LayoutInflater inflater = this.getLayoutInflater();
View parent = inflater.inflate(R.layout.activity_main, null);
then set your builder to that parent view.builder.setView(parent);
you can then perform any view finding with that parent inflated view. Or you can call AlertDialog diag = builder.create(); and perform a find with the diag.findViewById(R.id.name);
so Button btR = (Button) diag.findViewById(R.id.toggleButtonA);
or Button btR = (Button)parent.findViewById(R.id.toggleButtonA);

how to customize Dialog header in android studio?

I'm using a custom dialog that I've created in a separate xml file in my project, and I'm coloring the main window a blueish tint,
but the main header still remains the default white color.
Is there no way to change the font color, size, background for the header?
Is the only thing I can change in the header the text?
xml:
<?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"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#3edfbc"
android:orientation="vertical">
<TextView
android:id="#+id/textaligmentManager_loader_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="Initlizing Wifi"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.github.ybq.android.spinkit.SpinKitView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/barcodeScanning_spinkit"
style="#style/SpinKitView.Large.FoldingCube"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="#+id/textaligmentManager_loader_textview"
android:padding="20dp"
android:layout_centerHorizontal="true"
app:SpinKit_Color="#ffffff" />
</RelativeLayout>
dialog:
Dialog dialog = new Dialog(Scanning_Barcode_Activity.this);
dialog.setContentView(R.layout.aligment_manager_loader_layout);
dialog.setTitle("Loading");
dialog.setCancelable(false);
//set up text
loaderScreenMainText = (TextView) dialog.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
//progressBar = (ProgressBar) dialog.findViewById(R.id.barcodeScanning_spinkit);
//DoubleBounce doubleBounce = new DoubleBounce();
//progressBar.setIndeterminateDrawable(doubleBounce);
//now that the dialog is set up, it's time to show it
dialog.show();
You can use AlertDialog which is a subclass of Dialog class. Here you can define a custom layout containing everything such as title, body and buttons. No extra title section will appear. Here is a demo:
AlertDialog.Builder builder = new AlertDialog.Builder(Scanning_Barcode_Activity.this);
builder.setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(Scanning_Barcode_Activity.this);
View dialogView = inflater.inflate(R.layout.aligment_manager_loader_layout, null);
TextView loaderScreenMainText = (TextView) dialogView.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
builder.setView(dialogView);
AlertDialog dialog = builder.create();
dialog.show();
Design your custom layout in such a way that the layout itself contains the header part. And then skip this code:
dialog.setTitle("Loading");
Instead add this statement:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
You can also use AlertDialog. In that case requestWindowFeature() method is not required.
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(Scanning_Barcode_Activity.this);
LayoutInflater inflater = LayoutInflater.from(Scanning_Barcode_Activity.this);
View customView = inflater.inflate(your_layout, null);
builder.setCancelable(false);
loaderScreenMainText = (TextView) customView.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
builder.setView(customView);
AlertDialog dialog = builder.create();
dialog.show();
There are plenty of stylish open source libraries that you can use for customizing header parts.
Here I'm sharing my newly developed open source library : PanterDialog
Hope it will help you.

Categories

Resources