not able to show custom Toast - android

I have two custom toasts.
When phone state is ringing it will show first toast, there is one button when i click on that button first toast will be dismiss and second toast will be shown.
And when I again click on second toast button it will dismiss second toast and show first toast. How can i do this? Please help me as soon as possible.
Here is my code:
hideBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(numberToast!=null){
numberToast.cancel();
showTest_Toast();
}
//------------------------------------------------------------------
public void showTest_Toast() {
LayoutInflater inflater = LayoutInflater.from(ctx);
View v = new View(ctx);
test_layout = inflater.inflate(R.layout.test_toast,
(ViewGroup) v.findViewById(R.id.test_id));
testBtn = (Button) test_layout.findViewById(R.id.test_toast_button);
testToast = new Toast(ctx);
testToast.setGravity(Gravity.LEFT, 0, 0);
testToast.setView(test_layout);
// test_layout.setVisibility(View.INVISIBLE);
if (showButton == 1) {
SharedPreferences timer_preferences = PreferenceManager
.getDefaultSharedPreferences(ctx);
String timer = timer_preferences.getString("duration", null);
new CountDownTimer(Long.parseLong(timer), 1000) {
public void onTick(long millisUntilFinished) {
testToast.show();
}
public void onFinish() {
// testToast.cancel();
}
}.start();
} else {
new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished) {
testToast.show();
}
public void onFinish() {
// testToast.cancel();
}
}.start();
}
testBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (testToast!=null) {
testToast.cancel();
numberToast.show();
}
}
});
}

Try to open Custom Popup Screen. Its easy for u.
xml
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/item_movies"
android:showAsAction="ifRoom|withText"
android:title="Default Screen"
android:visible="true"
/>
<item
android:id="#+id/item_music"
android:showAsAction="ifRoom|withText"
android:title="Silent"
android:visible="true"/>
java code.
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) myview.findViewById(R.id.slidehandlebutton);
// Get the x, y location and store it in the location[] array
button.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
private void showPopup(Unlock_hud unlock_hud, Point p2) {
// TODO Auto-generated method stub
int popupWidth = 180;
int popupHeight = 80;
LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout1 = layoutInflater.inflate(R.layout.popup_layout, null);
// Creating the PopupWindow
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = -200;
int OFFSET_Y = 5;
// Displaying the popup at the specified location, + offsets.
params2.x =20;
params2.y = 40;
wm2.addView(layout1, params2);
isDialogShow = true;
// Getting a reference to Close button, and close the popup when
// clicked.
Button close = (Button) layout1.findViewById(R.id.stop);
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
wm2.removeView(layout1);
isDialogShow = false;
onDestroy();
}
});
on Button click.
case R.id.slidehandlebutton:
if (isDialogShow) {
DissmissPopup();
} else {
showPopup(Unlock_hud.this, p);
}

Related

Show hidden views of List item on click of button outside the list

I am developing an Android Application. In this app I am making a list of views using code given below. In each item of list there is delete button with visibility "Gone". Now there is another button Edit outside the list, on click of edit button I have to show delete button on each item of list. but using this code delete button shows only in the last item. Please help me to solve the problem. Thanks.
dynamicView();
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// lay.removeAllViews();
addnew.setVisibility(View.INVISIBLE);
btn_red.setVisibility(View.VISIBLE);
edit.setText("Done");
}
});
public void dynamicView() {
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < templates.length; i++) {
final View customView = linflater.inflate(R.layout.order_template_item,
null);
btn_red=(ImageView)customView.findViewById(R.id.btn_negative);
btn_drag=(ImageView)customView.findViewById(R.id.button_drag);
btn_delete=(ImageView)customView.findViewById(R.id.button_delete);
final ImageView image = (ImageView)customView.findViewById(R.id.arrow);
final TextView text = (TextView)customView.findViewById(R.id.date);
final TextView sku = (TextView)customView.findViewById(R.id.time);
final TextView price = (TextView)customView.findViewById(R.id.last);
final TextView names =(TextView)customView.findViewById(R.id.name);
image.setId(i);
text.setId(i);
sku.setId(i);
price.setId(i);
names.setId(i);
btn_red.setId(i);
btn_red.setTag(i);
btn_delete.setId(i);
btn_drag.setId(i);
names.setText(templates[i]);
btn_red.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_delete.setVisibility(View.VISIBLE);
TranslateAnimation anim = new TranslateAnimation(100,0 , 0, 0);
anim.setInterpolator(new BounceInterpolator());
anim.setDuration(1000);
btn_delete.setAnimation(anim);
}
});
btn_delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lay.removeView(customView);
}
});
lay.addView(customView);
}
You're holding reference only to the last one btn_red.
You can do something like this.
List<Button> buttons = new LinkedList<Button> ();
Then in your loop after findViewById on btn_red
buttons.add(btn_red);
And finally in your onClickListener
for (Button button: buttons) {
button.setVisibility(View.VISIBLE);
}
Do somethink like below to make visible all the button added to list items-
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addnew.setVisibility(View.INVISIBLE);
for (int i = 0; i < templates.length; i++) {
int id = btn_red.getId(i);
id.setVisibility(View.VISIBLE);
}
edit.setText("Done");
}
});

