Wednesday, 11 January 2017

Android Tab Layout with Swipe View Example

Hello friends Today i am going to share an example of fragment please follow me step by step 


Download full source code
http://viid.me/qodCXI


Step 1 : Create a Application SwipeTab with packagename com.ritesh.swipetab and create MainActivity and activity_main.xml

Step 2 : Write this code in your build.gradle file

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.ritesh.swipetab"
minSdkVersion 11
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
}

Step 3 : Open your MainActivity and write this code

package com.ritesh.swipetab;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

MyPagerAdapter myadapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myadapter = new MyPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(myadapter);

setUpTab();

mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
getSupportActionBar().setSelectedNavigationItem(position);
}
});
}

private void setUpTab() {

final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
mViewPager.setCurrentItem(tab.getPosition());
}

public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}

public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

}
};
for (int k = 0; k < 3; k++) {
actionBar.addTab(actionBar.newTab().setText("Tab " + (k + 1))
.setTabListener(tabListener));
}

}

public class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int i) {
Fragment fragment = null;
switch (i) {
case 0:
fragment = new FragmentFirst();
break;
case 1:
fragment = new FragmentSecond();
break;
case 2:
fragment = new FragmentThird();
break;
}
return fragment;
}

@Override
public int getCount() {
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
}


Step 4 : Open your activity_main.xml and write this code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_height="match_parent"
>

<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" />
</RelativeLayout>


Step 5 : Now Create a FragmentFirst and write this code

package com.ritesh.swipetab;

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 android.widget.TextView;



public class FragmentFirst extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment1,container,false);
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);


}
}



Step 6 : Now Create a fragment1.xml and write this code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#FFFFFF"android:layout_height="match_parent">

<TextView
android:text="Fragment 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="#000"android:id="@+id/textView1"
android:layout_weight="1" />
</LinearLayout>




Step 7 : Now Create a FragmentFirst and write this code

package com.ritesh.swipetab;

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 android.widget.TextView;



public class FragmentSecond extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment2,container,false);
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);


}
}



Step 8 : Now Create a fragment2.xml and write this code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#FFFFFF"android:layout_height="match_parent">

<TextView
android:text="Fragment 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="#000"
android:id="@+id/textView2"
android:layout_weight="1" />
</LinearLayout>



Step 9 : Now Create a FragmentThird and write this code

package com.ritesh.swipetab;

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 android.widget.TextView;



public class FragmentThird extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment3,container,false);
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

}
}



Step 10 : Now Create a fragment3.xml and write this code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#FFFFFF"android:layout_height="match_parent">

<TextView
android:text="Fragment 3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="#000"
android:id="@+id/textView3"
android:layout_weight="1" />
</LinearLayout>

Step 11: Write this code in your manifest file


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ritesh.swipetab">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Step 13 : styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

Step 14 : colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#bed20e</color>
</resources>

Step 15 : Run Your Project

Android Fragment Example

Hello friends Today i am going to share an example of fragment please follow me step by step .


Step 1: Create a Project with package name com.ritesh.demo

Step 2 :Create MainActivity java file and activity_main.xml
Step 3: Write this code in your build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.ritesh.demo"
minSdkVersion 10
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
}


Step 4 : Write this code in your MainActivity

package com.ritesh.demo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
public static FragmentManager fragmentmanager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFragment(new FragmentFirst());
}

private void initFragment(Fragment fragment) {
fragmentmanager=getSupportFragmentManager();
FragmentTransaction ft=fragmentmanager.beginTransaction();
ft.add(R.id.frame_container,fragment,null);
ft.commit();
}
public static void gotoFragment(Fragment fragment) {
FragmentTransaction ft=fragmentmanager.beginTransaction();
ft.replace(R.id.frame_container,fragment,null);
//for allow backpress in fragment
ft.addToBackStack(null);
ft.commit();
}
}


Step 5 : Write this code in your layout activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_container"
/>
</RelativeLayout>


Step 6 : Now Create a FragmentFirst and write this code

package com.ritesh.demo;

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 android.widget.TextView;

/**
* Created by riteshsingh on 11/01/17.
*/

public class FragmentFirst extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment1,container,false);
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//object creation
TextView textView1=(TextView)getActivity().findViewById(R.id.textView1);
textView1.setText("First Fragment,Click me");
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.gotoFragment(new FragmentSecond());
}
});

}
}



Step 7 : Now Create a fragment1.xml and write this code

<?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">

<TextView
android:text="Fragment 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:id="@+id/textView1"
android:layout_weight="1" />
</LinearLayout>




Step 8 : Now Create a FragmentFirst and write this code

