andEngine alertdialog error - java

error java.lang.RuntimeException: Can't create handler inside
thread that has not called Looper.prepare() =(
If I call showDialog (id) in GameActivity - works
But if I call activity.showDialog from another class - an error
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
Log.d("Dialog", "Dialog 1");
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Help");
alert.setMessage("Help");
WebView wv = new WebView(this);
wv.loadUrl("http:\\www.google.com");
wv.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
alert.setView(wv);
AlertDialog ALERT = alert.create();
return ALERT;
default:
return null;
}
}
I want to call a dialog with any other class
UPDATE:
new code activiti.runOnUiUpdate()
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
this.runOnUiThread(new Runnable() {
#Override
public void run() {
Log.d("Dialog", "Dialog 1");
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Help");
alert.setMessage("Help");
WebView wv = new WebView(this);
wv.loadUrl("http:\\www.google.com");
wv.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
alert.setView(wv);
AlertDialog aALERT = alert.create();
}
});
return ALERT;
default:
return null;
}
}

Make your dialog in the update thread method, example
activity.runOnUiThread(new Runnable(){
#Override
public void run() {
//code for dialog creation goes here
}
});
update any internal references from this to the activities context activity
also you could just dialog.show() at the end of creating it and make it show up after its created. Hope I helped.

Related

Hold the button for 2 seconds and then show an AlertDialog

I have a button and when I hold the button for 2 seconds, I want to show an AlertDialog.
This is what I have tried:
btn.setOnTouchListener(new OnSwipeTouchListener(getContext()) {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Helper.setTimeout(() -> {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Message");
builder.setMessage(text);
builder.show();
System.out.println("I am text");
}
}, 2000);
return super.onTouch(view, motionEvent);
}
}
My method setTimeout():
public static void setTimeout(Runnable runnable, int delay){
new Handler(Looper.getMainLooper()).postDelayed(runnable, delay);
}
My problem is that sometimes the AlertDialog shows up multiple times and I always get this warning:
W/InputEventReceiver: Attempted to finish an input event but the input
event receiver has already been disposed.
What am I doing wrong?
I don't know if there is a better solution. I have also tried It with
btn.setOnLongClickListener(v -> {
System.out.println("hold");
return true;
});
but that doesn't output anything.
You can try this
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Message");
builder.setMessage("hello");
builder.show();
}
}, 2000);
return false;
}
});
}
}

Why Can't I See The Soft Keyboard When I Inflate A Layout With A WebView?

I have a donation option on my app in the menu. When the user taps on that an AlertDialog.Builder appears that will display a layout with a webviewer on it. The page loads just fine in the WebViewer in the AlertDialog. However, when the webpage loads and there is a editable text view on the webpage the keyboard will not popup to allow users to enter in their dollar amount. Here is what I tried...
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setIcon(R.drawable.ic_baseline_payment_32);
adb.setTitle("Tip Developer");
View v = getLayoutInflater().inflate(R.layout.tip_developer_layout, null);
WebView wv = v.findViewById(R.id.tip_donation_webViewer);
WebSettings ws = wv.getSettings();
ws.setJavaScriptEnabled(true);
wv.loadUrl("");
wv.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
public void onPageFinished(WebView view, String url) {
wv.requestFocusFromTouch();
wv.requestFocus(View.FOCUS_DOWN);
}
});
wv.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus())
{
v.requestFocus();
}
break;
}
return false;
}
});
adb.setPositiveButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
adb.setView(v);
adb.create();
adb.show();
I appreciate the help!
Insert this code.
private void showSoftKeyBoard() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
Then, implement it in OnCLick.
wv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showSoftKeyBoard();
}
});
Add this line in your EditText.
android:inputType="number"
And insert this code.
#Override
public void onResume() {
super.onResume();
wv.setFocusableInTouchMode(true);
wv.requestFocus();
}

How do i pass information from an activity to an edittext in a custom alertdialog

