android Custom Dialog title doesnot show - android

Im new at android app development so i was following a youtuber on making a Database, im doing same as he is, but problem is that i can make db successfully but custom dialog title doesnt show but error mesage & textview text is visible in my app.
Im attaching screenshot of successful code on youtube.
screenshot
btninsert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean boolln=true;
try {
String firstname = firstnam.getText().toString();
String lstname = lastnam.getText().toString();
db1.insertname(firstname, lstname);
}catch (Exception ex){
boolln=false;
String error=ex.toString();
Dialog dl=new Dialog(Activity_DB.this);
dl.setTitle("Uncuceesful");
TextView tx=new TextView(Activity_DB.this);
tx.setText(error);
dl.setContentView(tx);
dl.show();
}finally {
if (boolln){
Dialog dl1=new Dialog(Activity_DB.this);
dl1.setTitle("succesful? YES");
TextView tx=new TextView(Activity_DB.this);
tx.setText("sucesss");
dl1.setContentView(tx);
dl1.show();
}
}

Solved this problem by adding AlerDialog.builder instead of using Dialog, now it works perfectly.
boolln=false;
String error=ex.toString();
AlertDialog.Builder dl=new AlertDialog.Builder(Activity_DB.this);
dl.setCancelable(true);
dl.setTitle("Uncuceesful");
dl.setMessage("No entry found. \n"+error);
dl.show()

Related

How to get access of oncreate variable accesss with in the ActivityResultLauncher Method Android

I am using this Barcode Scanner Library Dependenceny in my code.
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
I am working in fragment and in fragment when i click on button there is a dialog appears and in the dialog i have an scan button to can the barcode and want to show the same text inside the the dialog's Textview. My dialog code is inside the oncreate but the ActivityResultLauncher in out of scope so how can i make my dialog variable to be in scope of ActivityResultLauncher. I am very thankful to all of you developer to answer this query.
here is the oncreate code :
fab_addproduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bottomSheetDialog = new BottomSheetDialog(getContext(), R.style.AppBottomSheetDialogTheme);
bottomSheetDialog.setContentView(R.layout.productadd_bottom_sheet);
TextInputEditText product_id = bottomSheetDialog.findViewById(R.id.product_id);
TextInputEditText product_name = bottomSheetDialog.findViewById(R.id.product_name);
TextInputEditText product_quantity = bottomSheetDialog.findViewById(R.id.product_quantity);
MaterialButton add_btn = bottomSheetDialog.findViewById(R.id.add_btn);
ImageView barcode_scanner = bottomSheetDialog.findViewById(R.id.barcode_Scanner);
TextInputEditText product_price = bottomSheetDialog.findViewById(R.id.product_price);
barcode_scanner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ScanOptions options = new ScanOptions();
options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
options.setPrompt("Scan a barcode");
options.setCameraId(0); // Use a specific camera of the device
options.setBeepEnabled(true);
options.setBarcodeImageEnabled(true);
barcodeLauncher.launch(options);
}
});
bottomSheetDialog.show();
}
});
here is the ActivityResultLauncher code where the barcode result data is avai
public final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
result -> {
if(result.getContents() == null) {
Toast.makeText(getContext(), "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getContext(), "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
** product_id.settext(result.getContents());// this is not accessable i want to make it accessable. here product_id is red in the code.
** }
});
I have tried many ways by using the varaiable static its also show error. i also use binding to make it accessable but i am not able to achieve my goal.

SetError on EditText

I am working on an android app. I am having an EditText on which I have applied OnCllickListener().
EditText _input = new EditText(context);
_input.setSingleLine(true);
_input.setFocusable(false);
I am setting OnClickListener() on this EditText:
_input.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// OnClick work here
}
});
I have applied some validations on this field and calling setError() method on this EditText. It shows that red icon of error in the EditTextwhen the validation fails. But when I click on that error icon it executes the OnClickListener() on this EditTextand I am not able to see the error occurred.
Is there any other way to do so, so that I can handle both functions.
Thanks a lot in advanced !!!
You would check if currently the edit text is in an error state, within the onclick handler:
_input.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getError() == null) {
// do not handle the click
return;
}
}
});
You can check for an error like like this:
if(view.getError() == null) return;
See the doc here
getError()
Returns the error message that was set to be displayed with
setError(CharSequence), or null if no error was set or if it the error
was cleared by the widget after user input.
Source
I have solved this with a simple solution. Just set text of editText to empty string before setting error and error message will show again like without onClickListener.

Google Plus one button android not working

