pop-up code for android - android

i want a pop-up to display one of d string data present in a database in random or in a sequence. what code should i b using for this kind of a pop-up. am very new to android development. i would appreciate help. please don't leave this unanswered.
package popupTest.popupTest;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
public class popupTest extends Activity {
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
mainLayout = new LinearLayout(this);
tv = new TextView(this);
but = new Button(this);
but.setText("Click Me");
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
public void run() {
// TODO Auto-generated method stub
popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
}
}, 1000);
//Use this to dismiss as per your need...
// popUp.dismiss();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
popUp.dismiss();
return false;
}
}
will this code help me?

A Toast is the easiest for this:
Toast.makeText(getApplicationContext(), "Button is clicked", Toast.LENGTH_LONG).show();
Example: http://www.mkyong.com/android/android-toast-example/

For poping up some information, there are two most commonly used methods.
Toast
AlertDialog
Toast:
it appears for a specific amount of time, displays some info and then disappears.
Dialog:
It displays your message but doesn't goes away from screen untill the user presses a button for example "Ok" button.
For toast:
Toast.makeText(getApplicationContext(), "your message here", Toast.LENGTH_LONG).show();
For Dialog:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
alertDialogBuilder.setTitle("Your Title");
alertDialogBuilder.setMessage("This is to notify you");
alertDialogBuilder.show();
alertDialogBuilder.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(),"You clicked on OK", Toast.LENGTH_SHORT).show();
}
});

Related

Android onClickListener works only once, Button does not works for second time

I am new in android Programming , Here is the code in which checkButton works only for once . Nothing happens when I click it for the second time. I can't seem to find problem.
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ProgressDialog progress;
Button checkButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkButton = (Button) findViewById(R.id.button);
checkButton.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v){
EditText aNumber = (EditText) findViewById(R.id.editText);
String aCount = aNumber.getText().toString();
boolean totalDigit = aCount.length()==16;
if(totalDigit){
if(checkConnectivity()) {
progress = new ProgressDialog(v.getContext());
progress.setTitle("Validating Number");
progress.setMessage("Processing...");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
progress.dismiss();
MainActivity.this.runOnUiThread(new Runnable()
{
public void run()
{
//Do your UI operations like dialog opening or Toast here
showAlert();
}
});
}
}).start();
}else{
Toast.makeText(MainActivity.this,"Internet Connection is not Available",Toast.LENGTH_SHORT).show();
}}else {
Toast.makeText(MainActivity.this, "Please enter correct 16 digit number",Toast.LENGTH_SHORT).show();
}
}
});
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void showAlert(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Number not found!");
builder.setCancelable(true);
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("Number Status");
alert.show();
setContentView(R.layout.activity_main);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private boolean checkConnectivity(){
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
return true;
}
else {
return false;
}
}
}
My Check Button works for only once. After I press it for second time nothings happen. I have looked all previous answer related to this in stackoverflow but nothings helped me. Please someone tell me what is going wrong.
I think the problem is you call setContentView(R.layout.activity_main); twice. setContentView will override the layout and replace it with the new one. You should only one call it in onCreate. That's why you should remove this line in showAlert() method.
private void showAlert(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Number not found!");
builder.setCancelable(true);
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("Number Status");
alert.show();
// remove this line below
//setContentView(R.layout.activity_main);
}
You will get the message warning Attempted to finish an input event but the input event receiver has already been disposed if you debug your code. The reason is you call setContentView twice so that the input event receiver of previous view is not free. That's why you can not click your button in the second times.
Try my solution, remove setContentView in showAlert and see the result.
You have two conditionals in your onClickListener which affect whether the onClicklistener "works"
Have you put in debug statements before these conditionals to see if they get this far (notice the two if statements below):
boolean totalDigit = aCount.length()==16;
if(totalDigit){
if(checkConnectivity()) {

How to use runOnUiThread() in alertDialog to change my textView using setText() method

I have been trying to change my textView text using setText() on Done button click from Soft Keyboard. But it wont work. I can set the text from anywhere else except for in the on button click. I am also able to set a working Toast from the Done button click, just can't cant manipulate the textView from inside. I was hinted that I should try runOnUiThread() method, but I still cant get it to work and I have spent hours trying. This is all from within a custom adapter if it makes any difference.
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
class CustomAdapter extends ArrayAdapter{
public CustomAdapter(Context context, ArrayList choreText) {
super(context, R.layout.custon_listview_row, choreText);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater myInflater = LayoutInflater.from(getContext());
View customView = myInflater.inflate(R.layout.custon_listview_row, parent, false);
ImageButton imageButton = (ImageButton) customView.findViewById(R.id.imageButton_ID);
final TextView textView = (TextView) customView.findViewById(R.id.textView_ID);
final EditText input = new EditText(getContext());
final AlertDialog OptionDialog = new AlertDialog.Builder(getContext()).create();
final MainActivity mn = new MainActivity();
//makes textView clickable
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//what happens when textView is clicked
//final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
// put aler dialog box logic here
OptionDialog.setTitle("Enter new chore");
OptionDialog.setView(input);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setImeOptions(EditorInfo.IME_ACTION_DONE);
//checks if "Done" button on soft keyboard is clicked
input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//what happens when "Done" is clicked
//textView wont change to string hello
//textView.setText(input.getText().toString());
mn.runOnUiThread(new Runnable() {
#Override
public void run() {
// your stuff to update the UI
textView.setText("hello");
}
});
//this toast works
// Toast.makeText(getContext(), "this is my Toast message!!! =)",
// Toast.LENGTH_LONG).show();
OptionDialog.dismiss();
}
return false;
}
});
OptionDialog.show();
}
});
imageButton.setImageResource(R.drawable.clock);
return customView;
}
}
The line final MainActivity mn = new MainActivity(); is wrong because the mainactivity already exist. Pass the reference of the calling activity to the arrayadapter and call myactivity.runOnUiThread()
Your code isn't in a subclass of the Activity, so you may not use runOnUiThread() method because it is a method of Activity.
final MainActivity mn = new MainActivity(); is not correct, you should never new an Activity yourself.
Instead, you can use View.post(Runnable action), just like this:
input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId== EditorInfo.IME_ACTION_DONE){
//what happens when "Done" is clicked
textView.post(new Runnable() {
#Override
public void run() {
// your stuff to update the UI
textView.setText("hello");
}
});
//this toast works
// Toast.makeText(getContext(), "this is my Toast message!!! =)",
// Toast.LENGTH_LONG).show();
OptionDialog.dismiss();
}
return false;
}
});

