Twitter tutorial covering hashtags, reply & favorite/un-favorite using twitter4j

This article is in continuation to our previous article which demonstrated user how to authenticate on twitter and post tweet with/without image using twitter4j. This article will help you understand on how to fetch tweets using hashtags and other twitter actions like tweet, re-tweet etc . So lets begin tweeting…I mean the tutorial 🙂 #letsBegin In social media everyone is aware of #hashtags, here we will explain how to fetch tweets for a particular hash tags. we are using “Query” class to fetch tweets.


Query query = new Query(“#android”);

Here, above code snippet is to fetch tweets containing “#android” hashtag. Multiple hashtags You can also fetch tweets for more than one hashtags, say you want to fetch tweets containing hashtag #code or #android,  you need to include both tags with keyword “OR”. for eg:  “#code OR #android” ,  “#code AND #android” Now lets see the above code in action.


Query query = new Query(hashtag);

query.count(10); //  Query will return only 10 tweets per request.

QueryResult queryResultObject = twitter.search(query);

List<twitter4j.Status> qrTweets = queryResultObject.getTweets();

queryResultObject.getTweets()  will return list of twitter4j.Status, which contains tweet data  like retweet count as well as tweet’s owner info. for eg: user screen name, profile image etc Here is code snippet to fetch tweet related data from status model.


status.getUser().getName(); // to get user name

status.getId();// to get tweet id

status.getUser().getScreenName(); // to get user screen name/ user handler

status.getText();// tweet

status.getFavoriteCount(); // favorite count

status.getRetweetCount();// retweet count

PAGINATION setMaxId(int) is useful to fetch next set of tweets . this method will  return tweets with status ids less than the given id.


query.setMaxId(lastSeenId);

TWITTER ACTIONS To perform actions like reply retweet you need to authenticate user on twitter. You can find a tutorial on how to authenticate user on twitter using twitter4j here 1] Favorite To perform fav action on any tweet  you  can use createFavorite() method with tweet id.


twitterInstance.createFavorite(mTweetId)

2] Re-tweet To re-tweet tweet  use  retweetStatus() method with tweet id.


twitterInstance.retweetStatus(mTweetId)

3] Reply To reply on a tweet use updateStatus() method with tweet id.


Twitter twitter = TwitterHelper.getTwitterInstance(mContext);

StatusUpdate statusUpdate = new StatusUpdate(mReplyText);

statusUpdate.inReplyToStatusId(mTweetId);

twitter.updateStatus(statusUpdate);

4] Un-favorite To un-favorite  any tweet  you  can use destroyFavorite() method with tweet id


twitterInstance.destroyFavorite(mTweetId)

Just follow the sample, and you’re ready to tweet. Any issues feel free to contact us. You can download working project from github. Happy Coding Happy Learning 🙂

Advertisements

Listview getViewType() and getViewTypeCount() in Action

This  is an article on listview with different view’s.

We all know that listview is the most used and conventional way of displaying list  of data in android. Many-a-times we do encounter a scenario where we want to display list item depending on a specific condition for example have a look at the images below.

The common way of displaying such a data is to inflate one row and depending on the condition you may hide or make a view visible.

Toggling a view between VIEW.GONE and VIEW.VISIBLE can be a very expensive task inside the getview(..) which will sure affect the list scroll. This can be easily managed and tackled with listview’s pre-defined methods getViewTypeCount() and getItemViewType(..).

So to begin with you need to create separate layouts for each type of view that will be displayed. This way it will be more efficient and less clustered to manage the data.

To put all this into action i have created a demo project which can be downloaded here.

In this project i ‘ m showing 3 different types of row in a listview:

1. A simple text

2. Text with image view

3. Poll with three option

so first create 3 different xmls for different row type.

  • view_row_type_one.xml (for text layout)
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/textview"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="center"
 android:background="@android:color/darker_gray"
 android:textColor="@android:color/black"
 android:textSize="20sp"
 android:padding="10dp"/>

  • view_row_type_two.xml (layout with text and image )
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@android:color/darker_gray"
 android:padding="10dp" >