Android GridView first button not working

I am having weird problems with Android GridView. I create a 3x4 grid and insert buttons into that grid. I want the background of the button to change when the user clicks that button. And this is working just fine for all buttons except the first one (the one with index 0 - top left). OnClick event listener doesn't fire at all for that button no matter what I do.
Here is the code where I create the view:
public View getView(int position, View convertView, ViewGroup parent) {
Button imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
Log.w("NOVO", "narejena nova celica");
imageView = new Button(mContext);
imageView.setPadding(8, 8, 8, 8);
} else {
Log.w("STARO", "stara celica");
imageView = (Button) convertView;
}
imageView.setEnabled(true);
int visina = parent.getHeight();
int sirina = parent.getWidth();
float dip = mContext.getResources().getDisplayMetrics().density;
float margin = 10*dip;
int view_height = (int)(visina - 3*margin)/4;
int view_width = (int)(sirina - 2*margin)/3;
int view_dim = 0;
if (view_height <= view_width)
view_dim = view_height;
else
view_dim = view_width;
imageView.setLayoutParams(new GridView.LayoutParams(view_dim, view_dim));
imageView.setId(position);
imageView.setOnClickListener(celice.get(position));
/*imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast toast = Toast.makeText(mContext, v.getId() + "bla", Toast.LENGTH_SHORT);
//toast.show();
celice.get(v.getId()).celicaVisible(4000);
}});*/
celice.get(position).id = position;
celice.get(position).setButton(imageView);
return imageView;
}
If I replace
imageView = (Button) convertView;
with
imageView = new Button(mContext);
then the onClick() gets fired but the background still doesn't change. All the other buttons are working as expected.
And here is the custom class "Celica" that takes care of the actual work - changing the background...
public class Celica implements OnClickListener {
public boolean odkrit;
public boolean najden;
public int id;
public Drawable slikca1, slikca2;
public Celica par;
private Timer tim;
public Button but;
public Context con;
static int buttonsVisible = 0;
Celica(Drawable s1, Drawable s2) {
this.slikca1 = s1;
this.slikca2 = s2;
}
void celicaVisible(int millis) {
if (odkrit)
return;
Log.w("TEST", "prizganih " + buttonsVisible);
if (buttonsVisible >= 2)
return;
odkrit = true;
buttonsVisible++;
tim = new Timer();
tim.schedule(new timerDone(), millis);
((Activity)con).runOnUiThread(new Runnable() {
#Override
public void run() {
but.setBackground(slikca2);
}
});
}
void setButton(Button b) {
but = b;
((Activity)con).runOnUiThread(new Runnable() {
#Override
public void run() {
but.setBackground(slikca1);
}
});
}
class timerDone extends TimerTask {
#Override
public void run() {
if (!najden) {
odkrit = false;
((Activity)con).runOnUiThread(new Runnable() {
#Override
public void run() {
but.setBackground(slikca1);
}
});
}
buttonsVisible--;
tim.cancel();
}
}
#Override
public void onClick(View v) {
celicaVisible(4000);
}
}
In Android, ID of any View must be non zero and non negative number. means View ID should be > 0. and there is problem, when you are setting ID to the Button like
imageView.setId(position)
here ID of a button will be zero when position is zero(means first item). may be due to this, First Button's OnClickListener is not getting fired...try setting a ID that is greater than zero to Button and try once...
you can write like
imageView.setId(position+1) to ensure ID > 0
I actually figured it out. Everything works if I use the view that gets provided by the onClick() method instead of saving the actual button at the creation of the Celica object.
So basically adding:
but = (Button) v;
to the onClick() method solved the problem.

