import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class SecondActivity extends ActionBarActivity {
private RelativeLayout mRoot;
private TextInputLayout mEmailLayout;
private TextInputLayout mPasswordLayout;
private EditText mInputEmail;
private EditText mInputPassword;
private FloatingActionButton mFAB;
private View.OnClickListener mSnackBarClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};
private View.OnClickListener mFabClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(mRoot, "FAB Clicked", Snackbar.LENGTH_SHORT)
.show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mRoot = (RelativeLayout) findViewById(R.id.root_activity_second);
mEmailLayout = (TextInputLayout) findViewById(R.id.email_layout);
mPasswordLayout = (TextInputLayout) findViewById(R.id.password_layout);
mInputEmail = (EditText) findViewById(R.id.input_email);
mInputPassword = (EditText) findViewById(R.id.input_password);
mFAB = (FloatingActionButton) findViewById(R.id.fab);
mFAB.setOnClickListener(mFabClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void submit(View view) {
boolean isEmptyEmail = isEmptyEmail();
boolean isEmptyPassword = isEmptyPassword();
if (isEmptyEmail && isEmptyPassword) {
Snackbar.make(mRoot, "One Or More Fields Are Blank", Snackbar.LENGTH_SHORT)
.setAction(getString(R.string.text_dismiss), mSnackBarClickListener)
.show();
} else if (isEmptyEmail && !isEmptyPassword) {
mEmailLayout.setError("Email Cannot Be Empty");
mPasswordLayout.setError(null);
} else if (!isEmptyEmail && isEmptyPassword) {
mPasswordLayout.setError("Password Cannot Be Empty");
mEmailLayout.setError(null);
} else {
//All Good Here
}
}
private boolean isEmptyEmail() {
return mInputEmail.getText() == null
|| mInputEmail.getText().toString() == null
|| mInputEmail.getText().toString().isEmpty();
}
private boolean isEmptyPassword() {
return mInputPassword.getText() == null
|| mInputPassword.getText().toString() == null
|| mInputPassword.getText().toString().isEmpty();
}
}
ThirdActivity.java
import android.os.Bundle;
import android.support.design.widget.TabLayout;
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.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ThirdActivity extends AppCompatActivity {
private Toolbar mToolbar;
private TabLayout mTabLayout;
private ViewPager mPager;
private MyPagerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
mAdapter = new MyPagerAdapter(getSupportFragmentManager());
mToolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(mToolbar);
mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mTabLayout.setTabsFromPagerAdapter(mAdapter);
mTabLayout.setupWithViewPager(mPager);
mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_third, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class MyFragment extends Fragment {
public static final java.lang.String ARG_PAGE = "arg_page";
public MyFragment() {
}
public static MyFragment newInstance(int pageNumber) {
MyFragment myFragment = new MyFragment();
Bundle arguments = new Bundle();
arguments.putInt(ARG_PAGE, pageNumber + 1);
myFragment.setArguments(arguments);
return myFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle arguments = getArguments();
int pageNumber = arguments.getInt(ARG_PAGE);
TextView myText = new TextView(getActivity());
myText.setText("Hello I am the text inside this Fragment " + pageNumber);
myText.setGravity(Gravity.CENTER);
return myText;
}
}
}
class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
ThirdActivity.MyFragment myFragment = ThirdActivity.MyFragment.newInstance(position);
return myFragment;
}
@Override
public int getCount() {
return 10;
}
@Override
public CharSequence getPageTitle(int position) {
return "Tab " + (position + 1);
}
}
FourthActivity.java
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
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.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class FourthActivity extends AppCompatActivity {
// Need this to link with the Snackbar
private CoordinatorLayout mCoordinator;
//Need this to set the title of the app bar
private CollapsingToolbarLayout mCollapsingToolbarLayout;
private FloatingActionButton mFab;
private Toolbar mToolbar;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ViewPager mPager;
private YourPagerAdapter mAdapter;
private TabLayout mTabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fourth);
mCoordinator = (CoordinatorLayout) findViewById(R.id.root_coordinator);
mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
mFab = (FloatingActionButton) findViewById(R.id.fab);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mToolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(mToolbar);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
mAdapter = new YourPagerAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.view_pager);
mPager.setAdapter(mAdapter);
//Notice how the Tab Layout links with the Pager Adapter
mTabLayout.setTabsFromPagerAdapter(mAdapter);
//Notice how The Tab Layout adn View Pager object are linked
mTabLayout.setupWithViewPager(mPager);
mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
mFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Notice how the Coordinator Layout object is used here
Snackbar.make(mCoordinator, "FAB Clicked", Snackbar.LENGTH_SHORT).setAction("DISMISS", null).show();
}
});
//Notice how the title is set on the Collapsing Toolbar Layout instead of the Toolbar
mCollapsingToolbarLayout.setTitle(getResources().getString(R.string.title_activity_fourth));
}
public static class MyFragment extends Fragment {
public static final java.lang.String ARG_PAGE = "arg_page";
public MyFragment() {
}
public static MyFragment newInstance(int pageNumber) {
MyFragment myFragment = new MyFragment();
Bundle arguments = new Bundle();
arguments.putInt(ARG_PAGE, pageNumber + 1);
myFragment.setArguments(arguments);
return myFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle arguments = getArguments();
int pageNumber = arguments.getInt(ARG_PAGE);
RecyclerView recyclerView = new RecyclerView(getActivity());
recyclerView.setAdapter(new YourRecyclerAdapter(getActivity()));
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}
}
}
class YourPagerAdapter extends FragmentStatePagerAdapter {
public YourPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
FourthActivity.MyFragment myFragment = FourthActivity.MyFragment.newInstance(position);
return myFragment;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
return "Tab " + (position + 1);
}
}
class YourRecyclerAdapter extends RecyclerView.Adapter<YourRecyclerAdapter.YourRecyclerViewHolder> {
private ArrayList<String> list = new ArrayList<>();
private LayoutInflater inflater;
public YourRecyclerAdapter(Context context) {
inflater = LayoutInflater.from(context);
list.add("Samset");
list.add("A.I.M.");
list.add("Samset");
list.add("Abin");
list.add("Abomination");
list.add("Samset");
list.add("Absorbing");
list.add("Adam");
list.add("Agent Bob");
list.add("Agent Zero");
list.add("Air Walker");
list.add("Samset");
list.add("Alan Scott");
list.add("Alex Mercer");
list.add("Alex Woolsly");
list.add("Alfred Pennyworth");
list.add("Allan Quartermain");
list.add("Samset");
list.add("Ammo Ando");
list.add("Masahashi Angel");
list.add("Angel Dust");
list.add("Samset");
list.add("A-Bomb");
list.add("Abe");
list.add("Abin");
list.add("Abomination");
list.add("Abraxas");
list.add("Samset");
list.add("Adam");
list.add("Agent Bob");
list.add("Agent Zero");
list.add("Samset");
list.add("Ajax");
list.add("Alan Scott");
list.add("Alex Mercer");
list.add("Alex Woolsly");
list.add("Samset");
list.add("Allan Quartermain");
list.add("Amazo");
list.add("Ammo Ando");
list.add("Masahashi Angel");
list.add("Samset");
list.add("Samset");
}
@Override
public YourRecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View root = inflater.inflate(R.layout.custom_row, viewGroup, false);
YourRecyclerViewHolder holder = new YourRecyclerViewHolder(root);
return holder;
}
@Override
public void onBindViewHolder(YourRecyclerViewHolder yourRecyclerViewHolder, int i) {
yourRecyclerViewHolder.textView.setText(list.get(i));
}
@Override
public int getItemCount() {
return list.size();
}
static class YourRecyclerViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public YourRecyclerViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text_superhero);
}
}
}
androidMeinifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cordinatedemo" >
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second" />
<activity
android:name=".ThirdActivity"
android:label="@string/title_activity_third" />
<activity
android:name=".FourthActivity"
android:label="@string/title_activity_fourth" >
</activity>
</application>
</manifest>
Finally see output...............