package com.ritesh.demo;

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 android.widget.TextView;

/**
* Created by riteshsingh on 11/01/17.
*/

public class FragmentSecond extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment2,container,false);
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//object creation
TextView textView2=(TextView)getActivity().findViewById(R.id.textView2);
textView2.setText("Second Fragment,Click me");

textView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.gotoFragment(new FragmentThird());
}
});

}
}



Step 9 : Now Create a fragment2.xml and write this code

<?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">

<TextView
android:text="Fragment 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:id="@+id/textView2"
android:layout_weight="1" />
</LinearLayout>



Step 10 : Now Create a FragmentThird and write this code

package com.ritesh.demo;

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 android.widget.TextView;

/**
* Created by riteshsingh on 11/01/17.
*/

public class FragmentThird extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment3,container,false);
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

}
}



Step 11 : Now Create a fragment3.xml and write this code

<?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">

<TextView
android:text="Fragment 3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:id="@+id/textView3"
android:layout_weight="1" />
</LinearLayout>

Step 12: Write this code in your manifest file


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ritesh.demo">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Step 13 : styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

Step 14 : colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>


Step 15 : Now run your fragment and Understand about fragment

Android Option Menu in Fragment Example

Hello friend today i am going to share a code of option menu in Activity .Please follow me step by step 




Step 1: Create OptionMenuFragment and write this code



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;


/**
* Created by riteshsingh on 11/01/17.
*/

public class OptionMenuFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.level_new
, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_1).setVisible(true).setEnabled(true).setTitle("Menu 1");
menu.findItem(R.id.menu_2).setVisible(true).setEnabled(true).setTitle("Menu 2");
menu.findItem(R.id.menu_3).setVisible(true).setEnabled(true).setTitle("Menu 3");
menu.findItem(R.id.menu_4).setVisible(true).setEnabled(true).setTitle("Menu 4");
menu.findItem(R.id.menu_5).setVisible(true).setEnabled(true).setTitle("Menu 5");

menu.findItem(R.id.action_settings).setVisible(true).setEnabled(true).setTitle("Settings").setIcon(R.drawable.ic_action_settings);
return;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_1:
Toast.makeText(getActivity(),"Menu 1 Clicked",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_2:
Toast.makeText(getActivity(),"Menu 2 Clicked",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_3:
Toast.makeText(getActivity(),"Menu 3 Clicked",Toast.LENGTH_SHORT).show();
break;

case R.id.menu_4:
Toast.makeText(getActivity(),"Menu 4 Clicked",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_5:
Toast.makeText(getActivity(),"Menu 5 Clicked",Toast.LENGTH_SHORT).show();
break;

case R.id.action_settings:
Toast.makeText(getActivity(),"Menu Settings Clicked",Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}

Step 2: write this code in your res>menu>main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/menu_1"
android:orderInCategory="1"
android:title="Menu 1"
app:showAsAction="never" />
<item
android:id="@+id/menu_2"
android:orderInCategory="2"
android:title="Menu 2"

app:showAsAction="never" />
<item
android:id="@+id/menu_3"
android:orderInCategory="3"
android:title="Menu 3"
app:showAsAction="never" />
<item
android:id="@+id/menu_4"
android:orderInCategory="4"
android:title="Menu 4"
app:showAsAction="never" />
<item
android:id="@+id/menu_5"
android:orderInCategory="5"
android:title="Menu 5"
app:showAsAction="never" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:visible="false"
android:title="Settings"

app:showAsAction="ifRoom" />
</menu>

Step 3 : Setup your theme in manifest for material and toolbar and add appcompact in your build.gradle

 a.  Theme.AppCompat
 b.  compile 'com.android.support:appcompat-v7:25.0.0'








Android Activity Option Menu Example

Hello friend today i am going to share a code of option menu in Activity .Please follow me step by step .





Step 1: Write this code in your MainActivity




import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

/**
* Created by riteshsingh on 11/01/17.
*/

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Option Menu");
//for enable back button in toolbar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);

menu.findItem(R.id.menu_1).setVisible(true).setEnabled(true).setTitle("Menu 1");
menu.findItem(R.id.menu_2).setVisible(true).setEnabled(true).setTitle("Menu 2");
menu.findItem(R.id.menu_3).setVisible(true).setEnabled(true).setTitle("Menu 3");
menu.findItem(R.id.menu_4).setVisible(true).setEnabled(true).setTitle("Menu 4");
menu.findItem(R.id.menu_5).setVisible(true).setEnabled(true).setTitle("Menu 5");

menu.findItem(R.id.action_settings).setVisible(true).setEnabled(true).setTitle("Settings").setIcon(R.drawable.ic_action_setting);

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.menu_1) {
Toast.makeText(OptionMenuActivity.this,"Menu 1 Clicked",Toast.LENGTH_SHORT).show();
}

if (menuItem.getItemId() == R.id.menu_2) {
Toast.makeText(OptionMenuActivity.this,"Menu 2 Clicked",Toast.LENGTH_SHORT).show();
}
if (menuItem.getItemId() == R.id.menu_3) {
Toast.makeText(OptionMenuActivity.this,"Menu 3 Clicked",Toast.LENGTH_SHORT).show();
}

if (menuItem.getItemId() == R.id.menu_4) {
Toast.makeText(OptionMenuActivity.this,"Menu 4 Clicked",Toast.LENGTH_SHORT).show();
}
if (menuItem.getItemId() == R.id.menu_5) {
Toast.makeText(OptionMenuActivity.this,"Menu 5 Clicked",Toast.LENGTH_SHORT).show();
}

if (menuItem.getItemId() == R.id.action_settings) {
Toast.makeText(OptionMenuActivity.this,"Menu Setting Clicked",Toast.LENGTH_SHORT).show();
}

return super.onOptionsItemSelected(menuItem);
}}

