WhatsApp Example:
File >> New >> Android Application
Enter Project name: WhatsAppExample
Package: com.ambilpurasunil.whatsappexample
Keep other default selections, click Next until you reach Finish

Copy and Past the following code in the MainActivity.java
package com.ambilpurasunil.whatsappexample;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
// Declaring Your View and Variables
private Toolbar toolbar;
private ViewPager pager;
private ViewPagerAdapter adapter;
private SlidingTabLayout tabs;
private CharSequence Titles[] = {"Calls", "Chats", "Contacts"};
int Numboftabs = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles
// fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles,
Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setViewPager(pager);
}
}
Now Create An Adapter class and Copy and Past the following code in the ViewPagerAdapter.java
package com.ambilpurasunil.whatsappexample;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import com.ambilpurasunil.slidingtab.calls.Call;
import com.ambilpurasunil.slidingtab.chats.Chat;
import com.ambilpurasunil.slidingtab.contacts.Contact;
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[];
int NumbOfTabs;
public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[],
int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
// This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Call();
case 1:
return new Chat();
case 2:
return new Contact();
}
return null;
}
// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
}
Now Create a Fragment Class Called Chat , Copy and Past the following code in the Chat.java
package com.ambilpurasunil.whatsappexample;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Chat extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.chat,container,false);
return v;
}
}
Now Create Fragment Class Called Call , Copy and Past the following code in the Call.java
package com.ambilpurasunil.whatsappexample;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.ambilpurasunil.slidetablistview.R;
public class Call extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.call, container, false);
return v;
}
}
Now Create Another Adapter class and Copy and Past the following code in the CustomAdapter.java
package com.ambilpurasunil.whatsappexample;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
CustomAdapter(Context context, List<RowItem> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
ImageView profile_pic;
TextView member_name;
TextView status;
TextView contactType;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.member_name = (TextView) convertView
.findViewById(R.id.member_name);
holder.profile_pic = (ImageView) convertView
.findViewById(R.id.profile_pic);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.contactType = (TextView) convertView
.findViewById(R.id.contact_type);
RowItem row_pos = rowItems.get(position);
holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
holder.member_name.setText(row_pos.getMember_name());
holder.status.setText(row_pos.getStatus());
holder.contactType.setText(row_pos.getContactType());
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
Now Create a class and Copy and Past the following code in the Contact.java
package com.ambilpurasunil.whatsappexample;
import java.util.ArrayList;
import java.util.List;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.ambilpurasunil.slidetablistview.R;
public class Contact extends ListFragment {
String[] member_names;
TypedArray profile_pics;
String[] statues;
String[] contactType;
List<RowItem> rowItems;
ListView mylistview;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.contact, container, false);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
rowItems = new ArrayList<RowItem>();
member_names = getResources().getStringArray(R.array.Member_names);
profile_pics = getResources().obtainTypedArray(R.array.profile_pics);
statues = getResources().getStringArray(R.array.statues);
contactType = getResources().getStringArray(R.array.contactType);
for (int i = 0; i < member_names.length; i++) {
RowItem item = new RowItem(member_names[i],
profile_pics.getResourceId(i, -1), statues[i],
contactType[i]);
rowItems.add(item);
}
CustomAdapter adapter = new CustomAdapter(getActivity(), rowItems);
setListAdapter(adapter);
profile_pics.recycle();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
}
Now Create a Data Type Class , Copy and Past the following code in the RowItme.java
package com.ambilpurasunil.whatsappexample;
public class RowItem {
private String member_name;
private int profile_pic_id;
private String status;
private String contactType;
public RowItem(String member_name, int profile_pic_id, String status,
String contactType) {
this.member_name = member_name;
this.profile_pic_id = profile_pic_id;
this.status = status;
this.contactType = contactType;
}
public String getMember_name() {
return member_name;
}
public void setMember_name(String member_name) {
this.member_name = member_name;
}
public int getProfile_pic_id() {
return profile_pic_id;
}
public void setProfile_pic_id(int profile_pic_id) {
this.profile_pic_id = profile_pic_id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getContactType() {
return contactType;
}
public void setContactType(String contactType) {
this.contactType = contactType;
}
}
Copy and Past the following code in the layout file called activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:padding="2dp"
app:titleMarginStart="20dp"
app:titleTextAppearance="@style/AppTheme.Toolbar.Title" />
<com.tutorialsbuzz.slidingtab.slider.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#075E54"
/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#e5e5e5" >
</android.support.v4.view.ViewPager>
</LinearLayout>
Stay Tune For Next Tutorial...WhatsAppExample-II in Android Studio
No comments:
Post a Comment