How pass rating from rating bar in the dialog to rating bar on activity

I'd like to pass the rating - that will be set by the user on the rating bar of the DIALOG - to the rating bar on the activity. (I have set 2 xmls with 2 different rating bars)
Can you help (with code) how I could to this?
THanks in advance.
public class RecipesDetails extends Activity implements OnClickListener, OnRatingBarChangeListener{
TextView rTitle, rType, rSubName, reviewAmount;
Button aRating;
RatingBar getNewRating, setRatingBar;
int count;
float curRate;
private static final int DIALOG_TEXT_ENTRY_RATE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView (R.layout.recipes_detail);
initialize();
aRating.setOnClickListener(this);
rTitle.setText(getIntent().getExtras().getString("rec_name"));
rType.setText(getIntent().getExtras().getString("current_rating"));
rSubName.setText(getIntent().getExtras().getString("sub_name"));
setRatingBar.setRating(curRate);
getNewRating.setOnRatingBarChangeListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
rTitle = (TextView) findViewById(R.id.tvDetailRecipeTitle);
rType = (TextView) findViewById(R.id.tv_detail_recipe_Rating);
rSubName = (TextView) findViewById(R.id.tv_detail_recipe_SubmittedName);
aRating = (Button) findViewById (R.id.bAddReview);
getNewRating = (RatingBar) findViewById (R.id.detail_recipe_ratingBar);
setRatingBar = (RatingBar) findViewById (R.id.dialogRatingBar);
reviewAmount = (TextView) findViewById (R.id.tv_detail_recipe_Rating);
}
#SuppressWarnings("deprecation")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bAddReview:
showDialog(DIALOG_TEXT_ENTRY_RATE);
break;
}
}
#Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
DecimalFormat decimalFormat = new DecimalFormat("#.#");
curRate = Float.valueOf(decimalFormat.format((curRate * count + rating)
/ ++count));
Toast.makeText(RecipesDetails.this,
"New Rating: " + curRate, Toast.LENGTH_SHORT).show();
setRatingBar.setRating(curRate);
reviewAmount.setText(count + " Ratings");
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_TEXT_ENTRY_RATE:
LayoutInflater acc = LayoutInflater.from(this);
final View textEntryViewAcc = acc.inflate(R.layout.add_rating_dialog, null);
final RatingBar rateME = (RatingBar) textEntryViewAcc.findViewById(R.id.detail_recipe_ratingBar);
//final EditText amountofwater = (EditText) textEntryViewAcc.findViewById(R.id.etGlasesOfWater);
return new AlertDialog.Builder(RecipesDetails.this)
//.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(R.string.alert_dialog_add_review)
.setView(textEntryViewAcc)
.setPositiveButton(R.string.alert_dialog_submit,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();
}
return null;
}
}
I need to move the stuff that is in the onRatingChanged method to be moved to the dialog on positive button section, but not sure how to go about it....

When the popUP closes,the input automatically erases.I want the Edittext to return with the input that was placed when the popUp button is pressed

My issue is that I have a popup window that does not save the input of an
Edit Text when the popUp window closes. Expected Result: what i want is that when the popup closes, if for any reason i need to reopen the popup,I would like to see the value I placed. I have tried tinkering around with the activity life cycle and onSaveInsanceState methods,but my issues seems to not be resolved. For Some reason or another my popup Window has a LIFE of it's own. It basically creates a new popUp instance every time.
public class MainActivity extends Activity {
private Point p;
private Button b;
private TextView tv;
private PopupWindow popup;
private LayoutInflater layoutInflater;
private View layout;
private LinearLayout viewGroup;
private EditText etext;
//on create method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainViewItems();
onPause();
}
//allows me to pause session
#Override
protected void onPause(){
super.onPause();
mainViewItems();
}
// This method is in charge of instantiating the
// buttons,textviews, and allowing an event handler to
// call
showPopup() method.
public void mainViewItems(){
b= (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
showPopup(MainActivity.this,p);}});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
b.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.
private void showPopup(final Activity context,Point p) {
int popupWidth = 230;
int popupHeight = 190;
// Inflate the popup_layout.xml
viewGroup = (LinearLayout) context.findViewById(R.id.pop);
layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.popup_layout,
viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(layout,350,350, true); //context
popup.setContentView(layout);//layout
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
popup.setTouchable(true);
popup.setOutsideTouchable(false);
popup.update();
int OFFSET_X = -100;
int OFFSET_Y = 100;
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x +
OFFSET_X, p.y + OFFSET_Y);
etext = (EditText) layout.findViewById(R.id.editText1);
Button b2 = (Button) layout.findViewById(R.id.calculate);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{tv.setText(etext.getText().toString());}});
Button close = (Button) layout.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) { popup.dismiss(); }});
}
}
You're creating a new PopupWindow each time you show it. What else would you expect- its a new window with a new EditText. The easiest way to fix this is to store the last entered String in a member variable of your activity, and to set the edit text to this variable when you show the popup.