<ImageView
 android:id="@+id/imageView"
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:layout_centerInParent="true"
 android:layout_marginBottom="10dp"
 android:contentDescription="@null" />

<TextView
 android:id="@+id/labelTextView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignEnd="@id/imageView"
 android:layout_below="@id/imageView"
 android:layout_marginTop="15dp"
 android:textColor="@android:color/black"
 android:textSize="20sp" />

</RelativeLayout>

* view_row_type_three.xml (layout with radio buttons)

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@android:color/darker_gray"
 android:padding="10dp" >

<TextView
 android:id="@+id/questionTextView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="5dp"
 android:textColor="@android:color/black"
 android:textSize="20sp" />

<RadioButton
 android:id="@+id/radioButtonOne"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/yes"
 android:textColor="@android:color/black"
 android:textSize="20sp" />

<RadioButton
 android:id="@+id/radioButtonTwo"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/no"
 android:textColor="@android:color/black"
 android:textSize="20sp" />

<RadioButton
 android:id="@+id/radioButtonThree"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/cant_say"
 android:textColor="@android:color/black"
 android:textSize="20sp" />

</RadioGroup>

What does getViewTypeCount() and getItemViewType(…) do ?

These methods are called before getView(…).

1] getViewTypeCount()

Returns the count of different type of views. Here we are having three different type of views so this method will return count as 3.

2] getItemViewType(…) 

Here we will write the logic to determine the type of view.

@Override

public int getItemViewType(int position) {

if(mList.get(position) instanceof DataModel){

return VIEW_TYPE_TEXT;

}else if(mList.get(position) instanceof ImageModel){

return VIEW_TYPE_IMAGE;

}else{

return VIEW_TYPE_QUESTION;
}

Inside getView(…) method first we determine the type of view by calling getItemViewType(..) method, this will return the type to be inflated as shown below.

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder.DataViewHolder dataViewHolder = null;

ViewHolder.ImageViewHolder imageViewHolder = null;

ViewHolder.QuestionViewHolder questionViewHolder = null;

int type = getItemViewType(position); // to determine type of view.

if(type == VIEW_TYPE_TEXT){

if(convertView == null){

convertView = mInflater.inflate(R.layout.view_row_type_one, parent, false);

convertView.setTag(dataViewHolder);

.................

}else{

dataViewHolder = (DataViewHolder)convertView.getTag();

........

}

}else if(type == VIEW_TYPE_IMAGE){

if(convertView == null){

convertView = mInflater.inflate(R.layout.view_row_type_two, parent, false);

convertView.setTag(imageViewHolder);

..........

}else{

imageViewHolder = (ImageViewHolder)convertView.getTag();

...............

}

}else{

if(convertView == null){

convertView = mInflater.inflate(R.layout.view_row_type_three, parent, false);

convertView.setTag(questionViewHolder);

.................

}else{

questionViewHolder = (ViewHolder.QuestionViewHolder)convertView.getTag();

.......

}

}

return convertView;

}

This is all it. You can follow this mechanism of displaying data and you will notice significant improvements in the list scroll and the number of objects created. Hope this is useful to someone.

Download sample project from here.

Happy coding, happy learning. 🙂

Gmail like pull to refresh

Hey  All Here is our new library which has gmail like refresh functionality.

It’s a simple & fully customizable library  which you can use  in your app easily

You can download library & sample project from github.

gmail like pull to refresh

gmail like pull to refresh

*Steps for usage.

1] Here is the code snippet


CustomView view = new CustomView(getApplicationContext(), actionBar);

setContentView(view);

view.setRefreshListner(SampleForGmailLIkePullToRefresh.this);

view.setActionBar(SampleForGmailLIkePullToRefresh.this);

view.getListView().setAdapter(new DummyAdapter(this)); //set adapter to list.

You can implement IRefreshListner interface which has two methods preRefresh() & postRefresh() which will be invoked when loading starts and stops respectively.

You can use these methods to write logic which will be executed before and after refreshing.

