Contact for Android Technical support, Free lancing and online training +919133711199, Email id: rams4android@gmail.com
Sunday, 29 June 2014
Thursday, 19 June 2014
Swipable Tab Navigation
Download this project here : SwipableTabNavigation.zip
First Add ActionBar Compat library to application follow below post
http://ramsandroid4all.blogspot.in/2014/03/actionbar-tutorial-part-01-adding.html
activity_main.xml:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
fragmenta.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffccff" >
<TextView
android:id="@+id/installedapps_customlayout_textview_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment A"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"/>
</LinearLayout>
fragmentb.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffcc"
android:orientation="vertical" >
<TextView
android:id="@+id/installedapps_customlayout_textview_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment B"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
fragmentc.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccffcc"
android:orientation="vertical" >
<TextView
android:id="@+id/installedapps_customlayout_textview_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment C"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
FragmentA.java:
package com.ram.swipingtabs;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class FragmentA extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragmenta, container, false);
return rootView;
}
}
FragmentB.java:
package com.ram.swipingtabs;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class FragmentB extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragmentb, container, false);
return rootView;
}
}
FragmentC.java:
package com.ram.swipingtabs;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class FragmentC extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragmentc, container, false);
return rootView;
}
}
TabsPagerAdapter.java:
package com.ram.swipingtabs;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int index)
{
switch (index)
{
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
}
return null;
}
@Override
public int getCount()
{
// get item count - equal to number of tabs
return 3;
}
}
MainActivity.java:
package com.ram.swipingtabs;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity implements
android.support.v7.app.ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private android.support.v7.app.ActionBar actionBar;
// Tab titles
private String[] tabs =
{ "FragmentA", "FragmentB", "FragmentC" };
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Hiding Title Bar
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
// Adding Tabs
for (String tab_name : tabs)
{
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
});
}
public void onTabReselected(android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1)
{
// TODO Auto-generated method stub
}
public void onTabSelected(android.support.v7.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction arg1)
{
// TODO Auto-generated method stub
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1)
{
// TODO Auto-generated method stub
}
}
First Add ActionBar Compat library to application follow below post
http://ramsandroid4all.blogspot.in/2014/03/actionbar-tutorial-part-01-adding.html
activity_main.xml:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
fragmenta.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffccff" >
<TextView
android:id="@+id/installedapps_customlayout_textview_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment A"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"/>
</LinearLayout>
fragmentb.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffcc"
android:orientation="vertical" >
<TextView
android:id="@+id/installedapps_customlayout_textview_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment B"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
fragmentc.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccffcc"
android:orientation="vertical" >
<TextView
android:id="@+id/installedapps_customlayout_textview_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment C"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
FragmentA.java:
package com.ram.swipingtabs;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class FragmentA extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragmenta, container, false);
return rootView;
}
}
FragmentB.java:
package com.ram.swipingtabs;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class FragmentB extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragmentb, container, false);
return rootView;
}
}
FragmentC.java:
package com.ram.swipingtabs;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class FragmentC extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragmentc, container, false);
return rootView;
}
}
TabsPagerAdapter.java:
package com.ram.swipingtabs;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int index)
{
switch (index)
{
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
}
return null;
}
@Override
public int getCount()
{
// get item count - equal to number of tabs
return 3;
}
}
MainActivity.java:
package com.ram.swipingtabs;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity implements
android.support.v7.app.ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private android.support.v7.app.ActionBar actionBar;
// Tab titles
private String[] tabs =
{ "FragmentA", "FragmentB", "FragmentC" };
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Hiding Title Bar
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
// Adding Tabs
for (String tab_name : tabs)
{
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
});
}
public void onTabReselected(android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1)
{
// TODO Auto-generated method stub
}
public void onTabSelected(android.support.v7.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction arg1)
{
// TODO Auto-generated method stub
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1)
{
// TODO Auto-generated method stub
}
}
Getting Installed Applications name and package name information from device
installedapps_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/installedapps_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
installedapps_customlayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/installedapps_customlayout_textview_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/installedapps_customlayout_textview_packagename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
GetInstalledAppsActivity.java:
package com.ram.installedapps;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.checking.R;
//Class to display installed app names and package names from device
public class GetInstalledAppsActivity extends Activity
{
// ArrayList to hold app names
ArrayList<String> appnames;
// ArrayList to hold app package names
ArrayList<String> packagenames;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set a Activity content from installedapps_activity layout
setContentView(R.layout.installedapps_activity);
// ArrayList to hold app names
appnames = new ArrayList<String>();
// ArrayList to hold app package names
packagenames = new ArrayList<String>();
// Get a handle to the listview to display installed apps information
ListView listView = (ListView) findViewById(R.id.installedapps_listView);
// Getting Global package information
PackageManager packageManager = getPackageManager();
// Get a List of all application packages that are installed on the
// device
List<ApplicationInfo> packageInfos = packageManager
.getInstalledApplications(PackageManager.GET_META_DATA);
// Getting every app information
for (ApplicationInfo a : packageInfos)
{
// To get app name
String appname = a.loadLabel(getPackageManager()).toString();
appnames.add(appname);
// To get app package name
String packageName = a.packageName;
packagenames.add(packageName);
}
// Creating Adapter object to create views in listview
CustomAdapter customAdapter = new CustomAdapter();
// Set a adapter object to the listview
listView.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter
{
@Override
public int getCount()
{
// return the all apps count
return appnames.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// Inflating installedapps_customlayout
View view = getLayoutInflater().inflate(R.layout.installedapps_customlayout,
null);
// Calling Textview references
TextView appnameTextView = (TextView) view
.findViewById(R.id.installedapps_customlayout_textview_appname);
TextView packageNameTextView = (TextView) view
.findViewById(R.id.installedapps_customlayout_textview_packagename);
// Setting data to the TextViews
appnameTextView.setText(appnames.get(position));
packageNameTextView.setText(packagenames.get(position));
return view;
}
}
}
Give following permission in AndoridManifest.xml:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/installedapps_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
installedapps_customlayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/installedapps_customlayout_textview_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/installedapps_customlayout_textview_packagename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
GetInstalledAppsActivity.java:
package com.ram.installedapps;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.checking.R;
//Class to display installed app names and package names from device
public class GetInstalledAppsActivity extends Activity
{
// ArrayList to hold app names
ArrayList<String> appnames;
// ArrayList to hold app package names
ArrayList<String> packagenames;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set a Activity content from installedapps_activity layout
setContentView(R.layout.installedapps_activity);
// ArrayList to hold app names
appnames = new ArrayList<String>();
// ArrayList to hold app package names
packagenames = new ArrayList<String>();
// Get a handle to the listview to display installed apps information
ListView listView = (ListView) findViewById(R.id.installedapps_listView);
// Getting Global package information
PackageManager packageManager = getPackageManager();
// Get a List of all application packages that are installed on the
// device
List<ApplicationInfo> packageInfos = packageManager
.getInstalledApplications(PackageManager.GET_META_DATA);
// Getting every app information
for (ApplicationInfo a : packageInfos)
{
// To get app name
String appname = a.loadLabel(getPackageManager()).toString();
appnames.add(appname);
// To get app package name
String packageName = a.packageName;
packagenames.add(packageName);
}
// Creating Adapter object to create views in listview
CustomAdapter customAdapter = new CustomAdapter();
// Set a adapter object to the listview
listView.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter
{
@Override
public int getCount()
{
// return the all apps count
return appnames.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// Inflating installedapps_customlayout
View view = getLayoutInflater().inflate(R.layout.installedapps_customlayout,
null);
// Calling Textview references
TextView appnameTextView = (TextView) view
.findViewById(R.id.installedapps_customlayout_textview_appname);
TextView packageNameTextView = (TextView) view
.findViewById(R.id.installedapps_customlayout_textview_packagename);
// Setting data to the TextViews
appnameTextView.setText(appnames.get(position));
packageNameTextView.setText(packagenames.get(position));
return view;
}
}
}
Give following permission in AndoridManifest.xml:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Thursday, 5 June 2014
Wednesday, 4 June 2014
Subscribe to:
Posts (Atom)