Android quick alarm

I have a little problem with taking a quick alarm.
The alarm works fine if I set the alarm time before normal "quick alarm". How do I set the alarm for example 8:00 pm, quick alarm for 7:55 pm this works differently does not turn on
private void showQuickAlarmDialog() {
View layout = mFactory.inflate(R.layout.quick_alarm_dialog, null);
final TextView text1 = (TextView) layout
.findViewById(android.R.id.text1);
final SeekBar slider = (SeekBar) layout
.findViewById(android.R.id.input);
int last_snooze = mPrefs.getInt(PREF_LAST_QUICK_ALARM, 0);
slider.setMax(59);
slider.setProgress(last_snooze);
text1.setText(AlarmClock.this.getString(R.string.minutes,
String.valueOf(last_snooze + 1)));
slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seek, int value,
boolean fromTouch) {
text1.setText(AlarmClock.this.getString(R.string.minutes,
String.valueOf(value + 1)));
}
public void onStartTrackingTouch(SeekBar seek) {
}
public void onStopTrackingTouch(SeekBar seek) {
}
});
AlertDialog d = new AlertDialog.Builder(this)
.setTitle(R.string.quick_alarm)
.setView(layout)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
int snooze_min = slider.getProgress() + 1;
long snoozeTarget = System.currentTimeMillis()+ 1000 * 60 * snooze_min;
Alarms.calculateNextAlert(AlarmClock.this).getAlert();
//if (nextAlarm > snoozeTarget) {
//alarm will fire before snooze will...
//}
//else {
Alarms.saveSnoozeAlert(
AlarmClock.this,0,snoozeTarget,AlarmClock.this.getString(R.string.quick_alarm));
Alarms.setNextAlert(AlarmClock.this);
Toast.makeText(
AlarmClock.this,
getString(
R.string.alarm_alert_snooze_set,
snooze_min),
Toast.LENGTH_LONG).show();
updateSnoozeVisibility();
mPrefs.edit()
.putInt(PREF_LAST_QUICK_ALARM,
snooze_min - 1).commit();
// }
}
}).show();
}
private void updateSnoozeVisibility() {
long next_snooze = mPrefs.getLong(Alarms.PREF_SNOOZE_TIME, 0);
View v = (View) findViewById(R.id.snooze_message);
if (next_snooze != 0) {
TextView tv = (TextView) v.findViewById(R.id.snooze_message_text);
Calendar c = new GregorianCalendar();
c.setTimeInMillis(next_snooze);
String snooze_time = Alarms.formatTime(AlarmClock.this, c);
tv.setText(getString(R.string.snooze_message_text, snooze_time));
v.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
v.setVisibility(View.GONE);
Alarms.disableSnoozeAlert(AlarmClock.this);
Toast.makeText(AlarmClock.this,
getString(R.string.snooze_dismissed),
Toast.LENGTH_LONG).show();
Alarms.setNextAlert(AlarmClock.this);
}
});
v.setVisibility(View.VISIBLE);
} else {
v.setVisibility(View.GONE);
}
}
private void updateEmptyVisibility() {
View v = findViewById(R.id.alarms_list_empty);
if (v != null)
v.setVisibility(mAlarmsList.getAdapter().getCount() < 1 ? View.VISIBLE
: View.GONE);
}
In one sentence. I can't turn on quick alarm if the normal alarm is set at a later time.

Categories

Resources