Android Studio Dialog with Style

 Java class

package com.elesteam.pefis.Dialogs;

/**
*
*
* BASIC INPUT DIALOG
*
* Description_______________
*
* Date______________________
*
*
*/


import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.DialogFragment;

import com.elesteam.pefis.R;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Objects;

public class BasicInputDialog extends AppCompatDialogFragment {
private EditText et_concept;
private EditText et_date;
private BasicInputDialogListener listener;

private Button positiveButton;

//control variables
private int mYear, mDay, mMonth;



public static BasicInputDialog newInstance(String description, String date) {
BasicInputDialog adf = new BasicInputDialog();
Bundle bundle = new Bundle(2);
bundle.putString("description", description);
bundle.putString("date", date);
adf.setArguments(bundle);
return adf;
}




@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.CustomAlertDialog);

LayoutInflater inflater = Objects.requireNonNull(getActivity()).getLayoutInflater();
View view = inflater.inflate(R.layout.layout_basic_input_dialog, null);

builder.setView(view)
.setTitle(R.string.enter_details)
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

String concept = et_concept.getText().toString();
String date = et_date.getText().toString();


try {
listener.applyTextsBID(concept, date);
} catch (ParseException e) {
e.printStackTrace();
}


}
});

// GETTING VALUES
assert getArguments() != null;
String description = getArguments().getString("description");
String date = getArguments().getString("date");



et_concept = view.findViewById(R.id.det_concept);
et_date = view.findViewById(R.id.det_date);

//SET EText
et_concept.setText(description);
et_date.setText(date);


et_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);


DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(),
new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
et_date.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);

}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}


});


et_concept.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable s) {}

@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0) {
if (et_date.getText().length() != 0)
positiveButton.setEnabled(true);
else
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(false);
}
}
});

et_date.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable s) {}

@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0) {
if (et_concept.getText().length() != 0)
positiveButton.setEnabled(true);
else
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(false);
}
}
});

return builder.create();
}


public interface BasicInputDialogListener {
void applyTextsBID(String concept, String date) throws ParseException;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

try {
listener = (BasicInputDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "Must be implemented with listener");
}
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This removes black background below corners.
setStyle(DialogFragment.STYLE_NO_FRAME, 0);
}

@Override
public void onStart() {
super.onStart();
AlertDialog d = (AlertDialog) getDialog();
if (d != null) {
positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setEnabled(false);
}else{
Log.d("BUTTOMCON","PASO A ELSE");
}

}
}




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="@drawable/dialog_bg_white"
android:padding="20dp">

<EditText
android:id="@+id/det_concept"
android:layout_width="match_parent"
style="@style/TextEdit2Theme"
android:layout_height="wrap_content"
android:hint="@string/name_or_description"
android:inputType="text" />


<EditText
android:id="@+id/det_date"
style="@style/TextEdit2Theme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/det_concept"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:focusable="false"
android:hint="@string/date"
android:inputType="date" />

</RelativeLayout>

style/TextEdit2Theme

<!-- TextEdit WITH GREY BACKGROUND AND BORDER RADIUS (FOR DIALOG) -->
<style name="TextEdit2Theme" parent="AppTheme">
<item name="android:background">@drawable/spinner_bg</item>
</style>

drawable/spinner_bg

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">

<item>

<layer-list>

<item>
<shape>
<gradient android:angle="90" android:endColor="#eee" android:startColor="#eee" android:type="linear" />

<corners android:radius="5dp" />

<padding android:bottom="10dp" android:left="3dp" android:right="3dp" android:top="10dp" />
</shape>
</item>

</layer-list>

</item>

</selector>

drawable/dialog_bg_white

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="#fff" />

<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />

</shape>

Implementation

public class MainActivity extends AppCompatActivity
implements
BasicInputDialog.BasicInputDialogListener {
BasicInputDialog basic_dialog;
basic_dialog = BasicInputDialog.newInstance(
"",
"");

Invocation

basic_dialog.show(getSupportFragmentManager(), "BasicD");


Result (Activity result)

/**
* SAVE ITEM OF BANK, SAVING (BASIC INPUT DIALOG)
*
* @param concept
* @param date
*/
@Override
public void applyTextsBID(String concept, String date) throws ParseException {

if (!concept.equals("") && !date.equals("")) {
switch (pefis_option) {

// ================================== BANK ======================================
case R.id.navigation_bank:
if (isEditing)
_id = banks.get(editIndex).getId();
else
_id = db.collection("bank").document(Objects.requireNonNull(fAuth.getUid())).collection("banks").document().getId();

_bank = new Bank(_id, concept, date, new Date(sdf.parse(date).getTime()));
db.collection("bank").document(Objects.requireNonNull(fAuth.getUid())).collection("banks").document(_id).set(_bank);
break;



// ===================================== SAVING =================================
case R.id.navigation_savings:
if (isEditing)
_id = savings.get(editIndex).getId();
else
_id = db.collection("saving").document(Objects.requireNonNull(fAuth.getUid())).collection("savings").document().getId();

_saving = new Saving(_id, concept, date, new Date(sdf.parse(date).getTime()));
db.collection("saving").document(Objects.requireNonNull(fAuth.getUid())).collection("savings").document(_id).set(_saving);
break;
}

populateListView(pefis_option);
}
}


Comments

Popular posts from this blog

Android/Java: Crear un SplashScreen para nuestra aplicación

Android/Java: Video de fondo en nuestro Login

Android/Kotlin: Video de fondo en nuestro Login