I am trying to make a calendar that shows a popup window when i long press the calendarview. But nothing happens when i do long click the calendarview. I tried using other event listeners like on click,onhover,etc. But none of them respond. I even thought of trying to use the calendarview as a dialog but none of the listeners i found in android documentation for dialog look for a long clock.
This is what i have tried to do.
public class MainActivity extends Activity{
PopupWindow p;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CalendarView c=(CalendarView)findViewById(R.id.calendarView1);
c.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View arg0) {
openpopupwindow();
return true;
}
private void openpopupwindow() {
// TODO Auto-generated method stub
LayoutInflater inflator=(LayoutInflater)MainActivity.this.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View lay=inflator.inflate(R.layout.add_appointment, (ViewGroup)findViewById(R.id.popupagenda));
p=new PopupWindow(lay,300,300,true);
p.showAtLocation(lay, Gravity.CENTER, 0, 0);
Button badd=(Button)findViewById(R.id.bAdd);
badd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
p.dismiss();
}
});
}
});
}
Related
I'm trying to do a modal view with a custom dialog, this is myDialog.java class:
public class MyDialog extends Dialog implements android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button btnaddsingleingredient, btncanceladdingsingleingredient;
public MyDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_dialog);
btnaddsingleingredient = (Button) findViewById(R.id.btnaddsingleingredient);
btncanceladdingsingleingredient = (Button) findViewById(R.id.btncanceladdingsingleingredient);
btnaddsingleingredient.setOnClickListener(this);
btncanceladdingsingleingredient.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnaddsingleingredient:
d.dismiss();
break;
case R.id.btncanceladdingsingleingredient:
d.dismiss();
break;
default:
break;
}
dismiss();
}
}
This is the class where I call the dialog:
public class AggiungiIngredientiActivity extends AppCompatActivity {
ImageButton btnAddNewIngredient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aggiungi_ingredienti);
btnAddNewIngredient = findViewById(R.id.btnAddNewIngredient);
btnAddNewIngredient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
MyDialog addIngredientDialog = new MyDialog(AggiungiIngredientiActivity.this);
addIngredientDialog.btncanceladdingsingleingredient = findViewById(R.id.btncanceladdingsingleingredient);
addIngredientDialog.btnaddsingleingredient.findViewById(R.id.btnaddsingleingredient).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Basically I want to open a modal view to let the user insert some data but it gives me the error:
Attempt to invoke virtual method 'android.view.View android.widget.Button.findViewById(int)' on a null object reference
and it says that the problem is here:
addIngredientDialog.btnaddsingleingredient.findViewById(R.id.btnaddsingleingredient).setOnClickListener(new View.OnClickListener())) { ... }
I don't know if this is the best way to do a modal view(i'm open to suggests), if you need the xml I can update the post but I think it's not relevant
Update 1:
I tried also
btnaddsingleingredient = (Button) findViewById(R.id.btnaddsingleingredient); //Replace sat1 with id defined in XML layout
instead of
addIngredientDialog.btnaddsingleingredient.findViewById(R.id.btnaddsingleingredient).setOnClickListener
Solved this, I put here the solution in case someone need it too.
Basically instead of creating a custom class that extends Dialog just use the class Dialog and then use dialog.setContentView(R.layout.yourlayout);
Then you reference the button with dialog.findViewById(R.id.idbuttoninthecustomalert);
And finally you can you setOnClickListener without the null reference!
Dialog mydialog;
Button button,
mydialog = new Dialog(context.this);
mydialog.setContentView(R.layout.yourlayout);
button= mydialog.findViewById(R.id.buttonid);
I am doing customized search bar like WhatsApp, google apps.I am trying to add animation while showing and closing the view.I add animation XML files to do these things.
Suppose, if the user presses the search icon, it will display the search bar with animation. if the user presses the close icon in the search bar, it will close the search bar with animation.
Animation happens only in First time.It doesn't animate for the second time like when I press the search icon.
public class MainActivity extends AppCompatActivity implements Animation.AnimationListener{
ImageButton ibSearch;
EditText etSearch;
ImageButton ibBack,ibClose;
CardView cardView;
ObjectAnimator objectanimator;
RelativeLayout search;
Animation animOpenView,animCloseView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ibSearch = (ImageButton)findViewById(R.id.search);
cardView = (CardView) findViewById(R.id.searchBarC);
ibClose = (ImageButton) findViewById(R.id.close);
search = (RelativeLayout) findViewById(R.id.searchLayout);
animOpenView = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.anim_left);
animCloseView = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.anim_right);
animCloseView.setAnimationListener(this);
animOpenView.setAnimationListener(this);
ibSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cardView.setVisibility(View.VISIBLE);
cardView.setAnimation(animOpenView);
}
});
ibClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cardView.setVisibility(View.GONE);
cardView.setAnimation(animCloseView);
}
});
}
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
Any suggestions would be appreciated.
This is because you are making your view gone
your view is going off the screen before the animation starts..
so to enjoy the animation just remove that line..
cardView.setVisibility(View.GONE); // remove this..
cardView.setAnimation(animCloseView);
try this in your activity's onAnimationEnd:
#Override
public void onAnimationEnd(Animation animation) {
cardView.clearAnimation();
}
The issue was fixed.Actually, the issue was,animCloseView and animOpenView get empty/non-null after the first time.So I reinitiate the anims.
ibSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cardView.setVisibility(View.VISIBLE);
animOpenView = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.anim_left); //just added this line
cardView.setAnimation(animOpenView);
}
});
ibClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cardView.setVisibility(View.GONE);
animCloseView = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.anim_right); //just added this line
cardView.setAnimation(animCloseView);
}
});
}
I am new to Android, i tried to use an ontouchevent() to detect screen touch, i worked on one of the sample page but when i copy pasted the same code in another page it doesn't seem to be working.
I think it has something to do with the View.
Can someone please help me on this.
Code
public class HelpDesk extends Activity{
#Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
ImageButton home = (ImageButton)findViewById(R.id.home);
ImageButton exit = (ImageButton)findViewById(R.id.exit);
final Intent homeIntent = new Intent(this,MobNavSys4VisuallyImpaired.class);
home.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
startActivity(homeIntent);
}
});
exit.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
startActivity(homeIntent);
}
});
}
public boolean onTouchEvent(MotionEvent event)
{
System.out.print("touch");
Toast.makeText(this, "hiiiiii", Toast.LENGTH_SHORT).show ();
return true;
}}
`
check it
change return true;
to return super.onTouchEvent(event);
i have a pop up which contains edit text..how can i prevent the softkeyboard from showing up on displaying of pop up's because my pop up is moving up as soon as it is displayed.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
i used this line in the activity in which a pop up is displayed on button click. please help...!
android:windowSoftInputMode="stateHidden"
i also used this line in manifest. but of no use.
public class MainActivity extends Activity implements OnClickListener {
AlertDialog dialog;
View checkBoxView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.des_button).setOnClickListener(this);
//checkBoxView = View.inflate(this, R.layout.popup_description, null);
}
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(this);
dialog.getWindow().getAttributes().windowAnimations =
R.style.dialog_animation;
// dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.popup_description);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Button btn_Start = (Button) dialog.findViewById(R.id.ok);
btn_Start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button cancel = (Button) dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
#Override
public void onDestroy() {
if (dialog!=null) {
if (dialog.isShowing()) {
dialog.dismiss();
}
Try
android:windowSoftInputMode="stateAlwaysHidden"
in your manifest in that activity. one more thing you can do call the following method after opening that dialog popup.
public static void hideKeyboard()
{
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(editTextInPopup.getWindowToken(), 0);
}
I am Using the following code for my custom dialog box.
Code is here
I am using a new layout by setCustomView Method.That layout contains a 'Ok' button and a 'Cancel' button.
I need to close the dialog box when click on cancel.
buttonCancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("onClick" , "YYYYY");
//up to this comes , here what I can wright
}
});
dialogObject.dismiss();
You can use this method
Why not you create custom dialog from here:
http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout
Very clearly explained and easy to implement too.
try this :
buttonCancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("onClick" , "YYYYY");
qustomDialogBuilder.dismiss();//this line will close the dialog
}
});
Replace your TestDialogActivity like below,
public class TestDialogActivity extends Activity {
private static final String HALLOWEEN_ORANGE = "#FF7F27";
private AlertDialog alertDialog;
private OnClickListener mShowDialogClickListener = new OnClickListener() {
public void onClick(View v) {
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(
v.getContext())
.setTitle("Set IP Address")
.setTitleColor(HALLOWEEN_ORANGE)
.setDividerColor(HALLOWEEN_ORANGE)
.setMessage("You are now entering the 10th dimension.")
.setCustomView(R.layout.example_ip_address_layout,
v.getContext())
.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
alertDialog=qustomDialogBuilder.create();
qustomDialogBuilder.setAlertDialog(alertDialog);
alertDialog.show();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(mShowDialogClickListener);
}
and replace setCustomView of QustomDialogBuilder like below
public QustomDialogBuilder setCustomView(int resId, final Context context) {
View customView = View.inflate(context, resId, null);
((TextView)customView.findViewById(R.id.ip_text)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
((FrameLayout)mDialogView.findViewById(R.id.customPanel)).addView(customView);
return this;
}
finally add below line into your QustomDialogBuilder
private AlertDialog alertDialog;
public void setAlertDialog(AlertDialog alertDialog)
{
this.alertDialog=alertDialog;
}
To close your dialog click on IP Address text.
Using the QustomDialog Source here in your activity class (TestDialogActivity), you can set the "Ok" and "Cancel" button by setting the Negative and Positive button of the dialog like this:
private OnClickListener mShowDialogClickListener =new OnClickListener(){
public void onClick(View v){
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(v.getContext()).
setTitle("Set IP Address").
setTitleColor(HALLOWEEN_ORANGE).
setDividerColor(HALLOWEEN_ORANGE).
setMessage("You are now entering the 10th dimension.").
setCustomView(R.layout.example_ip_address_layout, v.getContext()).
setIcon(getResources().getDrawable(R.drawable.ic_launcher));
qustomDialogBuilder.setNegativeButton("Cancel", new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
qustomDialogBuilder.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
/**
* Do something here...
*/
}
});
qustomDialogBuilder.show();
}
};
And it will look like this:
Hope you'll find this helpful. Thanks!