When I press a button in my Activity, a DialogFragment pops up. Inside the dialog fragment, there is a RecyclerView that looks a like a normal list view.
The behaviour I want is that when I press on row n of the RecyclerView, the DialogFragment closes and the Activity does something based on the value associated with row n.
It seems that the ViewHolder has to implement the OnClickListener interface. When a row is clicked, the ViewHolder's delegate should do something. The delegate should be the DialogFragment. And the DialogFragment in turn talks with the Activity to which it is attached.
If this is the case, the ViewHolder has to ask the DialogFragment to do something, and the DialogFragment asks the Activity to do something. Is this the right approach? How do I pass the reference of the DialogFragment to onCreateViewHolder()? Should the Adapter keep a reference to the DialogFragment?
Yes, you are moving in the right direction. Pass the DialogFragment's reference in the constructor of the adapter. Once you have the reference and the desired click event fires, call getActivity() on the dialog's reference to get a reference to the activity. Then you can do whatever you want in the activity. Also, I suggest you implement listeners using interfaces. What you want to do is keep the DialogFragment invisible to the underlying activity and your adapter loosely coupled to the DialogFragment, and interfaces will help in that case.
I've been following the android developer guide, (specifically the Passing events back to the dialog's host) to create a DialogFragment that will contain an EditText and pass back that text.
I want my DetailFragment to call that Dialog with a button press, but implementing my DialogListener on the fragment is not enough, it must be implemented on the activity, but from the DetailActivity I can't call my button's OnClick (button is on the fragment).
So my DialogListener must be implemented on the activity, but from there I can't get to my buttons, which are on the fragment.
Do my buttons just have to be on the DetailActivity instead of on the fragment for this to work? Or is there another way to go about this?
I think this is what you are looking for
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
you need to send a message to the activity when the button is clicked, then let the activity popup the dialog
CONTEXT:
I have a simple ListActivity (call it "Activity1") that contains a ListView and a number of menu items.
Clicking an item in the ListView shows a popup dialog.
Clicking a menu item button (let's call it "Button") in the ActionBar starts an Activity (let's call it "Activity2") using startActivityForResult()
PROBLEM: If the user touches a ListView item, generating an onListItemClick() event in the ListActivity and then really quickly touches Button, a onOptionsItemSelected() event is posted to the message queue and processed in due course. This results in the following odd behaviour:
User touches ListView item
User touches Button
User sees dialog generated by onListItemClick() for a split second
Activity2 started by onOptionsItemSelected() starts over top of the dialog
When user finishes with the Activity2, he returns back to the dialog, not right back to Activity1
This is very odd and unsettling. In my experience doing UIs in older GUI frameworks, the GUI framework never allowed the second event to post to the queue so you never had to worry about these things.
QUESTION: Is there a "preferred" design pattern to prevent the user from being able to press Button and have it start Activity2 when my app is already in the process of showing a dialog in response to the ListView item select?
Disable the entire Activity1 view hierarchy until the dialog ends??
Write in your ToS that users with 2 thumbs per hand should use the app with caution.
More seriously, maybe you can check if your dialog is open before starting your activity with either a boolean or Dialog.isShowing()
This question is actually a 3 Part Question. Please suggest the valid solution for my problem.
Application Overview
I'm creating an application having a splash screen and a ListView. On click of the launcher icon, first the splash screen is shown and then after 3-4 seconds later, the splash screen calls the ListView activity and user is left with ListView items on the phone screen. On click of each item, respective textual content will be visible to the user. After reading the content user will click the "Back" button and will be returned to hte ListView items section again. If he/she wants to read any other item content, will be taken to that page on the click of that item. If not willing, then will click "Back" button on phone and application will close.
Part 1 - What I actually want to do
I want to show different textual content (not normal text but styled (bold/italized/containing images and all) on the click of different listView items. I can create the html pages and store them in my resource folder locally but don't know how to call those pages while calling onItemClickListener. I'm not sure if this is a valid approach to do this sort of work or not. Please advice. It's not working so far
Part 2 - What I'm doing instead now
Since I was not able to call the textual content so I wrote the content on some text editor, did all the fancy work on the editor and when the content was ready, I took the screenshot and then call that image instead while calling onItemClickListener. It's working fine and I'm able to see the image on click of my ListView item.
Part 3 - The problem I'm facing
When the image is shown to the user, on click of "Back" button, the application is closed instead of going back to the ListView Section. Apparently the image opening on click of the listview item is in the same activity as is the ListView itself. What I want is that user should be able to go back to the ListView section after he/she finishes reading the textual content.
Below is the code of the ListView Activity:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.menulist);
String[] values = new String[] { "1. First Article Here", "2. Second Article Here", "3. Third Article Here"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.custom_listview, R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id){
//
if(0==position)
{
setContentView(R.layout.image0);
}
else if (1==position)
{
}
}
});
}
*The code works fine upto setContentView(R.layout.image0);
* But after this I want the application to return back to the ListView section on click of "Back" button but it closes instead. Please guide.
May be it solves by,
Using layout.image0 in another activity and call by intent when click on list item.
By use this, When you click back button you view your previous activity
When you press the back button, the current activity get closed and you get back to the previous activity on the stack. Given that you are not changing activity by pressing the list element, but only changing the layout, pressing the back button empties the stack and closes your app.
The cleanest way would be to launch the splash screen in another activity.
A couple of other ways to solve it are:
- add a stack of fragments
or (the dirtiest way), override the onBackPressed(), intercept it when your app is showing the splash screen and change back the layout to the listview.
I would prefer the other approaches.
If you use setContentView() method, the back button functionality will not work, since no new Activity was created, from which you can go back to the listView. I'd rather take the clicked item's useful information, put it in an Intent (link), and call startActivity() method, to start a new activity, to show content to the user. Then, if the user clicks the back button, the "details" activity will disappear, and the user again sees the listView.
You are using setContentView so its overriding the current view with ur new View in OnItem Click...Rather add a View Group to ur activity like a Relative layout...And when u click your Itme add a new layout to ur existing layout and on back remove the layout.
Or u can Use DialogView to dynamically add a view ..see below:
Dialog dialog = new Dialog(this, android.R.style.Theme_Dialog);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, 0xff000000);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(Your VIew);
dialog.show();
Or at last you can start Another Intent using your current Activity..
Part 1 and 2
You can create complex and styled layouts using the XML files.
If you really want to use HTML, I advice you to use a WebView. You can store the plain html-string somewhere in your resources and use Webview.loadDataWithBaseUrl() to show it in your WebView.
You can make your choice depending on your functionality. If you have alot of different events to be handled (like clicking on a View), I would opt for the XML files. Because you can assign different listeners to seperate Views. When your functionality doesn't go further as it does now, only clicking a whole view. You can use a WebView as well.
Part 3
The problem is that you are abusing the setContentView-method. setContentView tells Activity which XML file it has to use. In your case, when the Activity is created, it will use R.layout.activity_main.
When you click your item, you tell the Activity.. Don't use the R.layout.activity_main anymore, but use R.layout.image0. The Activity will do this and your image will be shown.
This is not a good way to change layout though and will mess up your working flow. The reason that, when the image is shown, your application closes on Back is that there is only 1 Activity started. (Your stack contains only 1 Activity) You just switched the layout, but no new Activity is started.
Solution
Create 2 Activities:
Create a main Activity showing the your list and set the layout with setContentView(R.layout.activity_main). When your click-event is triggered, you will start the second Activity. Intent i = new Intent(...). You can also add some arguments to the Intent and access them in your second Activity to know which item was clicked and which image to show.
A detail Activity showing your content or in your case an Image. setContentView(R.layout.image0).
Solution 2
Create a popup to show your content. You can use a Dialog for that.
I've got an Activity which displays a list of custom components (in a LinearLayout). Each component represents a user in the database, and contains a button (among other things). When I click the button in the custom component, I want to pass a message back to the Activity to save the the user. The button knows the id of the user.
I was wondering how best to communicate back to the activity? In the click event of the button, should dispatch a new event (and catch it in the activity)? If so, can I add the user ID into the event? I'm new to events but I think I need to create my own custom event type perhaps?
The other way I thought of was to pass a reference for the activity into the component, so that the button can just call the method on the activity e.g 'component.parentActivity=this' but although I think it would work, I'm not sure if it's the 'proper' way to do it.
Thanks
You can implement the View.OnClickListener interface. The onClick method you'll need to implement will have the View (the thing that was clicked) passed to it. So you should have all the details about what was clicked and thus handle it the way you want from there.
What I'm not clear on in your question is what you mean by 'component'? Is that a custom View? Or a layout of Views? Or other?