Little more about CustomView class

We have exposed following  useful methods of CustomView class.

  • startLoading()  to start refreshing/loading explicitly.
  • stopLoading()  to stop refreshing/loading explicitly.
  • getListView()  this will return listview.

Its an Apache License, Version 2.0 library so you can use it in your apps the way you want. Let us know in case of any issues. Download this library from  github.

Happy Coding Happy Learning 🙂

Path like scrollbar with clock widget

Here is our library which you can use to add Path app like little clock that slides up and down the right-hand side of the screen as you scroll down the timeline. You can also add little data in the scroll panel along with fully customizable clock.

You can download library from github page.

Wthout second hand

Without second hand

      

With second hand Overview

With second hand Overview

We have used an open source library Android-ScrollBarPanel to show a scrollbar-panel. So here we go …

we have a clock class which is used to show clock  on a list view panel,

by using clock class methods you can easily customize the clock widget .

*Steps for usage.

1] Use ExtendedListView view instead of normal listview.

The ExtendedListView replaces a standard ListView widget and provides the ScrollBarPanel capability.

Here’s the xml file snippet

<com.learnNcode.android.ExtendedListView

xmlns:clock="http://schemas.android.com/apk/res-auto"

android:id="@android:id/list"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:choiceMode="singleChoice"

clock:hand_second="@drawable/ic_timer_clock_minute_hand"

clock:scrollBarPanel="@layout/scrollbarpanel"

clock:scrollBarPanelInAnimation="@anim/in"

clock:scrollBarPanelOutAnimation="@anim/out" />

As you can see we have added some custom attributes for clock widget (more information about custom attributes at bottom ).

2] You can use/edit the clock widget the following way, this should be done in the layout for scrollbar panel:

Here’s the xml file snippet:

<com.learnNcode.android.Clock

xmlns:clock="http://schemas.android.com/apk/res-auto"

android:id="@+id/analogClockScroller"

android:layout_width="25dp"

android:layout_height="25dp"

clock:hand_second="@drawable/ic_timer_clock_minute_hand"

clock:hand_minute="@drawable/ic_timer_clock_minute_hand"

clock:hand_hour="@drawable/ic_timer_clock_hour_hand"

clock:hand_dial="@drawable/ic_timer_clock_dialer"/>

You can also set drawables  for second, minute and hour hand programmatically using setImages(..) method. Also you can set drawables individually for each needle as well as clock dial.

3] Implement onPositionChangeListner this listener will  be called every time you scroll listview.

Here’s the snippet.

ExtendedListView mListView = (ExtendedListView) findViewById(android.R.id.list);

mListView.setOnPositionChangedListener(this);

Every time you scroll the list, this onPositionChanged listener get’s called in  onPositionChanged()

Then you create a clock class instance to update clock widgets value

Here is the code snippet for onPositionChanged.

@Override
public void onPositionChanged(ExtendedListView listView, int position, View scrollBarPanel) {

Clock analogClockObj = (Clock) scrollBarPanel.findViewById(R.id.analogClockScroller);

// this textview you can use to some data on list scroll panel.
TextView tv = (TextView) scrollBarPanel.findViewById(R.id.timeTextView);

tv.setText(""+position);

//this is set visibility of second hand.
analogClockObj.setSecondHandVisibility(false);

// to visible clock widget
analogClockObj.setVisibility(View.VISIBLE);

// here set Time class object with value. this time will be set as time on clock.
Time timeObj = new Time();

timeObj.set(position + 3, position, 5, 0, 0, 0);

analogClockObj.onTimeChanged(timeObj);

}

Done with clock widget:)))

** little more about clock class :

You can customize clock widget through this class .

This is fully customizable clock widget you can alter its resource through methods or  xml file.

We have exposed some basic methods which are useful to customize your clock widget

like setSecondHandVisibility(boolean) method which is used to make second hand gone or visible.

Its an Apache License, Version 2.0 library so you can use it in your apps the way you want. Let us know in case of any issues. Download this library from  github.

Happy Coding Happy Learning 🙂