I'm trying to make a custom NumberPicker to display in a DialogFragment. So far I've succeeded in getting the picker to display in a dialog fragment and getting it to display the custom strings I want it to. I've also disabled the descendantFocusability so the text is not editable. Here is an overview of the questions I have about NumberPicker behaviour, I'll go more in depth after:
How does one 'commit' their selection?
How to return the selected value?
How does one 'commit' their selection?
When the dialog appears, I don't see a clear way to 'select' an option (see image below). Looking at native Android selection dialogs, I often see radiobuttons. Is that the way to go? And am I using the wrong UI component to build this?
How to return the selected value?
This question is tightly knit with the last one, as not knowing how to commit a selection obviously doesn't help here. Right now I use NumberPicker.OnValueChangeListener to see if the value changed, however it never fires. Here's how I structured the code:
class PlatePickerFragment: DialogFragment() {
lateinit var listener: NumberPicker.OnValueChangeListener
//I set up the fragment with onCreateDialog here.
}
And this is the code I use when I create an instance:
val platePicker = PlatePickerFragment()
platePicker.listener = NumberPicker.OnValueChangeListener { numberPicker, i1, i2 ->
//set what to do on value change here.
}
However, this block never gets called.
TL;DR: Am I using the right UI component? If I am, how would I implement this in a way that it works? Why does the NumberPicker not have a cancel/ok section by default (see image of DatePicker below)? Thanks in advance!
Answer to first part :
This is the ideal way of implementing NumberPicker. One thing you can do
is add an OK button to side to catch selection.See screenshot
Code for same :
picker.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="16dp">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="5dp"
android:text="OK"
android:textSize="15dp" />
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv"
android:layout_marginTop="10dp" />
</RelativeLayout>
</LinearLayout>
In your activity :
final NumberPicker aNumberPicker = (NumberPicker) dialog.findViewById(R.id.numberPicker);
aNumberPicker.setMaxValue(12);
aNumberPicker.setMinValue(1);
aNumberPicker.setValue(1);
aNumberPicker.setFocusable(true);
aNumberPicker.setFocusableInTouchMode(true);
aNumberPicker.setOnScrollListener(new NumberPicker.OnScrollListener() {
#Override
public void onScrollStateChange(NumberPicker view, int scrollState) {
value = view.getValue();
}
});
aNumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
value = newVal;
}
});
TextView ok = (TextView) parent.findViewById(R.id.tv);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// value variable can be used here
}
});
Declare value as global variable.
Answer to the second part of your question :
int hour;
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(final NumberPicker numberPicker, final int i, final int i1) {
hour = Integer.valueOf(numberPicker.getDisplayedValues()[numberPicker.getValue()]);
}
});
On clicking of OK button you will have answer in hour variable.
Related
I'm making an android app and i want to put a settings button on every layout in the app. When i press the settings button, a custom dialog pops up and i can access the app settings.
The problem i'm having is that i want to refer to 1 method in some class (doesn't matter to me which one). I'm already using the include in my XML of my layouts like this:
<include android:id="#+id/settingsButton"
layout="#layout/settingsbuttonlayout"/>
The settingsbuttonlayout.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/root_vg">
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp" app:srcCompat="#drawable/settingsicon"
android:id="#+id/settings_dialog"
android:cropToPadding="true"
android:adjustViewBounds="false" android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.133"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.123"
android:clickable="true"
android:focusable="true"
android:background="#drawable/customdialog" android:onClick= "showSettings"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You can see that there is an onclick defined in this layout. However (for as far as i know) this means i need the same "showSettings" method in every layout class. How can i work around this so i should only write the "showSettings" method once and can refer to it?
This is the showSettings method:
public void showSettings(View v){
Dialog dialog = new Dialog(this, R.style.DialogStyle);
dialog.setContentView(R.layout.settings_dialog);
dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbackground);
Button btnClose = dialog.findViewById(R.id.close_settings);
btnClose.setOnClickListener(view -> dialog.dismiss());
dialog.show();}
PS: I'm pretty new into making apps and GUI's. I didn't learn it yet in school and i'm just figuring out everything myself so sorry if this is some straightforward or stupid question :)
you can remove the onClick attribute from your setttings_dialog which calls the showSettings, next create a Utility.java file in which you can make your function as static
public static void showSettings(View v){
Dialog dialog = new Dialog(this, R.style.DialogStyle);
dialog.setContentView(R.layout.settings_dialog);
dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbackground);
Button btnClose = dialog.findViewById(R.id.close_settings);
btnClose.setOnClickListener(view -> dialog.dismiss());
dialog.show();}
now in whichever class you want to call this method just write
Button settingsButton = (Button) findViewById(R.id.settingsButton);
settingsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Utility.showSettings(v);
}
});
After searching some more I found the following:
From whichever class i wanted to open the dialog i have to write this:
SettingsDialog.showSettings(this);
In my SettingsDialog class i have the following:
public class SettingsDialog {
static void showSettings(Context context) {
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.settings_dialog);
Dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbackground);
}
dialog.show();}
}
So this is another problem that has been brought up a million times, but I'm still doing something wrong. Using EditText.getText() is returning an empty string.
I'm doing this in a small custom dialog I've made. I'm building it with the AlertDialog Builder, which might be causing the issue? I really don't know at this point.
Some things I've tried/notes on what I know about the issue:
I'm checking for text in the OK button's click listener, so I'm not trying to get a value before there would be one, which was a common error I saw.
I have ID's set for the EditText objects in my XML and the debugger seems to show that I'm referencing them properly.
I've tried defining the EditText objects outside of the onCreateDialog method and that didn't change things (though I am curious which is better practice).
Using EditText.setText() before getText() will allow it to return the argument used in setText(), but it doesn't seem to be fetching a value input by the user.
Here is my custom Dialog Fragment:
public class GPSLocationDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.gps_dialog, null);
final EditText latitudeText = (EditText) view.findViewById(R.id.latitude);
final EditText longitudeText = (EditText) view.findViewById(R.id.longitude);
// Define the dialog
builder.setView(inflater.inflate(R.layout.gps_dialog, null))
.setMessage("Manually input a GPS address")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("myTag", "Text: " + latitudeText.getText()); // This prints ""
// These throw errors since they're trying to parse "" as a double
double latitude = Double.parseDouble(latitudeText.getText().toString());
double longitude = Double.parseDouble(longitudeText.getText().toString());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
Here is how I'm calling the Dialog from my Activity:
GPSLocationDialogFragment gpsDialog = new GPSLocationDialogFragment();
gpsDialog.show(getFragmentManager(), "GPSDialog");
And here is my layout .xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/longitude"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="#string/longitude"
android:inputType="numberSigned|numberDecimal" />
<EditText
android:id="#+id/latitude"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="#string/latitude"
android:inputType="numberSigned|numberDecimal" />
</LinearLayout>
If more context is necessary I can share it, I tried to simplify to just the relevant code.
Try changing this:
builder.setView(inflater.inflate(R.layout.gps_dialog, null))
to this:
builder.setView(view)
What's happening is that you inflate gps_dialog xml and turn its layout hierarchy into a View. Then you find the EditText's and a assign a reference to them.
However, by calling setView(inflater.inflate(R.layout.gps_dialog, null))
instead of passing the View that you already inflated, and whose child EditText's you have a reference to, to the dialog, what you are doing is inflating a new version of the gps_dialog.xml and passing that to the dialog. The references that you have are to EditText's that aren't on the screen.
I'm a novice android developper and was wondering:
Could I have 2 buttons to be linked into the same, 1 onClick method (which i'll presumably override to accept 2 extra parameter, int btnId and View targetTextView for instance) in order to decide which button is calling the method and then which TextView text to update?
For Example:
btn1 will update the text on text_view_1
and btn2 will update text_view_2.
Except they we will be linked to the same method:
public void generalOnClick(View view, String btnId, String textViewId){...}
<?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:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:onClick="btnClick"/>
<TextView
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two"
android:onClick="btnClick"/>
</LinearLayout>
Button Click Function in your Activity
public void btnClick(View view) {
TextView tv = (TextView)view;
int id = tv.getId();
if(id==R.id.one) {
tv.setText("One Clicked");
} else if(id==R.id.two){
tv.setText("Two Clicked");
}
}
set an tag to button and verify it with onClick method that which buttons click event has been triggered , if it doesn't work then follows following trick,
define an common method for the functionality which you are going to execute on button click, make it as common function.
define independent onclick event for both button and then while calling the common function which created above pass some unique param and verify it.
use following code:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener clickListener = new View.OnClickListener() {
public void onClick(View v) {
if(v.getId().Equals(btn1.getId())){
//do your work here
}
else{
//work for 2nd button
}
};
btn1 = (Button)findViewById(R.id.btn_1);
btn2 = (Button)findViewById(R.id.btn_2);
btn1.setOnClickListener(clickListener);
btn2.setOnClickListener(clickListener);
}
Problem scenario:
I have a view pager in which I have divided a form in different tabs.
Now one of the tabs looks like:
The XML for one row being:
<LinearLayout
android:id="#+id/email_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="lin_email_container">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#drawable/rounded_linear"
android:tag="email_sub_container"
android:weightSum="5">
<Spinner
android:id="#+id/email_spinner"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5" />
<View
android:id="#+id/view3"
android:layout_width="0.3dp"
android:layout_height="match_parent"
android:background="#D3D3D3" />
<EditText
android:id="#+id/edittext_email"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#null"
android:hint="Email"
android:inputType="textEmailAddress"
android:paddingLeft="10dp"
android:textSize="14sp" />
<ImageView
android:id="#+id/btn_emailadd"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight=".5"
android:src="#drawable/ic_action_add_to_queue" />
</LinearLayout>
</LinearLayout>
On press of the + icon at the extreme right, which is an ImageView with id = btn_emailadd I add one more similar control, its like the good old add more field and looks like this after added:
As is visible above, I added one email field.
The Problem
In order to get values from these dynamic EditTexts I need to implement a textwatcher for all. I am also implementing tags for views, but I am afraid I am not able to maintain unique tags while adding content. This is how I add content:
public void addView(LinearLayout container, String[] spin_array, String hint) {
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.dynamic_content, null);
Spinner spin_dynamic = (Spinner) addView.findViewById(R.id.email_spinner1);
EditText edt_dynamic = (EditText) addView.findViewById(R.id.edittext_email1);
ImageView remove_dynamic = (ImageView) addView.findViewById(R.id.btn_remove);
LinearLayout sub_layout = (LinearLayout) addView.findViewById(R.id.sub_layout);
edt_dynamic.setHint(hint);
setUpSpinners(spin_array, spin_dynamic);
edt_dynamic.setTag(container.getTag() + "edt_text" + container.getChildCount());
container.indexOfChild(edt_dynamic);
// get height from dimens
int height = (int) getResources().getDimension(R.dimen.lin_height);
// set this height
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
// we are only concerned about top margin here.
layoutParams.setMargins(0, (int) getResources().getDimension(R.dimen.topMargin), 0, 0);
container.addView(addView, 1, layoutParams);
remove_dynamic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout) addView.getParent()).removeView(addView);
}
});
}
So it melts down to, without having any solid Id's and without any solig tags, how can I implement textwatchers, that too to a dynamic content.
The easiest solution I could find to this problem is to use a Parent traversal mechanism, however this would not work in a View Pager, as there is no event I can run this code on:
public void traverseLinearLayout(LinearLayout layout) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof LinearLayout) {
traverseLinearLayout((LinearLayout) v);
}
if (v instanceof EditText) {
System.out.println(v.getTag());
//Get Edit Text value here
}
if (v instanceof Spinner) {
}
}
}
I see that you have tags on the Parent Layout:
android:id="#+id/email_container"
This solves half of your problem. Now make a generic Class which implements a TextWatcher:
public class GenericTextWatcher implements TextWatcher {
public View view;
public GenericTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//do nothing
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do nothing
}
public void afterTextChanged(Editable s) {
System.out.println(view.getTag());
}
}
When you create the view dynamically, you just need to know if the EditText is for email or for any other form field like: web, phone, messenger etc. You can use .contains or .equals methods for the same.
This is a bit hacky but now you can easily do something like:
edt_dynamic.setTag(container.getTag() + "edt_text" + new Date());
edt_dynamic.addTextChangedListener(new GenericTextWatcher(edt_dynamic));
Beware to replace new Date() with something having a nanosecond precision.
In the Textwatcher, you can add key value pairs to a Map, depending on the scenario. Make sure you remove the Key, Value (from the Map) when X for removing EditText is pressed.
I am trying to develop a Log in page, where I have Username, password and a button. When I click the button for the first time, nothing happens, but when I click the button for the second time, then it works properly. I am confused, why it is happening ?
activity_login.xml
<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/splash_bg"
tools:context=".LoginActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:layout_margin="30dp" >
<EditText
style="#style/EditText1"
android:id="#+id/userEditText"
android:layout_marginBottom="50dp"
android:inputType="textNoSuggestions"
android:singleLine="true"
android:hint="username" />
<EditText
style="#style/EditText1"
android:id="#+id/passEditText"
android:inputType="textPassword"
android:layout_marginBottom="50dp"
android:hint="password" />
<Spinner
android:id="#+id/locationSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:popupBackground="#ffffff"
style="#style/EditText1"
android:layout_marginBottom="50dp" />
<Button
style="#style/Button1"
android:focusable="true"
android:focusableInTouchMode="true"
android:onClick="onLoginClick"
android:text="continue"
/>
loginActivity.java
#SuppressLint("DefaultLocale")
public void onLoginClick(View view) {
String username = mUserEditText.getText().toString();
String password = mPassEditText.getText().toString();
String location = mLocationData.get(
mLocationSpinner.getSelectedItemPosition()).toLowerCase();
if (username.isEmpty() || password.isEmpty()) {
CreatorMessenger
.getInstance()
.showMessage(this, "Error!!",
"You need to enter username and password both to continue!!");
return;
}
User user;
user = new User(username);/*
* }
*/
user.setLocation(location);
AppManager.getInstance().setLoggedInUser(user);
APICaller.getInstance().login(username, password, location);
}
You are setting android:focusableInTouchMode="true"
Hence on 1st click it receives focus.
Refer Android button - How to set focusable to true and still accept onClick listener on first click?
Set this -
android:focusableInTouchMode="true"
to this -
android:focusableInTouchMode="false"
on button.
Explanation - If you'll make the button focusable then on first click the focus is passed to the button, then the click is passed on second touch. EditText is a focusable View which gains focus first and therefore other views must first regain the focus from EditText, unless they do not need focus to work, just like buttons. If you just need the OnClick function, then you don't need focus, so you can spre one extra click.
PS: Although it shouldn't require, but setting android:focusable to false will help too, if the first one doesn't work.
Yes Yes I got the answer, after a lot of RND, I got the solution, I just need to implement setOnClickListener(), and setOnFocusChangeListener(). So I am putting here the solution.
ActivityLogin.java
buttonLogin= (Button) findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("hello", "hellow");
String username = mUserEditText.getText().toString();
String password = mPassEditText.getText().toString();
String location = mLocationData.get(mLocationSpinner.getSelectedItemPosition()).toLowerCase();
if(username.isEmpty()||password.isEmpty()){
popbox();
return;
}
User user;
user = new User(username);
user.setLocation(location);
AppManager.getInstance().setLoggedInUser(user);
APICaller.getInstance().login(username, password, location);
}
});
buttonLogin.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.performClick();
}
}
});
}
activity_login.xml
<Button
android:id="#+id/buttonLogin"
style="#style/Button1"
android:text="continue"
/>
if in your xml in button, or img, or liner layout has:
android:focusableInTouchMode="true"
To resolve, just
remove this.
I suggested to use request focus in the "onCreate()" method.
When you using android:focusableInTouchMode="true" you may don't want the focus catch by an 'EditText' field.
For example:
1. An activity has a view contain -> android:focusableInTouchMode
2. You open a Fragment Dialog, but the back button need click two times to fire it.
3. You should call requestFocus() after the Fragment Dialog onCreateView()