Step 2: Create a xml file in res >menu >main.xml and write this code



<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/menu_1"
android:orderInCategory="1"
android:title="Menu 1"
app:showAsAction="never" />
<item
android:id="@+id/menu_2"
android:orderInCategory="2"
android:title="Menu 2"

app:showAsAction="never" />
<item
android:id="@+id/menu_3"
android:orderInCategory="3"
android:title="Menu 3"
app:showAsAction="never" />
<item
android:id="@+id/menu_4"
android:orderInCategory="4"
android:title="Menu 4"
app:showAsAction="never" />
<item
android:id="@+id/menu_5"
android:orderInCategory="5"
android:title="Menu 5"
app:showAsAction="never" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:visible="false"
android:title="Settings"

app:showAsAction="ifRoom" />
</menu>


Step 3: Write this code in your activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_width="match_parent"
android:background="@color/md_white_1000"
android:layout_height="match_parent">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp" />

<FrameLayout
android:layout_below="@id/toolbar"
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/txtLabel"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:textColor="@color/colorPrimary"
android:layout_height="wrap_content"
android:text="Test"
android:visibility="gone"
android:textSize="16sp" />
</FrameLayout>
</RelativeLayout>


Step 4 : Setup your theme in manifest for material and toolbar and add appcompact in your build.gradle

 a.  Theme.AppCompat
 b.  compile 'com.android.support:appcompat-v7:25.0.0'







Friday, 6 January 2017

Android Recycler GridView Example

Hello friends today i am going to share a good tutorial of recycler list view .Please follow me step by step ....

                

Donwload full source code
http://viid.me/qi0v4T
Step 1 : Create a project Recycler View Example with packagename com.ritesh.recyclerview and activity  MainActivity.java alongwith activity_main.xml layout

Step 2 : Open build.gradle and paste this code and sync your project

compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:25.1.0'


after adding this code your build.gradle look like this

apply plugin: 'com.android.application'

android {
 compileSdkVersion 25
 buildToolsVersion "25.0.2"
 defaultConfig {
 applicationId "com.ritesh.recyclerview"
 minSdkVersion 10
 targetSdkVersion 25
 versionCode 1
 versionName "1.0"
 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
 }
 buildTypes {
 release {
 minifyEnabled false
 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 }
 }
}

dependencies {
 compile fileTree(include: ['*.jar'], dir: 'libs')
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
 exclude group: 'com.android.support', module: 'support-annotations'
 })
 compile 'com.android.support:appcompat-v7:25.1.0'
 testCompile 'junit:junit:4.12'
 compile 'com.android.support:recyclerview-v7:25.1.0'
}

Step 3 : colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="colorPrimary">#3F51B5</color>
 <color name="colorPrimaryDark">#303F9F</color>
 <color name="colorAccent">#FF4081</color>
</resources>

Step 4 : dimens.xml

<resources>
<!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>


Step 5 :your  styles.xml code like this

<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>



Step 6 :your  AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.ritesh.recyclerview">

 <application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:supportsRtl="true"
 android:theme="@style/AppTheme">
 <activity android:name=".MainActivity">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>


Step 6 :Your complete MainActivity.java code like this

package com.ritesh.recyclerview;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 ArrayList<String> list=new ArrayList<>();
 for (int i=0;i<40;i++) {
 list.add("Ritesh List Item "+i);
 }


 RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
 recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(MainActivity.this,3);
