Skip to main content

Posting Tweets

Lesson 19 from: Building a Twitter App

Tony Hillerson

Posting Tweets

Lesson 19 from: Building a Twitter App

Tony Hillerson

buy this class

$00

$00
Sale Ends Soon!

starting under

$13/month*

Unlock this classplus 2200+ more >

Lesson Info

19. Posting Tweets

Lesson Info

Posting Tweets

Basically what we want to do is create a little form like this, um, to post tweets. Ah, One thing is we need to We need to not limit the amount of text they can input toe 1 40 but indicate how many characters they have left because you can only post 140 characters to Twitter inside of a tweet. So we need to let the user know when there are when they're running up against the limit. Then we need a post button which will just post the tweet. And then we're gonna once the post with a tweet is posting, will show progress. And we're going to do all of this using an A sink task. So we'll set up the view that we see here or at least run through. It will make a little counter guy, which is pretty simple. And then we'll make a new a sink task to post the tweet and will show progress while the A sink task is running. And then once everything is done, we will finish the activity. So that will move back to the status list activity that was already running. Um, we're well going we're going to creat...

e a a sink task to post that tweet. And here's kind of how it's gonna look. Ah, the generics. In this case, we're gonna take a string, which is the body of text that we want a post. We're not gonna provide progress. And the return type will be a Twitter for J status. Um, the interface will be for the responder. Will be will notify the responder when the tweet is posting and we'll notify the responder when the tweet is posted. So let's look at that in action Step for Step five now. Yep. Step five, Mr. Five this week. So let's look at the basic tasks. First post tweet. A sink task. This does the actual work. Agana Jin. The types are string, um, as the argument and status is the return type. I had to prefix this the fully qualified task name here because there's also a internal class called status or maybe a genome. I can't remember. Let's defined on a sink task. So I ran into some weird problems there until I had, until I finally figured out that I needed to prefix this with the fully qualified class name that's why Have the foot fault fully Qualify class name, everyone the code. So this is going to take in the, um, in the constructor. It's going to take a responder and then an instance of the Twitter class to actually do the work through. Ah, the interface again is well, say, when the tweets posting and when the tweet is done posting um and then doing background, which is again the meat of the where the work is done, we're gonna take some of our our eggs type string, grab the tweet as the first instance of that argument over the first ah item from that array. And then you just say Twitter updates status with that string, and that's a simple as you could get. Um, we will catch any Twitter exceptions and throw a new runtime exception, which is probably not the most friendly way toe handle that problem. But it simplifies the code for our purposes and then on post execute after the execution is done, we're gonna post to the responder that the tweet was posted and pass along that status, um, on pre execute. I got kind of got these out of order here. just put these in order. So on pre execute. Um, we're going to tell the responders that the tweet is posting. So let me actually show you the, um activity. The post activity post activity is pretty simple. Um, it has a referenced to the O tweet application as a reference to a text view, which is the counter text and as a reference, the edit text, which is where the user puts in a tweet. Then it has an alert dialogue, and it has a progress dialogue. Let's go look at the view to see where all those are defined. Most of those are defined, and resource is layout. Post view is where the XML defines that view. You could see that the visual editor for Android actually works in this case. Um, we just say what's happening? Um, and then there's a detector where they can put in the tweet. And then there's a post button. So that's a pretty standard view there. Um, nothing too interesting going on. But inside the post activity, once I have referenced all those views, um, I also have to respond to the menu stuff in here. That's pretty much the same as what we already saw in the last step. Um, here are the callbacks from the a sink task. So when a tweet is posting, I'm going to create a progress dialogue. This is I think we've already seen a progress dialogue. But all you need to do is use this this constructor method, um, on the progress dialogue that passes in a context online 62. Then it passes in a title in a description. We saw that last week and then that'll show progress. And then once the tweet is actually posted, I'll get called back with the content of the tweet, which I just just discard. In this case, I don't really need it, but I'm going to dismiss the progress dialogue, and then I'm gonna use a toast, a man message, which just pops up a quick, momentary alert to the user that will fade away in a couple of seconds. We'll see that in action a little bit, and I will just say, Hey, where your tweet is posted, you were successful and then we'll finish this activity. So whatever activity we went to before or whatever activity spawned the intent that started this activity will now show Oppa's the new activity once this post activity is done. Um, okay, now, some of the more mechanics of it of this set up, um, in set up views which is called from the on create on this activity. Ah, I get a reference to the counter text and the tweet content and all that stuff. But I also do this thing here online tow line 1 12 The tweet content, which is where the user types in the status they want to put in. They want to say what's happening on Twitter? Um, they type in their tweet there. But I'm watching that with a what's called in a text changed listener. And I think we saw this in week, maybe one or two of the last class. So if you remember back, we looked at this fairly quickly. But in this case, basically what we're using this checks change listener for is it will get called when, after Texas changed before text has changed. Um, and in this case, the only thing we're actually paying attention to is when Texas being changed, which is basically when Texas being entered by the user into the and edit text component on, and we can get call backs in all these cases. But in this case, whenever the Texas being changed, I want to immediately update the counter text. How many characters air left? Um, just by counting, just by subtracting from 1 40 the length of the text Lazzari already in there. So that will keep a running total of how many characters we have left. We'll see that inaction in just a second. But let's see what happens once the post button is clicked, which, um, is wired up inside the view. We say when the post button is clicked, call this method, um, that calls post valid tweet or Worn, which is a very aptly named method that I created online. 68. Um, that basically gets the post text out of the edit text. And then we want to check that because it's not just enough to count how many characters or left. We don't want to let the user actually post if there's more than one earned 40 because it's just gonna air out on Twitter. Actually, you know what? It doesn't air out on Twitter, which is also this is what most of these Twitter clients do. What it does is actually just truncated. And there's a way to get the full text, I think. But you'll never see that full anybody's stream. So we may as well not let the user put in more than characters. So basically, we're gonna build if there. If the the post length is greater than 140 we're gonna create an alert builder for an alert dialogue through its builder class, which just says, Just possible string, which says there's too many characters, Um, and then the positive button is just going to dismiss it, but we're not actually gonna post the tweet. In the other case, if the user hits post and there's no content in the tweet, we're going to say you can't Smith something that's blank. That's kind of weird, Um, and it's probably not what you want to do anyway, so we're just probably alert. This is you're tryingto submit a blank tweet, otherwise, of everything's kosher. Then we'll kick off the post tweet a sink task which passes in ourselves as a responder, and then our instance of Twitter and then executes with the the tweet text. So let's see that guy running to see how it works. Run as Android application. And then once this gets deployed, I'll actually live in front of everybody. Post a tweet, By the way, the menu. I think I shut the menu off. How do we shut the menu? Off camera. You have to look at that code, but I am. But well, it the status is loading. I don't but the menu show. But when I click menu here and click Post, we go to the new post screen and we answer the question. What's happening? Um, so you can see as I'm typing, this counter starts counting down. And if I put in a whole bunch of text as fast as I can type, sooner or later I'll get to more than 140. So it says, I will get that there's a little bit of lag after I can keep on typing Wow! After the delete a lot of stuff. So once it gets to more than hurt 40 characters, that shows that I'm way a I'm way over, and if I had post, I get this too many characters. You need to delete something. So if I instead select all this oops, I could just cut it all off. I cut this all and then try to hit posts. It will say your tweets blank can't post an empty message. Instead, if I say something meaningful now, when the post button is clicked, we showed progress. We showed this little tweet posted toast message fades away after a little bit and the activity finishes and we go back to the status list. So that's how it's supposed to work. Are there any questions about what we saw? I'll buy away in summer? You can use this toast. Make text thing, Teoh. Quick, put up a quick notification and don't forget to call show. Look at that code really quickly. Well, well, I ask, Are there any questions about what we saw so far? You have one person was curious about what if you wanted to have the posting text Bock box on the same screen where activity is the home timeline like you like, I think if there's a couple of Twitter clients that allow you to post directly from the Holmes timeline screen, we just be adding that text box in an extra button at the bottom. Yeah, I would do. I would probably do something, like, have a view that set underneath the list and then have it be hidden until you press some button and then just have it sort of animate up or something like that. That would be kind of one way you could do it and then do the same kind of code that we have for the post activity inside that status. Less activity Question from the chat room. Um, how is auto Correct? Enabled. Oh, yeah. Let's look that. Sorry. I kind of glossed over that here in post view, XML, um, inside the edit text. I've done a few things here. Thanks for reminding me. Um, inside the in foot type, you can You can do a couple different things, Um, split up by this pipe symbol. So, for in a couple of do a couple things here, one is cap sentences, which means that I want, um, android to capitalize every sentence that I put in, but not every word. Um, the text multi lamp in the line means that this text will take out more than one line to make it scroll and don't limit it toe one line or truncate or anything like that. And then we want auto correct to work, and we want auto complete to work. So AutoCorrect will automatically correct words that you type in. Um, that android thinks air spelled wrong, which may or may not be annoying. So they give you in a way, toe override that behavior and then auto complete will work by, um, as you're typing in, like, say, um, no, actually, I'm typing from a keyboard. So instead, if I type from the soft keyboard um, actually, it's not actually showing anything, but it will usually pop something up here with a list of different words that I may be typing, um, and give me a way to complete instead of having to type the whole word. So that's how you said all this stuff up with this input type online 26 Opposed to view XML and Tony When, after you post a tweet and it goes back to the list that the home timeline, um, it if the tweet had been posted quickly enough on Twitter, are we reloading that? So that we should see it, and it just took too long. Are we not reloading? I'm not even trying to reload. I could I could reload. I just didn't want to put in that that code. Um, that could be that could be an exercise for the reader. Yes, that's a good homework. All right. Any more questions from the chat room before we wrap up and get any final questions? Um, yeah, from Beth M. In the chat room. What about copy Paste during a long touch? Um, that bad is enabled by default. Sorry. Only flip back. Um, I don't know. I might be able to shut that off, but by this long press this this stuff is all default. I didn't set this up. You get that for free? In other words,

RELATED ARTICLES

RELATED ARTICLES