How to use dynamic buttons with array data - java

I am still working on my first android app and hope you can help me once more. I use the YouTube API to get all the content from my channel and create the following list:
Now my problem is I do not know how I can tell the buttons in the middle of each thumbnail to start the according video. I have the correct video ID for each element (provided in a textview on the right during troubleshooting...) but when I try to connect this information to the button onclick method it will just use the first textview content on the screen, so it is always just opening the first video => for every single button.
public void jumpin(View view) {
TextView textViewHelper = (TextView) findViewById(R.id.videoIdTester);
String textViewHelperString = textViewHelper.getText().toString();
Intent intent = YouTubeStandalonePlayer.createVideoIntent(this, API_KEY, textViewHelperString);
startActivity(intent);
}
My basic problem is that the buttons are not filled with the video ID information like the textviews for example. The buttons are just created and I call the onclick method which needs the video id then which is not reachable from my perspective as soon as the list is created.
I think this is by far too complicated at the moment but I have no plan how to solve that. I can provide more code if you need it but I think this problem is more about button and api/array behaviour in general.

Anwered by myself, using an onitemclick listener instead of the buttons ...

Related

How to add another Screen inside a Fragment?

I'm new to Android programming, and I have some questions about what type of layout to use. I am developing an application and have already done a basic mock-up of the interaction I want the user to have.
Following some tutorials I managed to build an app with a Bottom Navigation Bar with 3 Fragments, each one with it's own view. But my goals is that when the user clicks a button on one of these Fragments, the application opens a new View on the same Fragment.
What is the best way to achieve this goal? I have attached an image of the mock-up I made below, hope it helps to clarify the question. Thank you in advance for taking the time to help me!
Link to the mock-up app
That's sounds like you want to start a new Activity.
Considering you have your fragments in CurrentActivity and you want to start InvokingActivity
On clicking button in CurrentActivity
onClickMethod(View v){
Intent intent = new Intent(CurrentActivity.this, InvokingActivity.class)
//put some extras if you like
startActivity(intent)
}
To get arrow in top left corner you need to update AndroidMainfest by adding to InvokingActivity
android:parentActivityName="CurrentActivity"

Changing activities with Intent on listview button

In the application I am building I have 2 listviews with, among other elements, 2 buttons on every row, one for eliminating that row and the other to pass to another activity using an Intent.
I am detecting clicks on both buttons by setting click listeners on both buttons on the getView method of the Adapter class.
The first button was something I couldn't figure out, because I needed to identify in which listview the button was clicked and the position of the clicked row which I couldn't realize how to do.
The second one I thought would be easier since all I needed to do was Intents.
However I needed to call a method from the activity class (had to instance it) since I couldn't make Intents in a non-activity class.
This last one threw a NullPointerException.
Will post all code & logcat in a while, be right back, any possible help will be appreciated.
I will need to see your code, but basically what worked for me for being able to call intents within the code of the adapter was to use the host activity.
Something like this:
item_new = (TextView) convertView.findViewById(R.id.my_view);
Activity host = (Activity) item_new.getContext();
Intent intent = new Intent(host, MyActivity.class);
host.startActivity(intent);
If you post the code maybe I can give more details
I'm not figure out the entire problem, but if you want to start an Activity from an Adapter you could simply pass the current activity to the adapter constructor (or cast the getContext() to Activity, if you used that as context of your adapter)
In a line of code:
Activity.class.cast(getContext()).startActivity(intent);

Android : Text Fields are displaying empty when returning to previous activity

I have a problem with back button functionality in an Activity of Android. The first Activity having the Spinner to select one item from the Spinner's list and second one is the text field. I implemented search functionality using the Spinner and text field. The results are displaying fine as a ListView.
Here my Problem is:
While returning to the first Activity, the Spinner and text field are showing empty in the Activity. It should show the previous searched results.
Help me with the sample code/ links.
Dont create a new intent. You just need to call finish() from your second Activity to handle back event and move back to your first activity.
Its normal. When your first Activity goes to back ground its finished by System itself. So make sure to save your data in some place and in Activitie's onCreate() and onRestart() method reload the data to TextView and spinner..
Edits:
Create a Data class and Store your search results in String[] array or a String or how ever you like it. and make the class a singlton class. and when you come back to this screen fetch those data's and set Text of TextView and adapter for Spinner..
Shafi yes, i am calling the back function using Intent as Intent i=new
Intent (presentClass.this, previousClass.class);
Don't do this. Because the Activity stack will become like loop with same Activities started again n again. Instead just finish presentClass.. it will come back to previousClass
Just an addition to #ntc post look to onSaveInstanceState(Bundle outState) and onRestoreInstanceState(Bundle state) methods in Activity.

Android: dynamic text creation?

Let's say I have some strings in Java that I've retrieved from a web script. It doesn't matter really, they're just strings stored in variables.
Now my question is how to dynamically append them to the application (to show the user) and possibly style their position, from Java.
To draw an analogy, I want to do something similar to the following in JavaScript:
var text = document.createElement('div');
text.appendChild(document.createTextNode("some string"));
text.style.position = "whatever";
// etc, more styling
theDiv.appendChild(text); // add this new thing of text I created to the main application for the users to see
I've looked into the TextView, and I don't seem to be using it properly (I don't understand the constructor I guess?). What I want to try right now is to have the user press a button in my application and then have some text dynamically generated. Here's what I've been trying:
search_button = (Button) findViewById (R.id.backSearchButton);
search_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String test = new String("testing!");
TextView test2 = new TextView(this); //constructor is wrong, this line gives me an error
test2.setText(test);
setContentView(test2);
}
});
As you can probably tell, I don't come from much of a Java background. I'm just trying to draw parallels to stuff I would want to do for a web app.
Thanks for the help.
Try:
TextView test2 = new TextView(ParentActivity.this);
replace "ParentActivity" with the name of your activity.
your this pointer is a reference to the OnClickListener that you have anonymously created. You need a pointer to the containing Activity.
You would probably be better constructing your text view in the onCreate of your activity and just setting the text from your onClick method
I'm new to Java and Android, myself. But I'll give your question a try.
You have to decide if you want to create the TextView in XML or dynamically in Java. I think creating it in XML in the layout creator is better.
I don't think you should be using setContentView(test2).
If you create a TextView dynamically, I don't think you need to put anything in its constructor. But you do have to add the TextView to the parent view. In other words, let's say you have a LinearLayout somewhere in your layout. You have to do: linearLayout1.add(myTextView) or something.
The rest of your code seems fine. But then again, I'm still new to this, myself. Let me know how helpful this answer is, I'll try Googling for more help if it's not enough.

Trigger Textview when clicked on ListView

I'm a noob Android developer. I learned of the basic UI controls and different types of views that I can utilize.
Now my problem is, I need to show list of items using the ListView that is being shown full screen. when I click on an item it should load a TextView full screen. when I click the back button it should go back to ListView again..
Can someone point me where I can learn such stuff?? any support or even external link is greatly appreciated.
Thanks in advance.
Write your text view in a different activity. And when ever you click on a list item start that activity putting your text in the intent. Fetch the text in that text view activity in onCreate() from the intent and set it to text view. Obviously when ever you click back you 'll come back to list view activity.
Check out this website... http://www.xtensivearts.com/topics/tutorials/
He's no longer updating but what you've asked is covered in the video tutorials he did create. It's not answered directly but by watching them, you should have a solid enough understanding to piece it together yourself.
Best of luck!

Categories

Resources