recyclerView.setLayoutManager(layoutManager); MyRecyclerAdapter adapter = new MyRecyclerAdapter(MainActivity.this,list); recyclerView.setAdapter(adapter); } public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> { private ArrayList<String> list; private Context context; private int lastPosition = -1; public MyRecyclerAdapter(Context context, ArrayList<String> list1) { this.list = list1; this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false); return new ViewHolder(view); } @Override public void onBindViewHolder(MyRecyclerAdapter.ViewHolder viewHolder, final int i) { viewHolder.textView_name.setText(String.valueOf(list.get(i))); viewHolder.lnCover.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context,"Item Clicked "+i,Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return list.size(); } public class ViewHolder extends RecyclerView.ViewHolder{ public RelativeLayout lnCover; public TextView textView_name; public ViewHolder(View view) { super(view); lnCover = (RelativeLayout) view .findViewById(R.id.lnCover); textView_name = (TextView) view .findViewById(R.id.textView_name); } } } }
Step 6 :Your activity_main.xml code like this

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@android:color/white"
 >


 <android.support.v7.widget.RecyclerView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/recycler_view"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" />
</RelativeLayout>


Step 7 :Your list_item.xml code like this
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:id="@+id/lnCover" android:paddingRight="8dp" android:background="@android:color/white" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@android:drawable/ic_lock_lock" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="8dp" android:id="@+id/imageView2" /> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_marginLeft="15dp" android:layout_marginTop="8dp" android:textColor="@android:color/black" android:textSize="17sp" android:layout_below="@+id/imageView2" android:id="@+id/textView_name" /> </RelativeLayout>

Step 7 : Run your project

Recycler Horizontal ListView Example

Hello friends today i am going to share a good tutorial of horizontal recycler list view .Please follow me step by step ....



Download full Source Code
http://viid.me/qi0UWY


Step 1 : Create a project Recycler View Example with packagename com.ritesh.recyclerview and activity  MainActivity.java alongwith activity_main.xml layout

Step 2 : Open build.gradle and paste this code and sync your project

compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:25.1.0'


after adding this code your build.gradle look like this

apply plugin: 'com.android.application'

android {
 compileSdkVersion 25
 buildToolsVersion "25.0.2"
 defaultConfig {
 applicationId "com.ritesh.recyclerview"
 minSdkVersion 10
 targetSdkVersion 25
 versionCode 1
 versionName "1.0"
 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
 }
 buildTypes {
 release {
 minifyEnabled false
 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 }
 }
}

dependencies {
 compile fileTree(include: ['*.jar'], dir: 'libs')
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
 exclude group: 'com.android.support', module: 'support-annotations'
 })
 compile 'com.android.support:appcompat-v7:25.1.0'
 testCompile 'junit:junit:4.12'
 compile 'com.android.support:recyclerview-v7:25.1.0'
}

Step 3 : colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="colorPrimary">#3F51B5</color>
 <color name="colorPrimaryDark">#303F9F</color>
 <color name="colorAccent">#FF4081</color>
</resources>

Step 4 : dimens.xml

<resources>
<!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>


Step 5 :your  styles.xml code like this

<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>



Step 6 :your  AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.ritesh.recyclerview">

 <application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:supportsRtl="true"
 android:theme="@style/AppTheme">
 <activity android:name=".MainActivity">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>


Step 6 :Your complete MainActivity.java code like this

package com.ritesh.recyclerview;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 ArrayList<String> list=new ArrayList<>();
 for (int i=0;i<40;i++) {
 list.add("Ritesh List Item "+i);
 }


 RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
 recyclerView.setHasFixedSize(true);
 RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL, false);

 recyclerView.setLayoutManager(layoutManager);
 MyRecyclerAdapter adapter = new MyRecyclerAdapter(MainActivity.this,list);
 recyclerView.setAdapter(adapter);


 }

 public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
 private ArrayList<String> list;
 private Context context;
 private int lastPosition = -1;

 public MyRecyclerAdapter(Context context, ArrayList<String> list1) {
 this.list = list1;
 this.context = context;

 }

 @Override
 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
 View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
 return new ViewHolder(view);
 }

 @Override
 public void onBindViewHolder(MyRecyclerAdapter.ViewHolder viewHolder, final int i) {

 viewHolder.textView_name.setText(String.valueOf(list.get(i)));


 viewHolder.lnCover.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 Toast.makeText(context,"Item Clicked "+i,Toast.LENGTH_SHORT).show();
 }
 });
 }

 @Override
 public int getItemCount() {
 return list.size();
 }

 public class ViewHolder extends RecyclerView.ViewHolder{
 public RelativeLayout lnCover;
 public TextView textView_name;

 public ViewHolder(View view) {
 super(view);
 lnCover = (RelativeLayout) view
 .findViewById(R.id.lnCover);
 textView_name = (TextView) view
 .findViewById(R.id.textView_name);
 }
 }

 }



 }


