I have some problem. Dialog.dismiss() does not work.
I want to input ip, username, password to login WinServer 2003. When I clicked Submit button, the dialog can't be closed. To be noted, my Thread-socket able to retrieve messages from Server and send messages back to Server. The Dialog can only be closed When the Thread-socket got error.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jiemian);
netInit();
JieMianActivity.jiemian = this;
LayoutInflater factory = LayoutInflater.from(JieMianActivity.this);
View view = factory.inflate(R.layout.login, null);
dialog02 = new AlertDialog.Builder(JieMianActivity.this)
.setIcon(android.R.drawable.btn_star)
.setTitle("login")
.setView(view)
.setPositiveButton("submit", onclickButton)
.setNegativeButton("cancel", onclickButton).create();
dialog02.show();
}
private OnClickListener onclickButton = new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.v("which", which+"");
switch(which){
case Dialog.BUTTON_POSITIVE:
dialog.dismiss();//doesn't work , cann't close dialog.
EditText ip = (EditText) findViewById(R.id.ip);
EditText username = (EditText) findViewById(R.id.username);
EditText password = (EditText) findViewById(R.id.password);
new Connect(JieMianActivity.jiemian).run();//do some socket thing
break;
case Dialog.BUTTON_NEGATIVE:
dialog.dismiss();
JieMianActivity.jiemian.finish();
break;
}
}
};
This is my Thread:
class Connect extends Thread{
private JieMianActivity jiemain;
public Connect(JieMianActivity jiemian){
this.jiemain = jiemian;
}
public void run(){
//Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
try {
Display display = getWindowManager().getDefaultDisplay();
InputStream is = getResources().getAssets().open(
connect2RDP.mapFile);
sfv = (SurfaceView) findViewById(R.id.surfaceView);
sfh = sfv.getHolder();
sfh.addCallback(JieMianActivity.jiemian);
if (conn.connect("192.168.10.134", "Adminstrator", "123",
display.getWidth(), display.getHeight(), 3389, is)) {
Log.v("login", "success");
//dialog02.dismiss();
Log.v("login", "ok");
canvas = new MyCanvas();
canvas.setRop(new RasterOp());
canvas.setHeight(Options.height);
canvas.setWidth(Options.width);
canvas.setRight(Options.width - 1);
canvas.setBottom(Options.height - 1);
canvas.setBackstore(new WrappedImage(Options.width,
Options.height, JieMianActivity.jiemian));
canvas.setJiemian(JieMianActivity.jiemian);
canvas.setSurView(sfv);
canvas.setSurHolder(sfh);
conn.doconnect(JieMianActivity.jiemian);// 启动RDP
// init();
}
} catch (OrderException e) {
} catch (Exception e) {
}
}
};
You need to change your code to start a Thread, You need to call the method start() - that will execute the run() method written in that Thread.
So,
Invoke Connect.start() instead of Connect.run() inside your onClick handler.
I think you should close your alert dialog in UI thread else it wont work. You can do this in two ways : 1. Use message handler 2. Use RunOnUiThread. Sample for your reference :
1.
messageHandler.sendEmptyMessageDelayed(unique_id, 500);
private Handler messageHandler = new Handler()
{
#Override
public void handleMessage(Message msg) {
switch(msg.what) {
case unique_id:
// do here
break;
}
}
};
2.
Runnable hide_ui = new Runnable() {
#Override
public void run() {
// do here
}
};
runOnUiThread(hide_ui);
Related
Hey every one has i am create rename application in android ,I will set a Edit-text box in set error message using without toast using Alert Dialog box
Sample Code :
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle(R.string.rename_title);
folderManager = new FolderManager(getActivity());
folderManager.open();
Cursor c = folderManager.queryAll(itemPos);
if (c.moveToFirst()) {
do {
Newnamefolder = c.getString(1);
} while (c.moveToNext());
}
// Set an EditText view to get user input
final EditText input = new EditText(getActivity());
input.setText(Newnamefolder);
alert.setView(input);
alert.setPositiveButton(R.string.rename_position_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Newnamefolder = input.getText().toString();
String Mesage_one = getResources().getString(R.string.folder_already_exit);
String Mesage_two = getResources().getString(R.string.types_minimum_eight_charcter);
String Mesage_three = getResources().getString(R.string.folder_empty);
String Matchnamerename = folderManager.getmatchfoldername(Newnamefolder);
if(Newnamefolder.equals(Matchnamerename))
{
input.setError(Mesage_one);
}
else if(Newnamefolder.length()>12)
{
input.setError(Mesage_two);
}
else if(Newnamefolder.equals(""))
{
input.setError(Mesage_three);
}
else
{
int newfolder = folderManager.update(itemPos,Newnamefolder);
reload();
}
}
});
alert.show();
But the problem once in click the OK button Don't show error message to exit the Alert Dialog box...
give me any solution ... Friends ?
You can extend Dialog and create your own dialog
public class CustomDialog extends Dialog implements View.OnClickListener {
private boolean success = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog);
Button positive = (Button) findViewById(R.id.button_positive);
Button negative = (Button) findViewById(R.id.button_negative);
EditText field = (EditText) findViewById(R.id.field);
positive.setOnClickListener(this);
negative.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button_positive:
onPositiveButtonClicked();
break;
case R.id.button_negative:
//onNegativeButtonClicked();
break;
}
}
private void onPositiveButtonClicked() {
if(verifyForm()) {
success = true;
dismiss();
}
}
public boolean isSuccess() {
return success;
}
private boolean verifyForm() {
boolean valid = true;
/* verify each field and setError() if not valid */
if(!TextUtils.isEmpty(field.getText())) { //or any other condition
valid = false;
field.setError("error message");
}
return valid;
}
}
You can show your CustomDialog like this
final CustomDialog customDialog = new CustomDialog();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
if(customDialog.isSuccess()) {
//update your folder manager
}
}
}
customDialog.show();
I have a Fragment. When the Fragment is created I want to show a Progress Dialog for 3 seconds, then dismiss it and show a pop up dialog. I attach below my code.
From the onCreate() of my Fragment:
final ProgressDialog myPd_ring=ProgressDialog.show(context, "Please wait", "text..", true);
myPd_ring.setCancelable(false);
new Thread(new Runnable() {
#Override
public void run() {
try
{
Thread.sleep(3000);
} catch(Exception e)
{
}
myPd_ring.dismiss();
}
}).start();
showPopup(0, 8, 1, "Next photo");
And my popup method:
public void showPopup(final int type, int photoNo, int currPhoto, String message) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.popup_erroare);
dialog.setCancelable(false);
TextView textHeader;
TextView textContent;
textHeader = (TextView) dialog.findViewById(R.id.text_titlu);
textContent = (TextView) dialog.findViewById(R.id.text_error);
textHeader.setText("Procedura fotografiere");
textContent.setText("Poza nr. " + currPhoto+ " of" + noPhoto+
". " + message);
if (type == 0) {
}
Button btn_nu = (Button) dialog.findViewById(R.id.button_nu);
if (type == 0) {
btn_nu.setText("NU");
}
btn_nu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
btn_nu.setVisibility(View.GONE);
Button btn_da = (Button) dialog.findViewById(R.id.button_da);
btn_da.setText("Fotografiere");
btn_da.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (type == 0) {
captureImage();
}
dialog.dismiss();
}
});
dialog.show();
}
The problem is that my ProgressDialog doesn't appear, the popup appears directly. If I put my pop up invoking method in the new Thread() body I get an error. It seems that you can invoke a dialog from a Runnable.
you can use the below code:
final ProgressDialog myPd_ring=ProgressDialog.show(context, "Please wait",
"text..", true);
myPd_ring.setCancelable(false);
myPd_ring.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
myPd_ring.dismiss();
showPopup(0, 8, 1, "Next photo");
}
}, 3000);
If you have any query please let me know.
Never use sleep() in final APP.
try this way, new a Runnable(), and call postDelay(runnable, delayTimes) on a Handle.
Android Handler has a handy method postDelay() that does what you need, for example:
final ProgressDialog myPd_ring=ProgressDialog.show(context, "Please wait", "text..", true);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
myPd_ring.dismiss();
// show popup
}
}, 3000);
In Android, all View related tasks must be executed in the main thread, doing it in other threads will result in error. If you want to do in inside a background task for example, you'll need to use myactivity.runOnUiThread()
runOnUiThread(new Runnable() {
#Override
public void run() {
// show my popup
}
});
I have a custom dialog with me. In which an editText id there, I am reading input through editText, putting the input to a string. This code is followed by a thread (sub) to handle one url. I want to use this string in the thread mentioned. But the thread is getting invoked before I type to the editText. How can i dynamically use the same text from the userinput inside the thread? Thanks in advance..
public void onClick(View v)
{
switch (v.getId())
{
case R.id.i1:
MyDevice.getInstance().currentUserImageId=R.drawable.jerry1;
MyDevice.getInstance().userName="You";
MyDevice.getInstance().facebook=0;
this.finish();
break;
case R.id.i2:
MyDevice.getInstance().currentUserImageId=R.drawable.chaplin1;
MyDevice.getInstance().userName="You";
MyDevice.getInstance().facebook=0;
this.finish();
break;
case R.id.i3:
MyDevice.getInstance().currentUserImageId=R.drawable.budy;
MyDevice.getInstance().userName="You";
MyDevice.getInstance().facebook=0;
this.finish();
break;
case R.id.facebook:
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.usernamefbdialog);
dialog.setTitle("Enter Facebook Username");
Button dialogButton = (Button) dialog.findViewById(R.id.done);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
EditText edit=(EditText)dialog.findViewById(R.id.username);
text=edit.getText().toString();
dialog.dismiss();
}
});
dialog.show();
Thread thread = new Thread(new Runnable()
{
#Override
public void run()
{
try
{
MyDevice.getInstance().bitmap=getUserPic(text);
MyDevice.getInstance().facebook=1;
ImageView facebookImg=(ImageView) findViewById(R.id.facebookimg);
facebookImg.setImageBitmap(MyDevice.getInstance().bitmap);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
thread.start();
break;
}
}
Basically what you want to do is run the Thread from within the button click event, that way it runs after you get the text. See your modified code below.
The events that happen to the String occur in this order.
1. Create the String
2. Set the String equal to the edit text String
3. Start the Thread and use the String
final String theStringYouWantToUseInTheThread = null;
Thread thread = new Thread(new Runnable()
{
#Override
public void run()
{
try
{
//use theStringYouWantToUseInTheThread here
MyDevice.getInstance().bitmap=getUserPic(text);
MyDevice.getInstance().facebook=1;
ImageView facebookImg=(ImageView) findViewById(R.id.facebookimg);
facebookImg.setImageBitmap(MyDevice.getInstance().bitmap);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
EditText edit=(EditText)dialog.findViewById(R.id.username);
theStringYouWantToUseInTheThread = edit.getText().toString();
thread.start();
dialog.dismiss();
}
});
dialog.show();
thread.start();
I'm not exactly sure if I understand but this sentence "How can i dynamically use the same text from the userinput inside the thread?"sounds like you want to implement a TextWatcher on your EditText
public class TempWatcher implements TextWatcher {
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// you can update something here
}
}
But if you check for a valid value in your EditText when clicking the Button and only start your Thread then, it should do what you want. If this doesn't work then please clarify your problem and show where you start the Thread and how it is implemented
I have an activity class in my Android App, where after some data is handled, that data is sent to another method in another class which gives back an ArrayList. What I want to do is to iterate through that ArrayList and for every piece of text in it, I would like to update the TextView label in my interface. However this should be done whenever the user presses the 'Yes' button on screen. (i.e. whenever the user presses the 'Yes' button, the loop would progress to the next element of the ArrayList and its value updated in the TextView label). Here is what I have so far:
try {
sentenceGenerator = new SentenceGenerator();
ArrayList<ArrayList<String>> states = stateGenerator
.getPossibleStates(resultOutput, result, result);
for (ArrayList<String> state : states) {
viewPoint = state.get(0);
subject = state.get(1);
object = state.get(2);
subjectInfo = new ObjectReference();
if (resultOutput.equals("Sliema")) {
Sliema sliema = new Sliema();
subjectInfo = sliema.getInfo(viewPoint, object, subject);
}
try {
s = sentenceGenerator.generateSentence("start", "middle",
"end", resultOutput, viewPoint, object, subject,
subjectInfo.type, subjectInfo.name,
subjectInfo.properties, false);
instructionTextView = (TextView) findViewById(R.id.instructionTextView);
mHandler = new Handler();
instructionTextView
.setText("Press 'Yes' to start Orientation procedure");
Button confirmButton = (Button) findViewById(R.id.confirmButton);
Button declineButton = (Button) findViewById(R.id.declineButton);
confirmButton
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mHandler.post(mUpdate);
}
});
declineButton
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//
}
});
Log.d("INSTRUCTIONS", s);
} catch (Exception e) {
}
}
} catch (ClassNotFoundException e) {
}
and this is the update Handler:
private Runnable mUpdate = new Runnable() {
public void run() {
instructionTextView.setText(s);
// mHandler.postDelayed(this, 1000);
confirmPressed = true;
// i++;
}
};
Any Suggestions? Thanks in advance!
Try Adding a break;in the buttonclickListener where you want to terminate the loop.
I have an Activity with Android.os.Handler, which is used for communication with an openGL thread.
I want to get messages from the opengl thread and draw some GUI in the activity depending on the message data, so I do:
Handler handle = new Handler(new Handler.Callback(){
public boolean handleMessage(Message msg){
// update GUI like
TextView v1 = (TextView) GamescreenActivity .this.findViewById(R.id.mytextview)
// then what I actually would like to do but it does not work:
Button b = (Button) GamescreenActivity.this.findViewById(R.id.mybutton);
b.setOnClickListener(null);
if (msg.what == MY_OWN_CONSTANT) {
b.setOnClickListener(getOnClickDoSomething(msg));
}
}
View.OnClickListener getOnClickDoSomething(final Message msg) {
return new View.OnClickListener() {
public void onClick(View v) {
makeDialog(msg);
}
};
}
private void makeDialog(Message msg) {
AlertDialog.Builder builder = new AlertDialog.Builder(GamescreenActivity.this);
builder.setMessage("yo")
.setTitle(""+ msg.what);
AlertDialog dialog = builder.create();
dialog.show();
}
I hope it is clear what i'm trying to achieve. What I get by now is msg being null in the makeDialog methode almost all the time.
What I get by now is msg being null in the makeDialog methode almost all the time.
I believe the Message has already been recycled when this happens. Let's create a local copy of msg and modify your code a little to make it more efficient. First create a new field variable:
Message message;
Next change your if-else block:
if (msg.what == MY_OWN_CONSTANT) {
message = Message.obtain(msg);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(GamescreenActivity.this);
builder.setMessage("yo")
.setTitle(""+ message.what)
.show();
message.recycle(); // Recycle our message when we're done
}
});
}
else {
b.setOnClickListener(null);
}