Could someone help me add a m player and a setonclicklistener in a fragment. I have a couple of sound bite apps and I am trying to put them together in a Viewpager. I have the layout all set up, but im not sure on how to have the m player to the fragment. Thanks.
Sent from my SCH-I535 using Tapatalk 2
Set an onClickListener by using the setOnClickListener() method in the onCreateView() method.
If you set the onClick attribute in the xml, it will look for the method in your Activity.
The MediaPlayer requires a Context, right? Pass your Activity which you can get by calling getActivity().
u can use fragmentpagerapteradapter
to assign fragment as viewpager child...
Related
whenever I use
Code:
tv.setText("Game Over!"); setContentView(tv);
my program crashes (note: tv is an object of the TextView class)
Before this call is made the program is set to a default view that contains a text view, buttons, etc. But the crash only happens when I call the method to change the view. Any ideas?
You probably wouldnt wanna do setContentView on a textview, unless its the only thing you want on the screen. Are you creating the textview dynamically? (In the java code) Or is this a textview you create in the xml. If you are doing it dynamically, are you giving it LayoutParams first?
Yes its dynamic. What kind of LayoutParams do you mean?
Sent from my GT-I9000M using XDA App
If you just create a new textview...
TextView text = new TextView(context);
its just floating around not attached to anything. Just create a quick layout with a textview in it, and do
setContentView(R.layout.layout2);
or you could try
tv.setLayoutParams(new LayoutParams(LayoutParam.WRAP_CONTENT,LayoutParam.WRAP_CONTENT));
It still crashes, no matter what I do as soon as I try to do anything with the TextView tv, it crashes
setting it works: final TextView tv = (TextView) findViewById(R.id.stats);
But doing ANYTHING with tv crashes.
Examples:
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams. WRAP_CONTENT));
tv.setText("testing");
Anything I'm overlooking?
Well for starters you cant change anything about a variable that you declare as final..
Hi All!
I'm new here and new to the Android Eclipse/development side and I'm having a bit of a problem with a java script.
I have a small advertising app with 2 buttons (looking to expand to 4 but that's a different thread), one for Facebook (Button1) and one for Twitter (Button2) and Button2 won't work until Button1 is clicked. It works fine except for that.
Any ideas on solutions? Aggravating me a bit lol
Thanks!!
We will need to see your layout XML and java code to help out.
not sure how i'd do that atm cos im a new member? i know that some admin members get pi**y when you dont wrap any code for formatting reasons??
i've got ready and i can imagine a senior member such as yourself will notice the mistake straight away.
Use pastebin http://pastebin.com/
not sure how to use it, just tried and i got the same message about not being able to post things outside links until 10 posts and so on? even tried pasting the raw text and still wouldnt let me
any suggestions?
thanks
Or use the CODE tags: [CODE ] and [/CODE ], but without the extra spaces.
adamj1910 said:
not sure how to use it, just tried and i got the same message about not being able to post things outside links until 10 posts and so on? even tried pasting the raw text and still wouldnt let me
any suggestions?
thanks
Click to expand...
Click to collapse
Take http off or www and just paste it as text
pastebin.com/4Q0g9ryg
pastebin.com/rHvFePNM
hopefully that should work?
thanks!
You had your button definition in the wrong place for your twitter button
Code:
package com.MYAPP.MYAPP;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Set the screen's view to your xml file
Button facebookbutton = (Button) findViewById(R.id.button1); // Retrieve the button from the XML file
facebookbutton.setOnClickListener(new View.OnClickListener() { //Add a listener for when the button is pressed
[user=439709]@override[/user]
public void onClick(View v) {
sendToFacebook();
}
});
Button twitterbutton = (Button) findViewById(R.id.button2); // Retrieve the button from the XML file
twitterbutton.setOnClickListener(new View.OnClickListener() { //Add a listener for when the button is pressed
[user=439709]@override[/user]
public void onClick(View v) {
sendToTwitter();
}
});
}
protected void sendToFacebook() {
String url = "https://www.facebook.com"; // You could have this at the top of the class as a constant, or pass it in as a method variable, if you wish to send to multiple websites
Intent i = new Intent(Intent.ACTION_VIEW); // Create a new intent - stating you want to 'view something'
i.setData(Uri.parse(url)); // Add the url data (allowing android to realise you want to open the browser)
startActivity(i); // Go go go!
}
protected void sendToTwitter() {
String url = "https://www.twitter.com"; // You could have this at the top of the class as a constant, or pass it in as a method variable, if you wish to send to multiple websites
Intent i = new Intent(Intent.ACTION_VIEW); // Create a new intent - stating you want to 'view something'
i.setData(Uri.parse(url)); // Add the url data (allowing android to realise you want to open the browser)
startActivity(i); // Go go go!
}
}
Thanks for that ill give that a try!!! Really appreciate it
Sent from my GT-I9305 using xda app-developers app
it works!!! would i just repeat that for any additional buttons, same format;
Button onclicklistener etc etc etc
Button onclicklistener etc etc etc
protected void
protected void
and so on
does it work like that?
thanks!
Yes, just besure you place the code in your onCreate method for defining your buttons
Ill remember that haha thanks again really appreciate it!! =D
Sent from my GT-I9305 using xda app-developers app
zalez said:
Yes, just besure you place the code in your onCreate method for defining your buttons
Click to expand...
Click to collapse
Hey zalez
I just tried adding a youtube button in the exact same format obviously changing the button source and so on, eclipse couldn't find any problems so I ran it and it just crashed the app on the avd and on a real device?
Sent from my GT-I9305 using xda app-developers app
There are multiple ways. I like setting a listener, then using an onClick method with switch-case statements for every button.
Sent from my GT-I9300 using Tapatalk 4
you've lost me lol i understand the onclick but switch cases? if you check my .xml and .java from pastebin or the .java that another member posted further up ^ ^ ^ ^ that might show where im going wrong?
edit *** i just googled the switch case thing and ive seen it before, just not sure how i'd implement it into my .java?
thanks
bump?
adamj1910 said:
you've lost me lol i understand the onclick but switch cases? if you check my .xml and .java from pastebin or the .java that another member posted further up ^ ^ ^ ^ that might show where im going wrong?
edit *** i just googled the switch case thing and ive seen it before, just not sure how i'd implement it into my .java?
thanks
Click to expand...
Click to collapse
Sorry, I'm away. I'll look up an example from one of my apps later.
Basically, just make an onClickListener. You are then required to use that interface and implement the onClick method. Do that, you have an empty method, with a parameter like View v.
Then, make a switch statement for v.getId(), and do cases for the buttons (use their XML IDs!) that you have.
Hope that's kind of clear?
Sent from my GT-I9300 using Tapatalk 4
bassie1995 said:
Sorry, I'm away. I'll look up an example from one of my apps later.
Basically, just make an onClickListener. You are then required to use that interface and implement the onClick method. Do that, you have an empty method, with a parameter like View v.
Then, make a switch statement for v.getId(), and do cases for the buttons (use their XML IDs!) that you have.
Hope that's kind of clear?
Sent from my GT-I9300 using Tapatalk 4
Click to expand...
Click to collapse
That's actually starting to make sense! But it's just putting that into a script, not really my strong point yet, I'm more of a designer lol
It would be good if you could find some examples for URL buttons, it'd really help. Thanks!
There shouldn't have been any issues adding more. I would need to see your updated java code. Using switch cases is a good idea but at this point, if I was you, I would avoid it for now until you get the hang of the basics. Also, a log cat would be beneficial in debugging.
My app uses the ActionBarCompat and NavigationDrawer, which begs the question of how is one supposed to structure the navigation? I see two options:
1) Use a single Activity and swap out Fragments.
2) Use multiple Activities, and transition between them.
The latter seems to be the correct way, however this means that each Activity must get and build the ActionBar and NavigationDrawer when called from another Activity.
How will this affect my transition animations? If I have a left-to-right transition effect when a navigation item is clicked, will it transition the entire Activity or everything below the ActionBar?
The former seems fraught with issues. A single .java class file could get really heavy and difficult to manage.
But while we're on the subject of Fragments, please consider this situation and school me on proper Fragment management:
A blank Activity with just a LinearLayout attaches a new Fragment to it that contains a Button. Inside the Fragment class, this button gets an onClickListener and when the button is clicked, another of the same kind of Fragment should be attached to the LinearLayout inside the Activity.
A) Should the Fragment class communicate to the Activity that a new Fragment should be attached? This could be done with a custom public interface and the callback being executed within the onClickListener in the Fragment class.
B) Or, should the Fragment handle attaching the new instance inside the onClickListener, making the Activity work less? By simply bridging the clicked Button and calling .getParent() until the LinearLayout is reached, then attaching it there.
The correct way is the former. You should use fragments with the navigation drawer. If you use activities you are going to setup the navigation drawer every new activity. You should open other fragments from the activity and not from the fragment itself. I believe their is a tutorial on the android developer site that explains this in depth. A quick Google search for "android fragment to fragment communication" or something like that should get you to it. I'll try and find a link later when I am on my computer if you don't find anything
Sent from my Nexus 7 using XDA Premium HD app
I am using one edit text view and one OK button to input a large amount of user data during a setup function but can't figure out how to pause the thread execution unit the OK button is pressed. I don't want to have to register and use a ton of different buttons and listeners to call individual functions for each user input and so far I've found out the hard way that a while look will lock the UI thread and running the loop in a separate thread will not make the program wait. Any Ideas?
public class SetupMenuActivity extends Activity
{
private TextView setupPrompt;
boolean okButtonPressed = false;
@override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.setup_menu);
setup();
}
private OnClickListener okButtonListener = new OnClickListener()
{
@override
public void onClick(View v)
{
okButtonPressed = true;
}
};
private void setup()
{
Button okButton = (Button) findViewById(R.id.okButton);
okButton.setOnClickListener(okButtonListener);
setupPrompt = (TextView) findViewById(R.id.setupPrompt);
setupPrompt.setText("Please Enter Your Name");
// Make program wait for ok button clicked
setupPrompt.setText("Please Enter a Name for your Account");
}
}
What else could the user click/etc that you want to prevent from happening? If you want to block another button, then you can either do button.setClickable(false) or even button.setVisibility(View.GONE) until the ok button is clicked. Instead blocking the whole thread doesn't make much sense
The only two things the user can interact with is the button and the edit text box. I want to prevent the changing of the setupPrompt text view until the Ok button is pressed. The easy way to do it would be to put it into the onClickListener but there is a whole series of the prompts and waiting for user input so I'm trying to avoid creating a ton of different button listeners for each piece of user input.
TShipman1981 said:
The only two things the user can interact with is the button and the edit text box. I want to prevent the changing of the setupPrompt text view until the Ok button is pressed. The easy way to do it would be to put it into the onClickListener but there is a whole series of the prompts and waiting for user input so I'm trying to avoid creating a ton of different button listeners for each piece of user input.
Click to expand...
Click to collapse
The way you think this would work is not right, you have to think through it again, sorry . In Android, you can almost never wait for user events (because they might not happen). Instead, you have to do what you can during setup and everything that can only happen after a certain event has to be in the onEvent method (for instance onClick). What you can do to make it less complex is one method which is called only from the onClickListener. The method keeps track of how many times it has been called with an int step instance variable. That method has to execute what should happen at each step.
SimplicityApks said:
The way you think this would work is not right, you have to think through it again, sorry . In Android, you can almost never wait for user events (because they might not happen). Instead, you have to do what you can during setup and everything that can only happen after a certain event has to be in the onEvent method (for instance onClick). What you can do to make it less complex is one method which is called only from the onClickListener. The method keeps track of how many times it has been called with an int step instance variable. That method has to execute what should happen at each step.
Click to expand...
Click to collapse
Yeah Agreed with Simp. I would honestly make one method with all the info you need then get all the info and call it only when the button is clicked. If I knew a bit more of what your trying to accomplish I might be able to help you code it more efficiently.
The app I'm developing has a number of master-detail scenarios using the standard design-pattern. In a detail activity I need to indicate to the listview activity that an update/delete has occurred. This so that I can update the list using the adapter's setNotifyChange method. The list activity uses a callback to its parent activity where a decision is made about type of device ie; Is it a 'twoPane' scenario? This activity then calls the Fragment passing a reference to the selected row to the detail activity so it can be updated/deleted. I've tried creating a callback from this detail activity to the listview activity but this doesn't work. I assume that is because the detail activity was not invoked directly from the list activity, but from the activity containing the device decision. Maybe I'm missing something obvious. but does anyone have any suggestions for a solution.
Thanks for any help.
javabiker said:
The app I'm developing has a number of master-detail scenarios using the standard design-pattern. In a detail activity I need to indicate to the listview activity that an update/delete has occurred. This so that I can update the list using the adapter's setNotifyChange method. The list activity uses a callback to its parent activity where a decision is made about type of device ie; Is it a 'twoPane' scenario? This activity then calls the Fragment passing a reference to the selected row to the detail activity so it can be updated/deleted. I've tried creating a callback from this detail activity to the listview activity but this doesn't work. I assume that is because the detail activity was not invoked directly from the list activity, but from the activity containing the device decision. Maybe I'm missing something obvious. but does anyone have any suggestions for a solution.
Thanks for any help.
Click to expand...
Click to collapse
After thinking more about this problem I came to the idea that creating a callback path thru the various classes involved was worth trying. Will publish the results.
Have you ever used an EventBus? Like Otto from Square? https://square.github.io/otto/
There's also, https://github.com/greenrobot/EventBus. I prefer Otto.
You just register your Fragments/Activities on onResume and unregister on onPause.
Thanks for the suggestion. Coincidently, I'd been looking at Otto without realizing its applicability to this issue. Will give it a try.
Sent from my SAMSUNG-SGH-I337 using XDA-FORUM, powered by appyet.com