Dialog context issue - Android - java

I have created a dialog box like so using a custom layout:
dialog = new Dialog(FetchMenu.this, R.style.CustomDialogTheme);
dialog.setContentView(R.layout.custom_dialog_iab);
dialog.show();
I am now trying to edit a textbox within 'layout.custom_dialog_iab' for example:
TextView text = (TextView) findViewById(R.id.all_topics_unlock_button);
text.setText("Purchased");
My Question: How do I get the right context to be able to edit the textboxes?
P.S. I have tried 'dialog.getContext()' but still keep throwing null pointers?

You need to use:
TextView text = (TextView) dialog.findViewById(R.id.all_topics_unlock_button);
Note the dialog. in front of findViewById
regular findViewById() will search your activity layout rather than the dialog layout.

Related

Text output on an AndroidApp

I'm trying to program an android App with AndroidStudio. It is like an calculator and i don't know how to print the answer on my screen. I tried it with System.out.println() but it doesn't work Log._() doesn't work too.
You can display a temporary pop-up message to the screen using a Toast as below:
Toast.makeText(context,"<Message here>", Toast.LENGTH_LONG).show();
If you want the text to persist, then use a TextView object. Add it to your .xml for that activity:
<TextView
android:id="#+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Then declare it in your Activity screen:
TextView myTextView = (TextView) findViewById(R.id.myTextView);
Then set the text using .setText(), probably in the onClick() method of whatever button you're using to calculate the result for the user:
myTextView.setText(calculatorResult);

Android Studio - DatePicker in AlertDialog, null object reference

I have an activity in my app, where I want the user to pick the date from a DatePicker, which is contained in an AlertDialog. In the AlertDialog, I have set a view, to an xml layout file (, which contains only a LinearLayout, with a single DatePicker.
The code is really simple, and looks like this, just below onCreate().
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(R.layout.activity_alertdialog_date);
DatePicker datePicker = (DatePicker) findViewById(R.id.Activity_AlertDialog_SetStartDate);
// ... The rest of the AlertDialog, with buttons and all that stuff
alert.create().show()
The layout shows up in the AlertDialog, and that part works great.
However, when I try to add this line, I get a null object reference error.
datePicker.setMinDate(System.currentTimeMillis() - 1000);
Here is the error message.
Attempt to invoke virtual method 'void
android.widget.DatePicker.setMinDate(long)' on a null object reference
How can I fix this, or improve my code in another way?
I really appreciate all the help I can get. Thanks!
Your problem is that your findViewById is looking in the wrong place for the DatePicker view. Calling findViewById in an activity will call it on the Activity's layout hierarchy, not the dialog's layout. You need to inflate the layout for the alert dialog first, and then get a reference to the view. This can be acheived in a couple ways.
Probably the easiest is to inflate the view and get the reference before showing the dialog:
View dialogView = LayoutInflater.from(this).inflate(R.layout.activity_alertdialog_date, false);
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.Activity_AlertDialog_SetStartDate);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(dialogView);
// ... The rest of the AlertDialog, with buttons and all that stuff
alert.create().show();
You could also get the view from the alert dialog after it's been created:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(R.id.Activity_AlertDialog_SetStartDate);
// ... The rest of the AlertDialog, with buttons and all that stuff
AlertDialog dialog = alert.create();
dialog.show();
DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.Activity_AlertDialog_SetStartDate);

Can't access to a spinner in Java

I've got an activity where there is a button which opens an AlertDialog.
My dialog works and I decided to add a layout to it which contains a spinner.
So I have 3 documents :
mainActivity.java : Its role is to open a Dialog
activity_main.xml
dialog_main.xml : The dialog's layout containing the spinner
I try to retrieve in mainActivity.java the spinner declared in dialog_main.xml (in order to add it an adapter) :
Spinner mySpinner = (Spinner)findViewById(R.id.mySpinner);
However mySpinner = NULL, I can't find mySpinner. What is the problem ?
findViewById
as a method of the Activity class finds Views in your Activity's layout. When you are showing a Dialog, it is not part of your layout, so you have to findViewById in the Dialog's layout.
You are probably inflating a View for the Dialog that you show, you cou can use this view to find your Spinnner.
It probably looks like
View view = layoutInflater.inflate (...);
then you do
view.findViewById(...);
Post the code of how how show the Dialog and I'll show you if you can't do it.

Can't get focus on editText when children are added in layout

i am adding a spinner, an image and an editText in a linearLayout everytime i click a button.
Now whenever i add this layout , editText shows blinking, means it has focus but the keyboard won't show up. Even i click on it, the keyboard won't show up. What most i can do is to click somewhere else and then back at the editText to make it show keyboard and proper focus.
I am using following code, how can i fix this bug.
viewHolder.title = (EditText) view.findViewById(R.id.AddNewDetail);
view.setTag(viewHolder);
layout.addView(view);
i think you should force the softkeyboard show.
((InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(editText,
InputMethodManager.SHOW_FORCED);
and close it
((InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(editText.getWindowToken(),
0);
and focus on edittext
editText.requestFocus();
May you have added the below line in for your activity in manifeast file
android:windowSoftInputMode="stateHidden"
try removing it or you can manaually use the requestfocus method to get focus. try the below method.
edittext.requestFocus();

Adding buttons on xml file other than main

I have two xml files in my application - main.xml and options.xml.
In both of them I use buttons. The problem is, that while interacting with the buttons in main.xml, I cannot do that with options.xml: if I write
Button b = (Button)findViewById(R.id.b1);
, b is going to be null. What is the cause of this problem and how do I fix it?
Thank you in advance.
You need to either inflate the options.xml or set it as content view:
setContentView(R.layout.options);
before you can use the views in that layout file.
It sounds like you want to be able to access both layouts so you should do something like this:
View view = LayoutInflater.from(context).inflate(R.layout.options, null);
Button b = (Button) view.findViewById(R.id.b1);

Categories

Resources