Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for an input text field. #28

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
import androidx.annotation.StyleRes;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;
import androidx.appcompat.app.AppCompatDialog;
import androidx.cardview.widget.CardView;

import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
Expand All @@ -32,6 +35,7 @@
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
Expand Down Expand Up @@ -81,6 +85,7 @@ public enum CFAlertActionAlignment {
private TextView dialogTitleTextView, dialogMessageTextView;
private ImageView cfDialogIconImageView;
private ScrollView cfDialogScrollView;
private EditText inputEditText;

// endregion

Expand Down Expand Up @@ -174,6 +179,7 @@ private void bindCardSubviews() {
buttonContainerLinearLayout = (LinearLayout) dialogCardView.findViewById(R.id.alert_buttons_container);
cfDialogFooterLinearLayout = (LinearLayout) dialogCardView.findViewById(R.id.alert_footer_container);
selectableItemsContainer = (LinearLayout) dialogCardView.findViewById(R.id.alert_selection_items_container);
inputEditText = dialogCardView.findViewById(R.id.tv_dialog_input);
}

private void populateCardView() {
Expand Down Expand Up @@ -202,6 +208,15 @@ private void populateCardView() {
// Buttons
populateButtons(params.context, params.buttons);

// Input text
inputEditText.setVisibility(params.showInput ? View.VISIBLE : View.GONE);
if(params.inputTint != -1)
DrawableCompat.setTint(inputEditText.getBackground(), params.inputTint);
if(params.inputHint != null)
inputEditText.setHint(params.inputHint);
if(params.inputType != null)
inputEditText.setInputType(params.inputType);

// Text gravity
setTextGravity(params.textGravity);

Expand Down Expand Up @@ -275,6 +290,10 @@ public void show() {
startPresentAnimation();
}

public String getInputText() {
return inputEditText.getText().toString();
}

@Override
public void dismiss() {

Expand Down Expand Up @@ -1061,6 +1080,26 @@ public Builder setAutoDismissAfter(long duration) {
return this;
}

public Builder setInputHint(@StringRes int hint) {
this.params.inputHint = hint;
return this;
}

public Builder showInput(boolean show) {
this.params.showInput = show;
return this;
}

public Builder setInputTint(@ColorInt int tintColor) {
this.params.inputTint = tintColor;
return this;
}

public Builder setInputType(int type) {
this.params.inputType = type;
return this;
}

public CFAlertDialog create() {
CFAlertDialog cfAlertDialog;
if (params.theme == 0) {
Expand All @@ -1083,6 +1122,7 @@ public CFAlertDialog show() {

private static class DialogParams {


private Context context;
private @ColorInt int backgroundColor = Color.parseColor("#B3000000");
private @ColorInt int dialogBackgroundColor = Color.parseColor("#FFFFFF");
Expand All @@ -1091,9 +1131,9 @@ private static class DialogParams {
private CharSequence message, title;
private @ColorInt int textColor = -1;
private int theme = R.style.CFDialog,
textGravity = Gravity.LEFT,
iconDrawableId = -1,
contentImageDrawableId = -1;
textGravity = Gravity.LEFT,
iconDrawableId = -1,
contentImageDrawableId = -1;
private CFAlertStyle dialogStyle = CFAlertStyle.ALERT;
private View headerView, footerView;
private int headerViewId = -1, footerViewId = -1;
Expand All @@ -1110,6 +1150,10 @@ private static class DialogParams {
private OnClickListener onSingleItemClickListener;
private OnMultiChoiceClickListener onMultiChoiceClickListener;
private long autoDismissDuration = -1;
private Integer inputHint;
private boolean showInput;
private @ColorInt int inputTint = Color.parseColor("#B3000000");
private Integer inputType;

public boolean isDialogBodyEmpty() {
if (!TextUtils.isEmpty(title)) return false;
Expand All @@ -1123,8 +1167,8 @@ public boolean isDialogBodyEmpty() {
return true;
}
}

private static class CFAlertActionButton {

private Context context;
private String buttonText;
private DialogInterface.OnClickListener onClickListener;
Expand Down
7 changes: 7 additions & 0 deletions cfalertdialog/src/main/res/layout/cfalert_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@
android:animateLayoutChanges="true"
tools:text="This is your description" />

<!-- Input item -->
<EditText
android:id="@+id/tv_dialog_input"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<!--Selection items-->
<LinearLayout
android:id="@+id/alert_selection_items_container"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.core.view.ViewCompat;
import androidx.appcompat.app.AppCompatActivity;

import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
Expand Down Expand Up @@ -37,7 +39,7 @@ public class StartActivity extends AppCompatActivity implements SampleFooterView
singleChoiceRadioButton;
private RadioButton textGravityLeft, textGravityCenter, textGravityRight;
private RadioButton buttonGravityLeft, buttonGravityRight, buttonGravityCenter, buttonGravityFull;
private CheckBox showTitleIcon;
private CheckBox showTitleIcon, showInputBox;
private RadioButton topDialogGravityRadioButton, centerDialogGravityRadioButton, bottomDialogGravityRadioButton;
private View selectedBackgroundColorView, selectBackgroundColorContainer;
private FloatingActionButton showDialogFab;
Expand Down Expand Up @@ -140,6 +142,12 @@ private void showCFDialog() {
builder.setIcon(R.drawable.icon_drawable);
}

// Input box
builder.showInput(showInputBox.isChecked());
builder.setInputHint(R.string.inputHint);
builder.setInputTint(getResources().getColor(R.color.colorAccent));
builder.setInputType(InputType.TYPE_CLASS_NUMBER);

// Buttons
if (positiveButtonCheckbox.isChecked()) {

Expand Down Expand Up @@ -303,6 +311,7 @@ private void bindViews() {
singleChoiceRadioButton = (RadioButton) findViewById(R.id.single_choice_items_radio_button);

showTitleIcon = (CheckBox) findViewById(R.id.show_title_icon);
showInputBox = (CheckBox) findViewById(R.id.show_input_box);

topDialogGravityRadioButton = (RadioButton) findViewById(R.id.top_dialog_gravity_radio_button);
centerDialogGravityRadioButton = (RadioButton) findViewById(R.id.center_dialog_gravity_radio_button);
Expand Down
6 changes: 6 additions & 0 deletions cfalertdialogdemo/src/main/res/layout/activity_start.xml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@
android:layout_height="wrap_content"
android:checked="true"
android:text="Add Footer" />

<CheckBox
android:id="@+id/show_input_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Input Box" />
</LinearLayout>

<TextView
Expand Down
1 change: 1 addition & 0 deletions cfalertdialogdemo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<string name="app_name">CFAlertDialogDemo</string>

<string name="btn_sample_button">Sample Button</string>
<string name="inputHint">Test Hint</string>

<string-array name="cfdialog_text_gravity">
<item>Left</item>
Expand Down