Hi Guys I have integrated a google +1 button in Android.
The main difference is I have created it inside a dialog and not inside the Activity. Below is the code I have used
final Dialog dialog = new Dialog(this.getContext());
dialog.setContentView(R.layout.plus_one_layout);
dialog.setTitle("Title...");
dialog.setCancelable(false);
plusOneButton = (PlusOneButton) dialog.findViewById(R.id.plus_one_button);
if(!DotsCrazy.this.mGoogleApiClient.isConnected()) {
DotsCrazy.this.mGoogleApiClient.connect();
}
OnPlusOneClickListener listener = new OnPlusOneClickListener() {
#Override
public void onPlusOneClick(Intent intent) {
if(!DotsCrazy.this.mGoogleApiClient.isConnected()) {
DotsCrazy.this.mGoogleApiClient.connect();
} else {
startActivityForResult(intent, 0);
}
}
};
plusOneButton.initialize("http://plus.google.com/+Example",listener);
dialog.show();
The problem is it always show "There was a temporary problem with your +1 Please try again later". Anybody knows the reason?

Show DialogBox in loop in android

I am trying to show dialog boxes with contents from the database .The fetched data from the database may contain more than 1 data.So I have to show dialog in for loop.But the dialog shows only for the first row in the database.Here is the code
cursor = sqldb.query(Database_Handler.dbdectab, null,"((" + Database_Handler.gendtime + "<='" + after_1hr + "' and " + Database_Handler.gendtime + ">='" + before_1hr + "') and ("
+ Database_Handler.calused + "='Gregorian' or " + Database_Handler.calused + "='both')) ", null, null, null, null);
notif_count = cursor.getCount();
//dec_name_ctr_builder = new StringBuilder("");
if(notif_count>0)
{
dialog1 = new Dialog(this);
cursor.moveToFirst();
do
{
dec_name =cursor.getString(cursor.getColumnIndex(Database_Handler.decname));
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setContentView(R.layout.custom_dialog_alert);
TextView tv_alert = (TextView)dialog1.findViewById(R.id.txt_alert);
tv_alert.setText( dec_name );
Button yes = (Button) dialog1.findViewById(R.id.btn_yes);
Button no = (Button) dialog1.findViewById(R.id.btn_no);
yes.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(donateurl));
startActivity(intent);
dialog1.dismiss();
cursor.close();
sqldb.close();
finish();
}
});
no.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
dialog1.dismiss();
cursor.close();
sqldb.close();
finish();
}
});
dialog1.show();
}while(cursor.moveToNext());
Android dialog boxes are not modal (which is to say, they don't block the background processes).Hence your work is done asycnhronuosly and according to :
Romain guy
We've designed Android to prevent developers from writing synchronous dialogs so you don't really have much of a choice.
SO you can not block the while loop execution in between using Dialog.
Karan_Rana is right. The simplest solution for your answer would be storing the results somewhere else and displaying a new dialog each time some data is left and user clicks on a previous dialogs button.
I know this is a bit late, but I had a similar problem once. I used recursion to loop a dialog box. My situation was unique as I needed to detect an internet connection during app startup. So, this approach may not work for you. But I was able to implement the solution without for loops and blocking the main UI thread. Because I was on a splash screen, there was no other UI I needed to contend so I would simple call the dialog box again until the user chooses to "Quit".
I had a splash screen that would only continue to the landing activity of the app if a network connection was detected.
Sample code below to demonstrate the concept. Note that there are custom methods, but you should get the idea to call the prompt again from within onClick method.
protected void onCreate(Bundle savedInstanceState) {
if (isNetworkAvailable()) {
launchApp();
} else {
retryPrompt();
}
}
private void retryPrompt() {
if (isNetworkAvailable()) {
launchApp();
} else {
final ConfirmDialog dialog = new ConfirmDialog.CustomAlertBuilder(this,
"Internet Connection",
"Internet Connection not detected. Please check your connection.",
"Retry").
setNegBtnLabel("Quit").
setCustomCallback(new CustomDialogCallback() {
#Override
public void onClickPositiveBtn(DialogInterface dialogInterface, int which) {
// go around again.
retryPrompt(); // <<== Call method recursively
}
#Override
public void onClickNegativeBtn(DialogInterface dialogInterface, int which) {
// abort app.
finish();
}
}).build();
dialog.show();
}
}

Custom error message for phonegap in android

I'm using phonegap for my android app. My app loads a webpage from remote server. Now i need to generate a custom error message if the connection was unsuccesful.
Phonegap already contains a default error alert using an Alertbox
Application Error
The connection to the sever was unsuccessful. (http://yoursite.com)
Can i customize this error message?
use this `
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.customdialoglayout);
TextView dialogbody=(TextView) dialog.findViewById(R.id.dialogbody);
Button dialogok=(Button) dialog.findViewById(R.id.dialogok);
dialogok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialogbody.setText(displayText);
dialog.show();`

Categories

Resources