Display(Enable) ActionBar when dialog is shown

How to Display ActionBar when Dialog is shown in Android? My ActionBar should be enabled even when dialog is appeared.
In my app it is becoming dim when dilog is appeared.
import android.app.*;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;
public class ShowPopUp extends Activity {
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
mainLayout = new LinearLayout(this);
tv = new TextView(this);
but = new Button(this);
but.setText("Click Me");
but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
}
}
Hope it helps you

On click Listener called twice

I know this is easy but i m just not able to debug it.
I am dynamically creating 3 LinearLayouts and adding them to a scroll View . I am adding a Button "ins" to the top of 3 linearlayout.
The problem is that on clicking the ins Button , its on clicklistener is being called 3 times .
code:
package com.integrated.mpr;
import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class Page1 extends Activity implements OnClickListener{
static int pos = new Choose().n;
static String partname;
int i;
int[][] id = new int[pos][5];
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int p =0;
for(int i =0;i<3;i++){
for(int j =0;j<5;j++){
p++;
}
}
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
int resid = getResources().getIdentifier("background", "drawable", getPackageName());
ll.setBackgroundResource(resid);
Button ins = new Button(this);
ins.setText("Instructions");
ins.setOnClickListener(this);
ins.setId(5000);
ll.addView(ins);
for(i =0;i<3;i++){
LinearLayout llay = new LinearLayout(this);
llay.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText("enter the " +(i+1)+" position name");
EditText et = new EditText(this);
et.setId(id[i][0]);
Button starta = new Button(this);
starta.setText("Record 1");
starta.setId(id[i][1]);
starta.setOnClickListener(this);
Button startb = new Button(this);
startb.setText("Record 2");
startb.setId(id[i][2]);
startb.setOnClickListener(this);
Button startc = new Button(this);
startc.setText("Record 3");
startc.setId(id[i][3]);
startc.setOnClickListener(this);
Button stop = new Button(this);
stop.setText("Submit");
stop.setId(id[i][4]);
stop.setOnClickListener(this);
TextView tv1 = new TextView(this);
tv1.setVisibility(llay.INVISIBLE);
llay.addView(tv);
llay.addView(et);
llay.addView(starta);
llay.addView(startb);
llay.addView(startc);
llay.addView(stop);
llay.addView(tv1);
ll.addView(llay);
}
Button bcon = new Button(this);
bcon.setText("Continue");
bcon.setId(10000);
bcon.setOnClickListener(this);
ll.addView(bcon);
sv.addView(ll);
this.setContentView(sv);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==5000){
Log.d("ins", "called");
Context mContext = Page1.this;
Dialog dialog = new Dialog(mContext);
dialog.setTitle("Instructions");
dialog.setContentView(R.layout.instructiondialog);
dialog.show();
}
}
}
}
I know definitely , there is some problem in the code of OnCreate but what it is ? Help please
Can anyone further explain how to set the button aligned o the right of screen , that is how to set its layout gravity?
I am not sure about what exactly you want to achieve.but you can set the flag.if flag value is 1 then only process onClick else not.
Below snippet will help you.
Yes but you will have to somehow reset that flag to 1.
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(v.getId()==5000)
{
if(flag)
{
Log.d("ins", "called");
Context mContext = Page1.this;
Dialog dialog = new Dialog(mContext);
dialog.setTitle("Instructions");
dialog.setContentView(R.layout.instructiondialog);
dialog.show();
}
flag=0;
}
}
You can also call button click event like this
Button ins = new Button(this);
ins.setText("Instructions");
ins.setOnClickListener(this);
ins.setId(5000);
ins.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
Context mContext = Page1.this;
Dialog dialog = new Dialog(mContext);
dialog.setTitle("Instructions");
dialog.setContentView(R.layout.instructiondialog);
dialog.show();
}
});

