how to show same Dialog in Activity and Fragment in Different situation? - dialog

I am doing a sample Project where I am using Navigation Drawer with Fragments.When I click in row item1 it opens Dialog Fragment1 .In Dialog Fragment1 I have a button .
My requirement is I want to open same Dialog which is triggred from navigation row item1 when button in Fragment is clicked ...
I am using Following code to make Dialog in Activity
public void showRegisterDialog() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_register);
dialog.show();
}
and following code to open Dialog in Fragment
private void LoadFragmentView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 1:
fragment = new Fragment2();
showRegisterDialog();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
case 4:
fragment = new Fragment5();
break;
default:
break;
}
I need some guidelines,thank you..

I did some research and got the solution.
You need to use callback method in fragment.
For this purpose,you need to use the following codes:
your_button_on_fragment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((YourActivityName)getActivity()).showRegisterDialog();
}
});

Related

How to replace a fragment from the main activity

I have a two fragments, mainactivity initially have fragment called "Dashboard fragment" Upon clicking this a button I have to replace this fragment by new fragment called "Dashboard1 fragment" but problem is Dashboard fragment is not hide/remove and now it showing both
My code
public void onClick(View view) {
Fragment fragment = null;
fragment = new Dash_Section();
replaceFragment(fragment);
}
public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
How can I remove or replace the Dashboard fragment?
Any help will be appreciated.
First of all, you have to declare a FragmentContainer in you Layout file of the MainActivity. This is the space where both fragments will be shown. Then you can instantiate both Fragments and change between them by adding:
Dashboard1Fragment dFragment = new Dashboard1Fragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, dFragment);
fragmentTransaction.commit();
This will replace the content of your FragmentContainer and instantiate a new Fragment, without overlapping both Fragments.

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)

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

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.

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

How to work with ids and strings in pagerTabStrip

Super newbie question about accessing information in strings. I have chunked together an app that uses a fragmentpager and the PagerTabStrip.
It is supposed to display titles on the tab, but mine displays nothing...because I am a total newbie hacking his way through. I am so grateful for this community.
From my layout file that calls the content (and once I fix it the title--id#pager_header right?):
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#ffffff" />
My code that gets me the swipeable pages of content:
public class Poems extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contentpage);
/** Getting a reference to the ViewPager defined the layout file */
ViewPager pager = (ViewPager) findViewById(R.id.pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
}
}
My code that should get me the titles from the string reference file.
public class MyFragment extends Fragment{
int mCurrentPage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
mCurrentPage = data.getInt("current_page", 0);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container,false);
TextView tv = (TextView ) v.findViewById(R.id.tv);
tv.setMovementMethod(new ScrollingMovementMethod());
switch(mCurrentPage){
case 0:
tv.setText(" ");
break;
case 1:
tv.setText(R.string.content_1);
break;
case 2:
tv.setText(R.string.content_2);
break;
case 3:
tv.setText(R.string.content_3);
break;
}
return tv;
}
}
How do I get pager_header from this?
A sample relevant string is:
<string name="content_1">Welcome to this app!</string>
I am thinking that I need to better understand strings since I'm coming from more of a text content background.
How do I include in the stings.xml file the indicators that allow the app to reference the content associated with pageview so that I can have that show up in the tab? How I do make it so that the content shows "Welcome to this app" but the title in the tab shows "Home"?
I think as if each entry (page) is like a line in a database with one point of reference being the title, one being the content, one being a link or some other content. Each has its own id, right?
Thank you in advance for explaining this to a beginner.
You need to implement getPageTitle(int position) in MyFragmentPagerAdapter. The String that is returned from this method is used to set the title of the PagerTabStrip.
#Override
public CharSequence getPageTitle(int position) {
Log.v(TAG, "getPageTitle");
String tabTitle;
switch(position) {
case 0:
tabTitle = "Tab #0";
break;
case 1:
tabTitle = "Home";
break;
case 2:
tabTitle = "Tab #2";
break;
case 3:
tabTitle = "Tab #3";
break;
default:
tabTitle = "Default Tab Title";
break;
}
return tabTitle;
}
You can make this more dynamic by making the title and content arguments that you pass in using a Bundle by calling setArguments(Bundle args) on your MyFragments that you add to your ViewPager. In MyFragment in onCreateView() you can then set your content based on the passed in content String argument. Then in MyFragmentPagerAdapter you can get a reference to the Fragment at the position and pull get the title directly from MyFragment.

Resources