1) PROBLEM:
I have an EditText in which the user has to enter USSD code.
Problem is, for entering USSD code the user has to switch to symbol keyboard (two to three times) which creates a very bad user experience.
USSD Code Examples: *345*77#, *333*25#, *123*678# etc.
<EditText
android:id="#+id/field_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeActionId="#integer/imo_action_search"
android:imeOptions="actionSearch"
android:inputType="phone" />
2) What I need:
How can I replace the highlighted buttons (below image) with * and # buttons without using a custom keyboard?
3) What I have tried!!!
1) All InputTypes for EditText.
2) Google the issue, the only solution I have found is a custom keyboard but I want to know is there any simple solution.
Each keyboard app has its own layout and you can't change it. For example android:inputType="phone" has different layouts on Gboard and SwiftKey.
Solution:
You should implement a custom InAppKeyboard and show it to the user instead of the system keyboard."Creating an In-App Keyboard for your Android Apps" is a good tutorial that describes how to develop a custom InAppKeyboard like this but you can design your desired layout and use it in your app easily.
Update:
For Using InAppKeyboard with dialog, I have an idea. I developed a custom DialogFragment which has 2 parts: 1. The dialog content part2. The custom keyboard part named CustomKeyboardDialog.
You can extend this class and create your custom dialog. Both setContentView and findViewById methods are available and you can use them to manage your dialog UI. You must override onCreate method in your custom dialog implementation and call setContentView there. Then override onCreateView method, call findViewById there and find your EditText and attach it custom keyboard by calling attachToCustomKeyboard method.
CustomKeyboardDialog.java
public abstract class CustomKeyboardDialog extends DialogFragment {
private View mRootView;
private int mContentLayoutResID;
private View mContentView;
private CustomKeyboardView mCustomKeyboardView;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
#Nullable
#Override
final public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
mRootView = inflater.inflate(R.layout.dialog_custom_keyboard, container, false);
ViewGroup contentViewContainer = mRootView.findViewById(R.id.content_view_container);
mContentView = inflater.inflate(mContentLayoutResID, contentViewContainer, true);
this.mCustomKeyboardView = mRootView.findViewById(R.id.keyboard_view);
this.onCreateView(mContentView);
return mRootView;
}
public View findViewById(int id) {
return mContentView.findViewById(id);
}
public void setContentView(#LayoutRes int layoutResID) {
this.mContentLayoutResID = layoutResID;
}
public void showKeyboard(InputConnection inputConnection) {
mCustomKeyboardView.setInputConnection(inputConnection);
mCustomKeyboardView.setVisibility(View.VISIBLE);
mCustomKeyboardView.animate().translationY(0).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
updateContentViewSize(true);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
}
public void hideKeyboard() {
updateContentViewSize(false);
mCustomKeyboardView.animate().translationY(mCustomKeyboardView.getMeasuredHeight()).setListener(null);
}
private void updateContentViewSize(boolean keyboardVisible) {
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) mContentView.getLayoutParams();
if(keyboardVisible) {
layoutParams.bottomToTop = R.id.keyboard_view;
layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.UNSET;
} else {
layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.bottomToTop = ConstraintLayout.LayoutParams.UNSET;
}
mContentView.setLayoutParams(layoutParams);
}
public void onCreateView(View contentView) {
}
#Override
public void onResume() {
super.onResume();
getDialog().setOnKeyListener(new DialogInterface.OnKeyListener()
{
#Override
public boolean onKey(android.content.DialogInterface dialog, int keyCode,android.view.KeyEvent event) {
if ((keyCode == android.view.KeyEvent.KEYCODE_BACK))
{
if(mCustomKeyboardView.getVisibility() == View.VISIBLE)
hideKeyboard();
return true;
}
else
return false;
}
});
}
#Override
public void onPause() {
super.onPause();
getDialog().setOnKeyListener(null);
}
public void attachToCustomKeyboard(EditText editText) {
editText.setShowSoftInputOnFocus(false);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
showKeyboard(editText.onCreateInputConnection(new EditorInfo()));
}
}
});
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showKeyboard(editText.onCreateInputConnection(new EditorInfo()));
}
});
}
public void detachFromCustomKeyboard(EditText editText) {
editText.setShowSoftInputOnFocus(true);
editText.setOnFocusChangeListener(null);
editText.setOnClickListener(null);
}
}
dialog_custom_keyboard.xml
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout
android:id="#+id/content_view_container"
android:background="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/keyboard_view"
android:orientation="vertical"
android:gravity="center">
</LinearLayout>
<mirm.test.testapp.dialog.CustomKeyboardView
android:id="#+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#eee"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
CustomKeyboardView.java
public class CustomKeyboardView extends LinearLayout implements View.OnClickListener {
private Button button1, button2, button3, button4,
button5, button6, button7, button8,
button9, button0, buttonDelete, buttonEnter, buttonSharp, buttonStar;
private SparseArray<String> keyValues = new SparseArray<>();
private InputConnection inputConnection;
public CustomKeyboardView(Context context) {
this(context, null, 0);
}
public CustomKeyboardView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomKeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
setOrientation(VERTICAL);
LayoutInflater.from(context).inflate(R.layout.custom_keyboard_layout, this, true);
button1 = (Button) findViewById(R.id.button_1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button_2);
button2.setOnClickListener(this);
button3 = (Button) findViewById(R.id.button_3);
button3.setOnClickListener(this);
button4 = (Button) findViewById(R.id.button_4);
button4.setOnClickListener(this);
button5 = (Button) findViewById(R.id.button_5);
button5.setOnClickListener(this);
button6 = (Button) findViewById(R.id.button_6);
button6.setOnClickListener(this);
button7 = (Button) findViewById(R.id.button_7);
button7.setOnClickListener(this);
button8 = (Button) findViewById(R.id.button_8);
button8.setOnClickListener(this);
button9 = (Button) findViewById(R.id.button_9);
button9.setOnClickListener(this);
button0 = (Button) findViewById(R.id.button_0);
button0.setOnClickListener(this);
buttonDelete = (Button) findViewById(R.id.button_delete);
buttonDelete.setOnClickListener(this);
buttonEnter = (Button) findViewById(R.id.button_enter);
buttonEnter.setOnClickListener(this);
buttonSharp = (Button) findViewById(R.id.button_sharp);
buttonSharp.setOnClickListener(this);
buttonStar = (Button) findViewById(R.id.button_star);
buttonStar.setOnClickListener(this);
keyValues.put(R.id.button_1, "1");
keyValues.put(R.id.button_2, "2");
keyValues.put(R.id.button_3, "3");
keyValues.put(R.id.button_4, "4");
keyValues.put(R.id.button_5, "5");
keyValues.put(R.id.button_6, "6");
keyValues.put(R.id.button_7, "7");
keyValues.put(R.id.button_8, "8");
keyValues.put(R.id.button_9, "9");
keyValues.put(R.id.button_0, "0");
keyValues.put(R.id.button_enter, "\n");
keyValues.put(R.id.button_sharp, "#");
keyValues.put(R.id.button_star, "*");
}
#Override
public void onClick(View view) {
if (inputConnection == null)
return;
if (view.getId() == R.id.button_delete) {
CharSequence selectedText = inputConnection.getSelectedText(0);
if (TextUtils.isEmpty(selectedText)) {
inputConnection.deleteSurroundingText(1, 0);
} else {
inputConnection.commitText("", 1);
}
} else {
String value = keyValues.get(view.getId());
inputConnection.commitText(value, 1);
}
}
public void setInputConnection(InputConnection ic) {
inputConnection = ic;
}
}
custom_keyboard_layout.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"/>
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"/>
<Button
android:id="#+id/button_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"/>
<Button
android:id="#+id/button_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"/>
<Button
android:id="#+id/button_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"/>
<Button
android:id="#+id/button_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"/>
<Button
android:id="#+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"/>
<Button
android:id="#+id/button_9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"/>
<Button
android:id="#+id/button_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_sharp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#"/>
<Button
android:id="#+id/button_star"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"/>
<Button
android:id="#+id/button_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.3"
android:text="Delete"/>
<Button
android:id="#+id/button_enter"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.7"
android:text="Enter"/>
</LinearLayout>
</merge>
Here is an example of custom dialog implementation:
SampleDialog.java
public class SampleDialog extends CustomKeyboardDialog {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_dialog);
}
#Nullable
#Override
public void onCreateView(View contentView) {
EditText editText = (EditText) findViewById(R.id.edittext);
attachToCustomKeyboard(editText);
}
}
sample_dialog.xm
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:background="#eee">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="32dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Code"></TextView>
<EditText
android:id="#+id/edittext"
android:hint="example: *455*415#"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="Send"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
and it looks like this:
Related
I have faced an issue radio check change listener not working in the android fragment, I tried this within an activity it will work fine, while I convert into fragment it will give no response, please help me, here is my code.
K06_Away.java
public class K06_Away extends Fragment {
private View kf06_view;
protected Typeface tfLatoBold,tfLatoMedium,tfLatoRegular;
private Button button_kf06_back,button_kf06_next;
private TextView txtVw_kf06_resident_away;
private EditText edTxt_kf06_visiting_lastname;
private RadioGroup radioGp_kf06_resident_away;
List<RadioButton> radioButtons = new ArrayList<RadioButton>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
kf06_view = inflater.inflate(R.layout.k06_away, container, false);
return kf06_view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
configViews();
}
private void configViews() {
button_kf06_back = (Button) kf06_view.findViewById(R.id.button_kf06_back);
button_kf06_next = (Button) kf06_view.findViewById(R.id.button_kf06_next);
txtVw_kf06_resident_away= (TextView) kf06_view.findViewById(R.id.txtVw_kf06_resident_away);
radioGp_kf06_resident_away = (RadioGroup) kf06_view.findViewById(R.id.radioGp_kf06_resident_away);
radioButtons.add( (RadioButton)kf06_view.findViewById(R.id.radio_kf06_1_2_hours) );
radioButtons.add( (RadioButton)kf06_view.findViewById(R.id.radio_kf06_halfday) );
radioButtons.add( (RadioButton)kf06_view.findViewById(R.id.radio_kf06_allday) );
radioButtons.add( (RadioButton)kf06_view.findViewById(R.id.radio_kf06_moreday) );
configClickListeners();
radioButtonAction();
}
private void configClickListeners() {
button_kf06_back.setOnClickListener(this);
button_kf06_next.setOnClickListener(this);
}
private void radioButtonAction(){
for (RadioButton button : radioButtons){
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) processRadioButtonClick(buttonView);
String radio_Text = buttonView.getText().toString();
int radio_Id = buttonView.getId();
System.out.println("Selected the Radio:"+radio_Text+", Radio-Id:"+radio_Id);
}
});
}
}
private void processRadioButtonClick(CompoundButton buttonView){
for (RadioButton button : radioButtons){
if (button != buttonView ) button.setChecked(false);
}
}
}
k06_away.xml
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/lLayout_kf06_resident_away"
android:id="#+id/radioGp_kf06_resident_away">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
tools:ignore="UselessParent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginLeft="150dp"
tools:ignore="RtlHardcoded">
<RadioButton
android:id="#+id/radio_kf06_1_2_hours"
style="#style/radionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_resident_away_hour_1"
android:layout_gravity="left"
android:checked="false"
android:textSize="25sp"
android:paddingStart="20dp"
android:layout_marginTop="150dp"
android:textColor="#color/colorWhite"
tools:ignore="NestedWeights,RtlSymmetry" />
<RadioButton
android:id="#+id/radio_kf06_halfday"
style="#style/radionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_resident_away_hour_2"
android:layout_gravity="left"
android:checked="false"
android:textSize="25sp"
android:textColor="#color/colorWhite"
android:paddingStart="20dp"
android:layout_marginTop="50dp"
tools:ignore="NestedWeights,RtlSymmetry"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginLeft="50dp"
tools:ignore="RtlHardcoded">
<RadioButton
android:id="#+id/radio_kf06_allday"
style="#style/radionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_resident_away_hour_3"
android:layout_gravity="left"
android:checked="false"
android:textSize="25sp"
android:textColor="#color/colorWhite"
android:paddingStart="20dp"
android:layout_marginTop="150dp"
tools:ignore="NestedWeights,RtlSymmetry"/>
<RadioButton
android:id="#+id/radio_kf06_moreday"
style="#style/radionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_resident_away_hour_4"
android:layout_gravity="left"
android:checked="false"
android:textSize="25sp"
android:textColor="#color/colorWhite"
android:paddingStart="20dp"
android:layout_marginTop="50dp"
tools:ignore="NestedWeights,RtlSymmetry,RtlHardcoded"/>
</LinearLayout>
</LinearLayout>
</RadioGroup>
1. Try to call method configViews() from onCreateView() and pass the view kf06_view as parameter.
2. Implement View.OnClickListener to handle button_kf06_back and button_kf06_next click events.
Update your K06_Away Fragment as below:
public class K06_Away extends Fragment implements View.OnClickListener {
protected Typeface tfLatoBold,tfLatoMedium,tfLatoRegular;
private Button button_kf06_back,button_kf06_next;
private TextView txtVw_kf06_resident_away;
private EditText edTxt_kf06_visiting_lastname;
private RadioGroup radioGp_kf06_resident_away;
List<RadioButton> radioButtons = new ArrayList<RadioButton>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View kf06_view = inflater.inflate(R.layout.k06_away, container, false);
// Init
configViews(kf06_view);
return kf06_view;
}
private void configViews(View view) {
button_kf06_back = (Button) view.findViewById(R.id.button_kf06_back);
button_kf06_next = (Button) view.findViewById(R.id.button_kf06_next);
txtVw_kf06_resident_away= (TextView) view.findViewById(R.id.txtVw_kf06_resident_away);
radioGp_kf06_resident_away = (RadioGroup) view.findViewById(R.id.radioGp_kf06_resident_away);
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_1_2_hours) );
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_halfday) );
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_allday) );
radioButtons.add( (RadioButton) view.findViewById(R.id.radio_kf06_moreday) );
configClickListeners();
radioButtonAction();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_kf06_back:
// Do something...
break;
case R.id.button_kf06_next:
// Do something...
break;
}
}
private void configClickListeners() {
button_kf06_back.setOnClickListener(this);
button_kf06_next.setOnClickListener(this);
}
private void radioButtonAction(){
for (RadioButton button : radioButtons){
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) processRadioButtonClick(buttonView);
String radio_Text = buttonView.getText().toString();
int radio_Id = buttonView.getId();
System.out.println("Selected the Radio:"+radio_Text+", Radio-Id:"+radio_Id);
}
});
}
}
private void processRadioButtonClick(CompoundButton buttonView){
for (RadioButton button : radioButtons){
if (button != buttonView ) button.setChecked(false);
}
}
}
RadioGroup Directly contain only RadioButtons,No other Layouts,if you add LinearLayout inside RadioGroup,it will not works as you expect.Only use RadioButtons as child of RadioGroup.
i have an issue related to customdialog.The Button in custom dialog has no function. setting_dialog.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:background="#color/exitdialog_background">
<TextView
android:id="#+id/setting_title"
android:layout_height="32dp"
android:layout_width="match_parent"
android:text="Setting"
android:textSize="20dp"
android:textColor="#color/While"
android:paddingTop="4dp"
android:background="#ff40c4ff"
android:typeface="sans"
android:textStyle="bold"
android:gravity="center" />
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_below="#+id/setting_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp">
<Switch
android:id="#+id/volume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:checked="true"
android:textSize="10sp" />
<TextView
android:id="#+id/txtVolume"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:text="Volume"
android:textColor="#color/While"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayoutBtns"
android:layout_below="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp">
<Button
android:id="#+id/btnSave"
android:layout_width="125dp"
android:layout_height="35dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:gravity="center"
android:text="Save"
android:textColor="#color/While"
android:textSize="14dp"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
Setting class
public class SettingDialog extends Dialog implements View.OnClickListener
{
private Activity c;
private Button btnSave;
private TextView txtTitle;
private int layoutResID;
private OnSettingDialogClickListener mSaveClickListener;
public static interface OnSettingDialogClickListener {
public void onClick(SettingDialog settingDialog);
}
public SettingDialog(Activity a, int layoutResID) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
this.layoutResID = layoutResID;
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(layoutResID);
btnSave = (Button) findViewById(R.id.btnSave);
txtTitle = (TextView) findViewById(R.id.setting_title);
this.setCancelable(true);
this.setCanceledOnTouchOutside(false);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setCancelable(Boolean cancelable)
{
this.setCancelable(cancelable);
}
public void setTitle(String title) {
this.txtTitle.setText(title);
}
public SettingDialog setSaveClickListener(String textName, OnSettingDialogClickListener listener) {
mSaveClickListener = listener;
btnSave.setText(textName);
return this;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSave:
if (mSaveClickListener != null) {
mSaveClickListener.onClick(SettingDialog.this);
}
break;
default:
break;
}
}
}
Call Setting custom dialog
private void ShowSetting() {
SettingDialog settingDialog = new SettingDialog(PlayActivity.this, R.layout.setting_dialog);
settingDialog.setTitle("Setting");
settingDialog.setSaveClickListener("Save", new SettingDialog.OnSettingDialogClickListener() {
#Override
public void onClick(SettingDialog sDialog) {
LogHelper.d("PlayActivity", " Save button ");
sDialog.dismiss();
}
});
settingDialog.show();
}
Setting custom dialog apear and i touch save button but it has no function.
Could you please help me?
Woa, i've just found solution
Mising : btnSave.setOnClickListener(this);
I implemented my own DialogFragment with my custom layout, with two simple buttons. The problem is that I am not able to dismiss it. Here there is the code of the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:l
ayout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imgProfileQuestionSender"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/questionToAnswer"
android:layout_gravity="center_horizontal"
android:padding="10dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/yes"
android:id="#+id/yesButtonAnswer"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no"
android:id="#+id/noButtonAnswer"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
here there is the code of my Dialog
public class QuestionDialog extends DialogFragment implements View.OnClickListener {
public interface QuestionInterface{
void yesQuestionPressed(int idQuestion);
void noQuestionPressed(int idQuestion);
}
private int idQuestion;
private QuestionInterface mQuestionInterface;
//private DialogInterface.OnClickListener mOnclickListener;
private Button yesButton, noButton;
private ImageView profileImage;
private TextView question;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.layout_dialog_question, null);
noButton = (Button)view.findViewById(R.id.noButtonAnswer);
yesButton = (Button)view.findViewById(R.id.yesButtonAnswer);
profileImage = (ImageView)view.findViewById(R.id.imgProfileQuestionSender);
question = (TextView)view.findViewById(R.id.questionToAnswer);
question.setText("here there should be the text of the question retrived using the id of the question, also the image on the left should be the image of" +
"the friend that sent the question, the id of the question is " + String.valueOf(getArguments().getInt("questionId")));
Picasso.with(getActivity().getApplicationContext()).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(profileImage);
setCancelable(false);
return view;
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.yesButtonAnswer){
dismiss();
mQuestionInterface.yesQuestionPressed(getArguments().getInt("questionId"));
}
if(v.getId()==R.id.noButtonAnswer){
dismiss();
mQuestionInterface.noQuestionPressed(getArguments().getInt("questionId"));
}
}
#Override
public void onAttach(Activity activity){
super.onAttach(activity);
if(activity instanceof QuestionInterface) {
mQuestionInterface = (QuestionInterface) activity;
}
}
Directly set your Button Click Listener in onCreateView(...)
#Override
public View onCreateView(....)
{
.......
.......
negativeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
positiveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
.......
}
This is the new code, It doesn't throw exceptions, but I can't see my custom control "Second". This is the new code:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/L1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<Button
android:id="#+id/addButton"
android:layout_width="74dp"
android:layout_height="wrap_content"
android:text="Add" />
<EditText
android:id="#+id/newEditText"
android:layout_width="174dp"
android:layout_height="wrap_content"
android:layout_weight="3.24"
android:ems="10"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:id="#+id/List"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
This is the custom control: second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/secodView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/expandButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.44"
android:text = "Expand"/>
<TextView
android:id="#+id/txtView"
android:layout_width="253dp"
android:layout_height="wrap_content"
android:layout_weight="0.56"
android:ems="10"
android:text = "LOL"/>
</LinearLayout>
<RadioGroup
android:id="#+id/ratingRadioGroup"
android:layout_width="321dp"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/likeButton"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="Like" />
<RadioButton
android:id="#+id/dislikeButton"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:text="Dislike" />
</RadioGroup>
</LinearLayout>
Second.java class:
public class Second extends ViewGroup
{
private Button mExpandButton;
private RadioButton mLikeButton;
private RadioButton mDislikeButton;
private TextView mText;
private RadioGroup mLikeGroup;
private ViewGroup vGroup;
OnClickListener myClickListener = new OnClickListener()
{
#Override
public void onClick(View v)
{
String s= mExpandButton.getText().toString();
if (s.equals("One Click"))
mExpandButton.setText("Other Click");
else
mExpandButton.setText("One Click");
}
};
OnCheckedChangeListener myCkeckListener = new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if (mLikeButton.isChecked())
mText.setText("I Like it");
if (mDislikeButton.isChecked())
mText.setText("I Don't Like it");
}
};
public Second(Context context)
{
super(context);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = LayoutInflater.from(context).inflate(R.layout.second, this, true);
mText = (TextView)this.findViewById(R.id.txtView);
mExpandButton = (Button) this.findViewById(R.id.expandButton);
mLikeGroup = (RadioGroup)this.findViewById(R.id.ratingRadioGroup);
mExpandButton.setOnClickListener(myClickListener);
mLikeGroup.setOnCheckedChangeListener(myCkeckListener);
//inflater.inflate(R.layout.second, null);
}
#Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4)
{
// TODO Auto-generated method stub
}
}
ActivityMain.java class, where the activity gets started:
public class MainActivity extends Activity
{
protected LinearLayout mList;
protected EditText mEditText;
protected Button mButton;
Second[] sec;
boolean unavez;
OnClickListener myClickListener = new OnClickListener()
{
#Override
public void onClick(View v)
{
}
};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList = (LinearLayout)findViewById(R.id.List);
mEditText = (EditText)findViewById(R.id.newEditText);
mButton = (Button)findViewById(R.id.addButton);
sec = new Second[4];
unavez = true;
for (int i=0; i<4; i++)
sec[i]= new Second(this);
}
#Override
protected void onStart()
{
super.onStart();
}
#Override protected void onResume()
{
super.onResume();
if (!unavez)
return;
for (int i=0; i<4; i++)
{
mList.addView(sec[i]);
//setContentView(sec[i]);
}
unavez = false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
When running, I can see the Button and the EditText in activity_main.xml, but I can't see my custom control in second.xml. If I use the line setContentView(sec[i]) instead of mList.addView(sec[i]) in onResume(), it gets worse: the EditText and the Button in activity_main.xml aren't visible and I get a blank layout.
How can I add my custom control to activity_main.xml and make it visible to user?
You elected to have Second inherit directly from ViewGroup. Since you did that, Second needs to be a real ViewGroup, with a real onLayout() implementation and everything else that goes along with being a ViewGroup. Do not just randomly override methods with do-nothing implementations, then complain when the do-nothing implementations do nothing.
Creating "custom controls" is a relatively advanced topic in Android. Newcomers to Android should focus on other solutions to whatever problem they think a "custom control" will somehow solve.
I have a onCreateView method in my DialogFragment which looks like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.purchase_dialog, container, false);
int title = getArguments().getInt("title");
String titleString = getResources().getString(title);
getDialog().setTitle(titleString);
final TextView currentOfferPrice = (TextView) v.findViewById(R.id.current_offer_price);
final SeekBar editPurchasePrice = (SeekBar) v.findViewById(R.id.purchase_price);
editPurchasePrice
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
currentOfferPrice.setText("You offer $"
+ editPurchasePrice.getProgress());
}
});
editPurchasePrice.setMax(((Purchase)getActivity()).getTotal());
final DisplayHouse house = ((Purchase)getActivity()).getHouse();
editPurchasePrice.setProgress(house.getMarketPrice());
currentOfferPrice.setText("You offer $"
+ editPurchasePrice.getProgress());
final Button btnOffer = (Button) v.findViewById(R.id.btn_offer);
btnOffer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final DisplayHouse house = ((Purchase)getActivity()).getHouse();
Log.d("log", "current market price: " + house.getMarketPrice());
final int offerPrice = editPurchasePrice.getProgress();
if (offerPrice >= house.getMarketPrice() * 0.999) {
editPurchasePrice.setProgress(offerPrice);
((Purchase)getActivity()).showAcceptDialog();
PortfolioManager.addHousePortfolio(house, offerPrice);
Log.d("log", "added a new house to portfolio.");
} else {
editPurchasePrice.setProgress(offerPrice);
((Purchase)getActivity()).showRejectDialog();
}
getDialog().dismiss();
}
});
final Button btnCancel = (Button) v
.findViewById(R.id.btn_purchase_dialog_cancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getDialog().dismiss();
}
});
return v;
}
But strangely when I open the dialog it only displays the SeekBar. I couldn't see the TextViews and buttons. How can I get them to show?
My Layout file is like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/purchase_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SeekBar
android:id="#+id/purchase_price"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:inputType="number"
android:gravity="center_horizontal"
android:layout_alignParentRight="true">
</SeekBar>
<TextView
android:id="#+id/purchasing_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:textSize="20dip"
android:gravity="center_horizontal"
android:layout_toLeftOf="#id/purchase_price"
android:text="#string/purchasing_text"
/>
</RelativeLayout>
<TextView
android:id="#+id/current_offer_price"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="center_vertical|center_horizontal"
/>
<RelativeLayout
android:id="#+id/layout_buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_offer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/offer"/>
<Button
android:id="#+id/btn_purchase_dialog_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/btn_offer"
android:text="#string/cancel"/>
</RelativeLayout>
</LinearLayout>
I've tried your code and it seems that this
android:layout_height="fill_parent"
of the first RelativeLayout hides your other views.
I've tried changing it to 50dp and the dialog works fine.