Step 6 :Your activity_main.xml code like this

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycler_view" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> </RelativeLayout>


Step 7 :Your list_item.xml code like this
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:id="@+id/lnCover" android:paddingRight="8dp" android:background="@android:color/white" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@android:drawable/ic_lock_lock" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="8dp" android:id="@+id/imageView2" /> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_marginLeft="15dp" android:layout_marginTop="8dp" android:textColor="@android:color/black" android:textSize="17sp" android:layout_below="@+id/imageView2" android:id="@+id/textView_name" /> </RelativeLayout>

Step 7 : Run your project




Android Recycler ListView Example

Hello friends today i am going to share a good tutorial of recycler list view .Please follow me step by step ....



Download full Source code
http://viid.me/qi0Voq
Step 1 : Create a project Recycler View Example with packagename com.ritesh.recyclerview and activity  MainActivity.java alongwith activity_main.xml layout

Step 2 : Open build.gradle and paste this code and sync your project

compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:25.1.0'


after adding this code your build.gradle look like this

apply plugin: 'com.android.application'

android {
 compileSdkVersion 25
 buildToolsVersion "25.0.2"
 defaultConfig {
 applicationId "com.ritesh.recyclerview"
 minSdkVersion 10
 targetSdkVersion 25
 versionCode 1
 versionName "1.0"
 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
 }
 buildTypes {
 release {
 minifyEnabled false
 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 }
 }
}

dependencies {
 compile fileTree(include: ['*.jar'], dir: 'libs')
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
 exclude group: 'com.android.support', module: 'support-annotations'
 })
 compile 'com.android.support:appcompat-v7:25.1.0'
 testCompile 'junit:junit:4.12'
 compile 'com.android.support:recyclerview-v7:25.1.0'
}

Step 3 : colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="colorPrimary">#3F51B5</color>
 <color name="colorPrimaryDark">#303F9F</color>
 <color name="colorAccent">#FF4081</color>
</resources>

Step 4 : dimens.xml

<resources>
<!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>


Step 5 :your  styles.xml code like this

<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>



Step 6 :your  AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.ritesh.recyclerview">

 <application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:supportsRtl="true"
 android:theme="@style/AppTheme">
 <activity android:name=".MainActivity">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>


Step 6 :Your complete MainActivity.java code like this

package com.ritesh.recyclerview;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 ArrayList<String> list=new ArrayList<>();
 for (int i=0;i<40;i++) {
 list.add("Ritesh List Item "+i);
 }


 RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
 recyclerView.setHasFixedSize(true);
 RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);

 recyclerView.setLayoutManager(layoutManager);
 MyRecyclerAdapter adapter = new MyRecyclerAdapter(MainActivity.this,list);
 recyclerView.setAdapter(adapter);


 }

 public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
 private ArrayList<String> list;
 private Context context;
 private int lastPosition = -1;

 public MyRecyclerAdapter(Context context, ArrayList<String> list1) {
 this.list = list1;
 this.context = context;

 }

 @Override
 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
 View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
 return new ViewHolder(view);
 }

 @Override
 public void onBindViewHolder(MyRecyclerAdapter.ViewHolder viewHolder, final int i) {

 viewHolder.textView_name.setText(String.valueOf(list.get(i)));


 viewHolder.lnCover.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 Toast.makeText(context,"Item Clicked "+i,Toast.LENGTH_SHORT).show();
 }
 });
 }

 @Override
 public int getItemCount() {
 return list.size();
 }

 public class ViewHolder extends RecyclerView.ViewHolder{
 public RelativeLayout lnCover;
 public TextView textView_name;

 public ViewHolder(View view) {
 super(view);
 lnCover = (RelativeLayout) view
 .findViewById(R.id.lnCover);
 textView_name = (TextView) view
 .findViewById(R.id.textView_name);
 }
 }

 }



 }


Step 6 :Your activity_main.xml code like this

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycler_view" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> </RelativeLayout>


Step 7 :Your list_item.xml code like this
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:id="@+id/lnCover" android:background="@android:color/white" android:layout_height="80dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@android:drawable/ic_lock_lock" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:id="@+id/imageView2" /> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:textColor="@android:color/black" android:textSize="17sp" android:layout_toRightOf="@+id/imageView2" android:id="@+id/textView_name" /> </RelativeLayout>

Step 7 : Run your project