When click action bar icon i want display customized dialog box.(i.e)xml our dialog - android-studio

My click action bar icon I want display dialog box.
(i.e)i am using custom dialog .
for dialog box i create xml layout.
The program i tried given below...
Actually i want that when click icon in action it display the custom dialog box (custom ).
switch (item.getItemId()) {
case R.id.icon:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dailog);
dialog.setTitle("Custom Dialog Example");
Button dialogButtonCancel = (Button) dialog.findViewById(R.id.cancel);
Button dialogButtonOk = (Button) dialog.findViewById(R.id.ok);
// Click cancel to dismiss android custom dialog box
dialogButtonCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
return true;
}
When run app there no output

You have to call dialog.show() to display the dialog;
Also you can't use a return in a switch you need to use break.
Edit:
switch (item.getItemId()) {
case R.id.icon:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dailog);
dialog.setTitle("Custom Dialog Example");
Button dialogButtonCancel = (Button) dialog.findViewById(R.id.cancel);
Button dialogButtonOk = (Button) dialog.findViewById(R.id.ok);
// Click cancel to dismiss android custom dialog box
dialogButtonCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
break;
}
Edit 2: Alternatively you could use the library Material Dialogs, it's really easy to use.

Related

Soft keyboard not closing when dialog is cancelled

In my app i have a dialog where the user should enter some text in an edittext. But when I tap outside of the dialog to close the dialog, the dialog closes but the soft keyboard which popped up because i clicked on the edittext stays. It's very weird: when I set windowsoftinputmode to stateAlwaysHidden,the keyboard gets a bit transparent but it doesn't close. I only have this problem in portrait, In landscape it doesn't happen but that could be because the softkeyboard fills the whole screen. I also cant click on the keys of the keyboard it doesn't react. I already tried to set the windowsoftinputmode to different values and I set a oncancellistener on my dialo g which should close the softkeyboard but it doesn't. It's seems to me as a bug.
The code of my dialog
public void create(View view) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_SWIPE_TO_DISMISS);
dialog.setContentView(R.layout.dialoglayout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
#Override
public void onCancel(DialogInterface dialog) {
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
View view = getCurrentFocus();
if (view == null) {
view = new View(getBaseContext());
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
});
editText = dialog.findViewById(R.id.levelname);
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
name = editText.getText().toString();
callables.totaloverwriteFile(name,getApplicationContext(),"newlevelname");
getApplicationContext().startActivity(new Intent(getApplicationContext(), CreatorActivity.class));
return true;
}
return true;
}
});
}```
Try building InputMethodManager from context, something like this (kotlin)
val inputMethodManager = context?.getSystemService<InputMethodManager>()
inputMethodManager?.hideSoftInputFromWindow(view.windowToken,0)

How to change the font size of content when radio button clicks?

I have a WebView and I am loading the data from the backend.The data is very long paragraph and it is in Html format.It is working.I have to implement change font size settings to my application.When radio button named "small" clicks,text should appear in small font,when "large" clicks text should appear in large font.The content dispaly is done in ChapterDetailFragment.class.The dialg which shows the "change font option" is in Menu in actionbar and in drawer.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.font_change) {
dialog = new Dialog(HomeActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.font_change_dialog);
dialog.show();
dialog.setCanceledOnTouchOutside(true);
//here i have to write the code for clicks of radiobutton "small","large"
}
}
This is the line for loading paragraph to the array adapter
ParagraphAdapter.class
mViewHolder.mChapterContextTextView.loadData(getItem(position - 1).getContent(), "text/html; charset=UTF-8;", null);
I don't know how to implement this.Please anyone help me
in onClick of "large" radio button, save value LARGE for large.Similarly in onClick of small radio button(value SMALL for small )
public void onTextLarge(View view) {
if (fragment.getClass().getSimpleName().equalsIgnoreCase("HomeFragment")){
setSharedPreference("FONT","LARGE");
fragment = new HomeFragment();
dialog.dismiss();
}
In the adapter,
if (fontsize.equals("LARGE")) {
mViewHolder.settings = mViewHolder.mChapterContextTextView.getSettings();
mViewHolder.settings.setTextZoom(mViewHolder.settings.getTextZoom() + 30);
mViewHolder.mChapterContextTextView.loadData(getItem(position - 1).getContent(),
"text/html; charset=UTF-8;", null);
Similarly done for fontsize.equals("SMALL"). :)

(Animation) Hide Toolbar in Fragment

Hey i want to hide my toolbar when i click inside an editText Therefore i added these lines
mORMetWeight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!enterWeightClicked) {
enterWeightClicked = true;
Toolbar mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
mToolbar.setVisibility(View.GONE);
mContainer.removeView(cv1);
mContainer.removeView(cv2);
mContainer.getChildAt(0);
header.getLayoutParams().height = (int) getResources().getDimension(R.dimen.smallEnterWeightHeight);
In my onCreateView method i added the transition effect but the animation now of the Toolbar looks horrible..
LayoutTransition transition = mContainer.getLayoutTransition();
transition.enableTransitionType(LayoutTransition.CHANGING);
I simply like to let the Toolbar scroll out and the view to the top? Is this possible with the "simple" animation style?

can i put a dialog in a spinner item?

I'm new in the android world but I got experience in java.
Im trying to show a dialog when I select an especific item in my spinner but, when I do it, my application has stopped.
I have 1 fragment and 1 class for the listener, instantiate an object from the fragment in the listener and try to call the dialog.
The code is somthing like this:
//Instantiate of class Guardar extends SherlockFragment.
Guardar controlador = new Guardar();
public void onItemSelected(final AdapterView parent, View view, int pos,long id) {
String addSM = parent.getItemAtPosition(pos).toString();
if (addSM == “Anyadir”){
// custom dialog
final Dialog dialog = new Dialog(controlador.context);
dialog.setContentView(R.layout.dialog_afegirsuper);
dialog.setTitle(“Title…”);
// set the custom dialog components – text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(“Android custom dialog example!”);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
Is this possible to implement? another similar idea?
thanks a lot for any solution

Extend View and Custom Dialogs

I am working on a game where I am extending the view and performing operations in the class. I need to have a popup in the game which will have 3 buttons inside it. I have managed to have the popup show-up using custom dialogs but when I configure the onClick as follows:
private void popUp() {
Context mContext = getContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_fullimage_dialog);
dialog.setTitle("Cheese Market");
Button one = (Button)findViewById(R.id.firstpack);
one.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cheeseLeft = cheeseLeft + 10;
masterMoveLock = false;
return;
}
});
}
It force closes giving a nullpointerexeption even though it is defined in the custom_fullimage_dialog layout.
Can someone help me figure out how to have the button click detected in this scenario?
Thank you.
Try calling dialog.findViewById instead.
You're setting the contentView for the dialog, but by calling findViewById you're looking for it under your activity's content view.

Resources