Regarding Validation of Edit Text in Android

I am working on validation of two edit text fields in my login page and little change in my idea. I need to show textview above this field if the user left field e1 without entering text and goes to e2. Any help is appreciated.
Here is my code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
;
public class LoginActivity extends Activity {
EditText usernameText;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
Button login = (Button) findViewById(R.id.loghomebutton);
Button newuser = (Button) findViewById(R.id.lognewuserbutton);
EditText usernameText = (EditText)this.findViewById(R.id.emaileditlog);
EditText passwordText = (EditText)this.findViewById(R.id.pwdeditlog);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(v.getContext(),
SettingsActivity.class);
startActivityForResult(myIntent, 0);
overridePendingTransition(R.anim.fadeout, 0);
}
});
newuser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(v.getContext(),
RegisterActivity.class);
startActivityForResult(myIntent, 1);
overridePendingTransition(R.anim.fadeout, 0);
}
});
}
}
in XML add a textview above the edittext.. set textviews android:visibility="GONE" then in java code do this..
if(editText.getText.toString().equals(" "))
{
TextView t = findViewById(.......)
t.setVisibility(View.VISIBLE);
t.setText("..............");
}
In your xml layout, define your textview, place it as you want, then use
android:visibility="gone"
and in your code, when needed
TextView errorTextView = (TextView) findViewById( R.id.error_view );
errorTextView.setVisibility( View.VISIBLE );
Regards,
Stéphane
It would actually be better if you used the hint feature of the editbox.
Here is how it will look with no data:
I have used like this in my Calculator application.I am showing an Alertdialog instead of diaplaying error message in Textbox
if(calc_txt_Prise.getEditableText().toString().trim().equals(""){
String dialog_message = "Please enter some Text";
AlertDialog.Builder invalid_input_dialog = new AlertDialog.Builder(Archive.this);
invalid_input_dialog.setTitle("Application name")
.setMessage(dialog_message)
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
showDialog(DATE_DIALOG_ID);
}
})
}

Categories

Resources