I have made a custom alertdialog that has 2 Edittexts in it. I want to pass information to it from an existing database when clicking something.
this is the Custom alert dialog's class
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.add_job_dialouge, null);
builder.setView(view).setTitle("Add Job")
.setPositiveButton("add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if(isDouble(wageValue.getText().toString())){
String nameOjob = jobName.getText().toString();
Double valueOwage = Double.parseDouble(wageValue.getText().toString());
listener.applyTexts(nameOjob, valueOwage);
builder.create();
}else{
wageValue.setError("Wrong Format Error");
}
}
})
.setNeutralButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
jobName = view.findViewById(R.id.job_name_ETD);
wageValue = view.findViewById(R.id.wage_ETD);
//I want to pass information to these
//jobName.setText();
//wageValue.setText();
return builder.create();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener =(ExampleDialogueListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "must implement Example Dialogue Listener");
}
}
public interface ExampleDialogueListener{
void applyTexts(String jobname, Double wage);
}
boolean isDouble(String str) {
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
I want to pass info to the edittext through parameters, so that there's info on the edit text when I create the alertdialog
It looks like you're extending a DialogFragment.
You can pass data in the same as you would a regular Fragment.
Bundle args = new Bundle();
args.putString("arg_key","something");
f.setArguments(args);
Where f is the instance of your DialogFragment.
Inside your DialogFragment you can call getArguments() to get your bundle.

Webview Inside popup window android

Hi,
I have an issue as the above image shown, on the left there is a button inside a webview. When I click on button a popupwindow in android should be appeared. In that popup window i need a webview.
now its all working fine without the webview in popup window.
this popup window is invoked by javascript interface
public class AppJavaScriptProxy {
private Activity activity = null;
public AppJavaScriptProxy() {
}
#JavascriptInterface
public String showMessage(String footNoteNo) {
Integer footNoteNoInt = Integer.parseInt(footNoteNo);
footnote = myDbHelper.getFootnote(chapterNumber, footNoteNoInt);
try {
LayoutInflater inflater = (LayoutInflater) HomeActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.footnote_popup,
(ViewGroup) findViewById(R.id.popup_footnote));
popup = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);
popup.showAtLocation(layout, Gravity.CENTER, 0, 0);
txt_footnote=(TextView) layout.findViewById(R.id.text_footnote);
txt_footnote.setTypeface(malayalamfont);
popup_close = (ImageButton) layout.findViewById(R.id.btn_close_popup);
txt_footnote.setText(footnote);
final WebView footnoteWView = (WebView) layout.findViewById(R.id.footnotePopupWebview);
footnoteWView.getSettings().setJavaScriptEnabled(true);
footnoteWView.loadUrl("file:///android_asset/www/footNotePopup.html");
footnoteWView.clearCache(true);
footnoteWView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){ footnoteWView.loadUrl("javascript:getFootnote('" + footnote + "')");
}
});
popup_close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
}
catch (Exception e) {
e.printStackTrace();
};
}
}
please help
Try this.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title here");
WebView wv = new WebView(this);
wv.loadUrl("http:\\www.yourweb.com");
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert.show();
public class List extends ActionBarActivity implements OnClickListener {
LinearLayout layoutOfPopup;
PopupWindow popupMessage;
Button popupButton;
WebView popupText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
init();
popupInit();
public void init() {
popupButton = (Button) findViewById(R.id.textview1);
popupText = new WebView(this);
popupText.setBackgroundColor(Color.TRANSPARENT);
String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";
String summary = "Some text go here</body></html>";
popupText.loadData(String.format(text, summary), "text/html", "utf-8");
popupText.setVerticalScrollBarEnabled(true);
popupText.setHorizontalScrollBarEnabled(true);
insidePopupButton = new Button(this);
insidePopupButton.setText("(X)Close");
insidePopupButton.setBackgroundResource(R.drawable.myborder);
layoutOfPopup = new LinearLayout(this);
LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layoutOfPopup.addView(insidePopupButton, lpView);
layoutOfPopup.addView(popupText);
layoutOfPopup.setOrientation(1);
layoutOfPopup.setBackgroundColor(Color.WHITE);
}
public void popupInit() {
popupButton.setOnClickListener(this);
insidePopupButton.setOnClickListener(this);
popupMessage = new PopupWindow(layoutOfPopup,
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
popupMessage.setContentView(layoutOfPopup);
}
#Override
public void onClick(View v) {
//Code goes here and to open the show as drop down
and dismiss
}

One of webview reload page after change screen orientation

First code:
package com.fshare.zsee;
public class ZSEEActivity extends TabActivity {
private WebView webview ;
private WebView webviewtwo;
private TabHost mTabHost;
private WebSettings webviewtwoSettings;
private int error;
protected void onStart() {
super.onStart();
// The activity is about to become visible.
}
protected void onStop() {
super.onStop();
// The activity is about to become visible.
}
protected void onRestart() {
super.onRestart();
}
protected void onDestroy(){
super.onDestroy();
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Activity activity = this;
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Zastępstwa").setContent(R.id.tab1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Plan Lekcji").setContent(R.id.tab2));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("O programie").setContent(R.id.tab3));
webview = (WebView) findViewById(R.id.webView1);
webviewtwo = (WebView) findViewById(R.id.webView2);
webviewtwoSettings = webviewtwo.getSettings();
if (savedInstanceState != null){
error = savedInstanceState.getInt("webtwoerror");
webview.restoreState(savedInstanceState.getBundle("stateone"));
webviewtwo.restoreState(savedInstanceState.getBundle("statetwo"));
if(error == 1){
webviewtwoSettings.setTextSize(TextSize.NORMAL);
}
else{
webviewtwoSettings.setTextSize(TextSize.LARGER);
}
mTabHost.setCurrentTab(savedInstanceState.getInt("CURRENT_TAB"));
}
else{
webviewtwoSettings.setTextSize(TextSize.LARGER);
webview.loadUrl("http://zsee.bytom.pl/ogloszenia.php");
webviewtwo.loadUrl("http://zsee.bytom.pl/plannew/index.html");
//error = 0 ;
mTabHost.setCurrentTab(0);
}
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
String summary = "<html><body><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ><center>Coś się zepsuło :(</center></body></html>";
webview.loadData(summary, "text/html","utf-8");
Toast.makeText(activity, "O nie! " + description, Toast.LENGTH_SHORT).show();
}
});
webviewtwo.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webviewtwoSettings.setTextSize(TextSize.NORMAL);
String summary = "<html><body><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ><center>Coś się zepsuło :(</center></body></html>";
webviewtwo.loadData(summary, "text/html","utf-8");
Toast.makeText(activity, "O nie! " + description, Toast.LENGTH_SHORT).show();
error = 1 ;
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// put your stuff here or just block the back button for perticular activity
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
}
return super.onKeyDown(keyCode, event);
}
protected void onSaveInstanceState(Bundle outState) {
Bundle outStateone = new Bundle();
Bundle outStatetwo = new Bundle();
webview.saveState(outStateone);
webviewtwo.saveState(outStatetwo);
outState.putBundle("stateone", outStateone);
outState.putBundle("statetwo", outStatetwo);
outState.putInt("CURRENT_TAB", mTabHost.getCurrentTab());
outState.putInt("webtwoerror", error);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.item1:
final AlertDialog alertdialog= new AlertDialog.Builder(this).create();
alertdialog.setTitle("O Programie");
alertdialog.setMessage("Zmiany w 1.0.1: \n-Obsługa planu z dnia 17.10.2011\n-Drobne Poprawki");
alertdialog.setButton("Fajno", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
alertdialog.cancel();
}
});
alertdialog.show();
return true;
case R.id.item2:
finish();
case R.id.item3:
if(mTabHost.getCurrentTab() == 0){
webview.loadUrl("http://zsee.bytom.pl/ogloszenia.php");
}
else if(mTabHost.getCurrentTab() == 1)
{
error = 0 ;
webviewtwo.loadUrl("http://zsee.bytom.pl/plannew/index.html");
webviewtwoSettings.setTextSize(TextSize.LARGER);
}
default:
return super.onOptionsItemSelected(item);
}
}
}
And now very simple question. Why webviewtwo reload page after change orientation and webview (webView1) doesen't ? and how to prevent it? Now only webView1 doesen't reload page after change screen orientation.
Sierran
I am not sure why the first one doesn't change but adding this to my manifest inside of the activity tag worked for me
android:configChanges="orientation|keyboardHidden|keyboard"

Categories

Resources