I' ve got two layouts (layout_first.xml and layout_second.xml).
I want to show layout_second, after click a Button (Show Second) in layout_first. And get an EditText value in the layout_first to set EditText value in the layout_second programmaticaly. How can i do this? Thanks for your answers.
my codes:
activity_main.xml
<android.support.design.widget.CoordinatorLayout
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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="#dimen/custom_tab_layout_height"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
layout_first.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:focusable="true"
android:background="#bc8383"
android:id="#+id/content_frame">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Second"
android:id="#+id/btnShowSecond"
android:layout_weight="0.18" />
<EditText
android:id="#+id/et1"
android:text="et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:elegantTextHeight="false"
android:maxLength="20"
android:singleLine="true"
android:textAlignment="center"
android:textColor="#85b27e"
android:textSize="40dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_marginTop="10dp"
android:layout_weight="1" />
</LinearLayout>
layout_second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:background="#bc8383">
<EditText
android:id="#+id/et2"
android:text="et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:elegantTextHeight="false"
android:maxLength="20"
android:singleLine="true"
android:textAlignment="center"
android:textColor="#85b27e"
android:textSize="40dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn2"
android:id="#+id/btn2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
custom_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tab"
android:textColor="#color/colorAccent"
android:textSize="14dp"
android:gravity="center"
android:fontFamily="#string/font_fontFamily_medium"/>
MainActivity.java
package xmaxsoft.delfragment;
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.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("One");
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("Two");
tabLayout.getTabAt(1).setCustomView(tabTwo);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new First(), "One");
adapter.addFrag(new Second(), "Two");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Second.java
package xmaxsoft.delfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class Second extends Fragment {
public Second() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.layout_second, container, false);
String data = getArguments().getString("value");
et2.setText(data);
return view;
}
}
First.java
package xmaxsoft.delfragment;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class First extends Fragment {
public First() {
}
Button btnShowSecond;
EditText et1;
public void addFragment(Fragment fragment, boolean addToBackStack, String tag) {
FragmentManager manager = getFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (addToBackStack) {
ft.addToBackStack(tag);
}
ft.replace(R.id.aid1, fragment, tag);
ft.commitAllowingStateLoss();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.layout_first, container, false);
btnShowSecond = (Button) view.findViewById(R.id.btnShowSecond);
et1 = (EditText) view.findViewById(R.id.et1);
btnShowSecond.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//what kind of code must be here ???
//what kind of code must be here ???
//what kind of code must be here ???
Second myfragment = new Second();
Bundle bundle = new Bundle();
bundle.putString("value",et1.getText().toString());
myfragment.setArguments(bundle);
Activity.addFragment(myfragment, false,"Second");//error ??? cannot resolve method ???
}
});
return view;
}
}
How can i display or switch layout_second programmatically in layout_first? Thank you for your helps.
You can do this by using Intent class.
1. on First.java put this code in clicklistener
public void mybutton_click()
{
Second myfragment = new Second();
Bundle bundle = new Bundle();
bundle.putString("value",et1.getText());
myfragment.setArguments(bundle);
activity.addFragment(myfragment, false,
"Second");
}
To call other Fragment
public void addFragment(Fragment fragment, boolean addToBackStack,
String tag) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (addToBackStack) {
ft.addToBackStack(tag);
}
ft.replace(R.id.content_frame, fragment, tag);
ft.commitAllowingStateLoss();
}
In Second.java put this on Oncreate():
String data = getArguments().getString("value");
et2.setText(data);
Related
The fragment only displays one cardview when I am trying to retrieve it from the news API.
Here is the code.
Main Activity
package com.manish.newapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create an instance of the tab layout from the view.
TabLayout tabLayout = findViewById(R.id.tab_layout);
// Set the text for each tab.
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label1));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label2));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label3));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label4));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label5));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label6));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label7));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label8));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label9));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label10));
// Set the tabs to scroll through the entire layout.
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
// Use PagerAdapter to manage page views in fragments.
// Each page is represented by its own fragment.
final ViewPager viewPager = findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
// Setting a listener for clicks.
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new
TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
TabNews Fragment
package com.manish.newapp.Fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.manish.newapp.Adapter;
import com.manish.newapp.ApiClient;
import com.manish.newapp.Models.Articles;
import com.manish.newapp.Models.Headlines;
import com.manish.newapp.Models.Source;
import com.manish.newapp.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class TabNews1 extends Fragment {
View v;
RecyclerView recyclerView;
final String API_KEY = "5a1ad719eee94e7ba60d19b51f4ff159";
String sources = "bbc-news";
Adapter adapter;
List<Articles> articles = new ArrayList<>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// String country = getCountry();
// retrieveJson(country,API_KEY);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v= inflater.inflate(R.layout.fragment_tab_news1, container, false);
recyclerView = (RecyclerView)v.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
String country = getCountry();
retrieveJson(country,API_KEY);
// retrieveJson(sources,API_KEY);
// Inflate the layout for this fragment
return v;
}
public void retrieveJson(String country, String apiKey){
Call<Headlines> call = ApiClient.getInstance().getApi().getHeadlines(country,apiKey);
call.enqueue(new Callback<Headlines>() {
#Override
public void onResponse(Call<Headlines> call, Response<Headlines> response) {
if (response.isSuccessful() && response.body().getArticles() != null){
articles.clear();
articles = response.body().getArticles();
adapter = new Adapter(getContext(),articles);
recyclerView.setAdapter(adapter);
}
}
#Override
public void onFailure(Call<Headlines> call, Throwable t) {
Toast.makeText(getContext(), t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public String getCountry(){
Locale locale = Locale.getDefault();
String country = locale.getCountry();
return country.toLowerCase();
}
}
adapter Class
package com.manish.newapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.manish.newapp.Models.Articles;
import com.squareup.picasso.Picasso;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
Context context;
List<Articles> articles;
public Adapter(Context context, List<Articles> articles) {
this.context = context;
this.articles = articles;
}
#NonNull
#Override
public Adapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items_recycler,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull Adapter.ViewHolder holder, int position) {
Articles art = articles.get(position);
holder.txtTitle.setText(art.getTitle());
holder.txtDescription.setText(art.getDescription());
holder.txtDate.setText(art.getPublishedAt());
String imageUrl = art.getUrlToImage();
Picasso.with(context).load(imageUrl).into(holder.imageView);
}
#Override
public int getItemCount() {
return articles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtTitle, txtDescription, txtDate, txtTime;
ImageView imageView;
CardView cardView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
txtTitle = (TextView)itemView.findViewById(R.id.title_text);
txtDescription = (TextView)itemView.findViewById(R.id.desc);
txtTime = (TextView)itemView.findViewById(R.id.time);
txtDate = (TextView)itemView.findViewById(R.id.date);
imageView = (ImageView)itemView.findViewById(R.id.image_news);
cardView = (CardView)itemView.findViewById(R.id.cardView);
}
}
}
Pager Adapter class
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import com.manish.newapp.Fragments.TabNews1;
import com.manish.newapp.Fragments.TabNews10;
import com.manish.newapp.Fragments.TabNews2;
import com.manish.newapp.Fragments.TabNews3;
import com.manish.newapp.Fragments.TabNews4;
import com.manish.newapp.Fragments.TabNews5;
import com.manish.newapp.Fragments.TabNews6;
import com.manish.newapp.Fragments.TabNews7;
import com.manish.newapp.Fragments.TabNews8;
import com.manish.newapp.Fragments.TabNews9;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(#NonNull FragmentManager fm,int NumOfTabs) {
super(fm, NumOfTabs);
this.mNumOfTabs = NumOfTabs;
}
#NonNull
/**
* Return the Fragment associated with a specified position.
*
* #param position
*/
#Override
public Fragment getItem(int position) {
switch (position){
case 0 : return new TabNews1();
case 1 : return new TabNews2();
case 2 : return new TabNews3();
case 3 : return new TabNews4();
case 4 : return new TabNews5();
case 5 : return new TabNews6();
case 6 : return new TabNews7();
case 7 : return new TabNews8();
case 8 : return new TabNews9();
case 9 : return new TabNews10();
default: return null;
}
}
/**
* Return the number of views available.
*/
#Override
public int getCount() {
return mNumOfTabs;
}
}
Here are my layout files.
item_recycler.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView"
android:layout_margin="16dp"
android:padding="10dp"
app:cardElevation="4dp"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/image_news"
android:src="#drawable/image"
android:scaleType="centerCrop"/>
<!-- <FrameLayout-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:id="#+id/frame_layout"-->
<!-- android:layout_below="#+id/image_news"-->
<!-- android:padding="5dp">-->
<!-- <RelativeLayout-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content">-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TITLE"
android:textSize="20dp"
android:layout_below="#+id/image_news"
android:textStyle="bold"
android:id="#+id/title_text"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/desc"
android:text="DESCRIPTION"
android:layout_marginTop="10dp"
android:textSize="14dp"
android:layout_below="#+id/title_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/time"
android:layout_below="#+id/desc"
android:text="TIME"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/date"
android:layout_below="#+id/time"
android:text="DATE"
android:layout_marginTop="5dp"/>
<!-- </RelativeLayout>-->
<!-- </FrameLayout>-->
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
fragment_tab_news1.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.TabNews1"
android:background="#color/colorBackground">
<!-- TODO: Update blank fragment layout -->
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- <TextView-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="BBC NEWS"-->
<!-- android:textStyle="bold"-->
<!-- android:textSize="17sp"-->
<!-- android:layout_marginLeft="16dp"-->
<!-- android:layout_marginRight="16dp"-->
<!-- android:layout_marginTop="10dp"-->
<!-- android:fontFamily="sans-serif-light"/>-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
Mainactivity.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/relative_main"
android:padding="16dp"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/tab_layout"/>
</RelativeLayout>
Please help me with this, I am unable to find whats wrong here.
I am unable to remove a present fragment in my second fragment while trying to replace it from the first fragment along with trying to open the second fragment from the first fragment on a click of a button.
Here is my code
FirstFragment.java
package com.example.testanderase;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.List;
import java.util.Objects;
public class FirstFragment extends Fragment implements View.OnClickListener{
TextInputEditText inputEditText;
Button enterButton;
String getMessage;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.first_fragment,container,false);
inputEditText = view.findViewById(R.id.enter_edit_txt_input);
enterButton = view.findViewById(R.id.first_fragment_btn);
enterButton.setOnClickListener(this);
return view;
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public void onClick(View v) {
getMessage = inputEditText.getText().toString();
SecondFragment secondFragment = new SecondFragment();
Bundle b = new Bundle();
b.putString("data",getMessage);
secondFragment.setArguments(b);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.second_fragment_layout,secondFragment);
//transaction.add(R.id.second_fragment_layout,secondFragment);
transaction.commit();
***Below here I am trying to change the code
***In order to open the second fragment from the first fragment
/assert getFragmentManager() != null;
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.second_fragment_layout,secondFragment).commit();/
//fm.beginTransaction().replace(SecondFragment.newInstance());
}
}
first_fragment.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:gravity="center">
<LinearLayout
android:id="#+id/first_fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp">
<android.support.design.widget.TextInputEditText
android:id="#+id/enter_edit_txt_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/input"
android:textAlignment="center"
android:gravity="center_horizontal" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/first_fragment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/enter"
android:background="#color/black"
android:textColor="#color/white"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
</LinearLayout>
</LinearLayout>
SecondFragment.java
package com.example.testanderase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SecondFragment extends Fragment implements View.OnClickListener{
TextView textView;
Button enterButton;
String firstFragmentValue;
Toast t;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.second_fragment,container,false);
textView = view.findViewById(R.id.second_fragment_txt_view);
enterButton = view.findViewById(R.id.second_fragment_btn);
enterButton.setOnClickListener(this);
//assert getArguments() != null;
//firstFragmentValue = getArguments().getString("data");
//textView.setText(firstFragmentValue);
Bundle bundle = getArguments();
if(bundle != null)
{
String name = bundle.getString("data");
textView.setText(name);
Toast.makeText(getContext(),"passed data "+name,Toast.LENGTH_SHORT).show();
}
//textView.setText(firstFragmentValue);
return view;
}
#Override
public void onClick(View v) {
//
ToastButton();
/*assert getFragmentManager() != null;
FirstFragment firstFragment = new FirstFragment();
FragmentManager fragmentTransaction = getFragmentManager();
fragmentTransaction.beginTransaction().replace(R.id.first_fragment_layout, firstFragment).commit();*/
}
public void ToastButton()
{
if(t!=null)
{
t.cancel();
}
else
{
Toast.makeText(getContext(),"Clicked",Toast.LENGTH_SHORT).show();
}
}
}
second_fragment.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:gravity="center">
<TextView
android:id="#+id/second_fragment_txt_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:text="#string/change_text"
android:textAlignment="center"
android:gravity="center_horizontal"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
<Button
android:id="#+id/second_fragment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/back_button_text"
android:background="#color/black"
android:textColor="#color/white"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
</LinearLayout>
SectionPageAdapter.java
package com.example.testanderase;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class SectionPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentListTitle = new ArrayList<>();
public void addFragment(Fragment fragment, String title)
{
fragmentList.add(fragment);
fragmentListTitle.add(title);
}
public SectionPageAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return fragmentListTitle.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
}
MainActivity.java
package com.example.testanderase;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity{
private SectionPageAdapter mSectionPageAdapter;
private ViewPager mainViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionPageAdapter = new SectionPageAdapter(getSupportFragmentManager());
mainViewPager = findViewById(R.id.container);
setMainViewPager(mainViewPager);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mainViewPager);
}
public void setMainViewPager(ViewPager viewPager)
{
SectionPageAdapter adapter = new SectionPageAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstFragment(),"FIRST");
adapter.addFragment(new SecondFragment(),"SECOND");
viewPager.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity"
android:id="#+id/main_layout"
android:layout_margin="20dp">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
where is R.id.second_fragment_layout
anyway change your second_fragment.xml to this
<?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:gravity="center">
<LinearLayout
android:id="#+id/second_fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/second_fragment_txt_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:text="hello"
android:textAlignment="center"
android:gravity="center_horizontal"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
<Button
android:id="#+id/second_fragment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="back"
android:background="#000"
android:textColor="#fff"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
</LinearLayout>
</LinearLayout>
and to open the second fragment from the first fragment on a click of a button
first make mainViewPager static
static ViewPager mainViewPager
put this on first fragment onclick
int page =1;
mainViewPager.setCurrentItem(page);
to back to first fragment
put this on second fragment onclick
int page =0;
mainViewPager.setCurrentItem(page);
ts working for me.!
IN this layout i have made tab layout with fragment activity and wanted to display few things,however nothing is being shown in fragment activity.
This is the base xml where the fragment is being made change.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_change"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseParallaxMultiplier="0.5"
tools:context="com.example.alpit.formula2.Change">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/change_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/change"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.NoActionBar.PopupOverlay"></android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/change_tab"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/change_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/change_appbar"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_change"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation_menu"
app:theme="#style/NavigationTheme">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
This is the java code of the tab layout
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Distance extends Fragment {
protected View onCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
return inflater.inflate(R.layout.activity_distance, container, false);
}
}
This is the xml code of distance
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_distance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.alpit.formula2.Distance"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg"
android:textSize="100sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
</LinearLayout>
This is my java code of adding tabs in the layout
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
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.Toolbar;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
class Change extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private ActionBarDrawerToggle toggle3;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change);
Toolbar toolbar = (Toolbar) findViewById(R.id.change);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.activity_change);
toggle3 = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.Close);
drawerLayout.addDrawerListener(toggle3);
toggle3.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_change);
navigationView.setItemIconTintList(null);
navigationView.setNavigationItemSelectedListener(this);
setTitle("Conversion");
viewPager = (ViewPager) findViewById(R.id.change_viewpager);
setUpViewpager(viewPager);
TabLayout tabs = (TabLayout) findViewById(R.id.change_tab);
tabs.setupWithViewPager(viewPager);
}
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (toggle3.onOptionsItemSelected(menuItem)) {
return true;
}
return onOptionsItemSelected(menuItem);
}
private void setUpViewpager(ViewPager upViewpager) {
Adapter_tab adapter = new Adapter_tab(getSupportFragmentManager());
adapter.addFragment(new Distance(), "Distance");
adapter.addFragment(new Mass(), "Mass");
upViewpager.setAdapter(adapter);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
static class Adapter_tab extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter_tab(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentTitleList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
just add onCreateView in Distance
I have tried it with bundle. I dont know how it works. Please suggest me something. It doesnt show anything in the listview.
I have also tried it without using FragmentStatePagerAdapter.
Please check the code. There are two tabs.I want to pass String from tab1 to tab2 in a listview and populate it.
package com.example.salman.q85;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.setTabGravity(tabLayout.GRAVITY_FILL);
final ViewPager viewPager= (ViewPager)findViewById(R.id.pager);
final FAdapter adapter= new FAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setAdapter(adapter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#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_main, 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);
}
}
package com.example.salman.q85;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by Salman on 11/23/2015.
*/
public class Fragment1 extends android.support.v4.app.Fragment {
private static EditText editText;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1, container, false);
Button button = (Button) view.findViewById(R.id.button);
editText = (EditText) view.findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*String data = editText.getText().toString();
*//*
Fragment2 frag = new Fragment2();
Bundle bundle = new Bundle();
bundle.putString("n", data);*//*
*/
getdata();
editText.setText("");
/*
String data = text.getText().toString();
Fragment2 frag = (Fragment2)getFragmentManager().findFragmentById(R.id.fragment2);
frag.addIten(data);
text.setText("");;
*/
}
});
return view;
}
public String getdata() {
String s;
s = editText.getText().toString();
return s;
}
}
package com.example.salman.q85;
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.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
/**
* Created by Salman on 11/23/2015.
*/
public class Fragment2 extends Fragment {
ListView listView;
ArrayList<String> items;
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag2, container, false);
listView = (ListView) view.findViewById(R.id.listView);
items = new ArrayList<String>();
listView=(ListView)view.findViewById(R.id.listView);
listView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items));
Fragment1 f1= new Fragment1();
String data=f1.getdata();
items.add(data);
return view;
}
/* public void adddata(String s){
ArrayAdapter<String> adapter= (ArrayAdapter<String>)listView.getAdapter();
adapter.notifyDataSetChanged();
}*/
}
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">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_weight="0"
android:hint="Name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TransfertoanotherFragment"
android:id="#+id/button"
android:layout_weight="0" />
</LinearLayout>
<?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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_weight="1" />
</LinearLayout>
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/tab_layout" />
</LinearLayout>
in Fragment2 class add constructor
String data;
public Fragment2(String data) {
this.data = data;
}
and in Fragment1 pass That String in constructor
Fragment1 f1= new Fragment1(data);
In your fragment two
Fragment newFragment = new Fragment1( );
Bundle bundle = new Bundle( );
bundle.putStringArrayList( "strings", getData( ) );
fragment.setArguments( bundle );
FragmentManager fragmentManager = getActivity( );
fragmentManager.beginTransaction( ).replace( R.id.activity_main, newFragment ).commit( );
To receive the bundle in fragment one
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
view = inflater.inflate( R.layout.fragment1, container, false );false );
if ( savedInstanceState == null )
{
Bundle extras = getArguments( );
dataStrings = extras.getStringArrayList( "strings" );
}
return view;
}
i use this tutorial for making collapsing toolbar with nestedscrollview. but when i use recycleview in fragment for making tab's the toolbar not react with recycleview scrolling... how to fix that?
here my code:
layout_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.inthecheesefactory.lab.designlibrary.activity.MainActivity">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="#layout/fragment_toolbar" />
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/secondColor"
app:tabSelectedTextColor="#color/error_color" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/fab_margin_bottom"
android:layout_marginRight="#dimen/fab_margin_right"
android:src="#drawable/ic_action_ok"
app:borderWidth="0dp"
app:fabSize="normal" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/header"
app:itemIconTint="#333"
app:itemTextColor="#333"
app:menu="#menu/navigation_drawer_items" />
tabview and viewpager handle in MainActivity:
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("TAB1"));
tabLayout.addTab(tabLayout.newTab().setText("TAB2"));
tabLayout.addTab(tabLayout.newTab().setText("TAB3"));
tabLayout.addTab(tabLayout.newTab().setText("TAB4"));
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
ContactAdapter.java
package tab;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
private List<ContactInfo> contactList;
public ContactAdapter(List<ContactInfo> contactList) {
this.contactList = contactList;
}
#Override
public int getItemCount() {
return contactList.size();
}
#Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
ContactInfo ci = contactList.get(i);
contactViewHolder.vName.setText(ci.name);
contactViewHolder.vSurname.setText(ci.surname);
contactViewHolder.vEmail.setText(ci.email);
contactViewHolder.vTitle.setText(ci.name + " " + ci.surname);
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.carditem, viewGroup, false);
return new ContactViewHolder(itemView);
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
protected TextView vName;
protected TextView vSurname;
protected TextView vEmail;
protected TextView vTitle;
public ContactViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.txtName);
vSurname = (TextView) v.findViewById(R.id.txtSurname);
vEmail = (TextView) v.findViewById(R.id.txtEmail);
vTitle = (TextView) v.findViewById(R.id.title);
}
}
}
PagerAdapter.java
package tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
case 3:
TabFragment4 tab4 = new TabFragment4();
return tab4;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
tab_fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
tab_fragment_1.java
package tab;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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 java.util.ArrayList;
import java.util.List;
public class TabFragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView v =(RecyclerView) inflater.inflate(R.layout.tab_fragment_1, container, false);
SetupRecycleView(v);
return v;
}
void SetupRecycleView(RecyclerView recList) {
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(G.context);
llm.setOrientation(LinearLayoutManager.VERTICAL);
// recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
recList.setLayoutManager(llm);
List<ContactInfo> contactInfos = new ArrayList<>();
for (int i = 0; i < 10; i++) {
ContactInfo contactInfo = new ContactInfo();
contactInfo.email = "salam " + i;
contactInfo.name = "salam " + i;
contactInfo.surname = "salam " + i;
contactInfos.add(contactInfo);
}
ContactAdapter adapter = new ContactAdapter(contactInfos);
recList.setAdapter(adapter);
}
}
carditem.xml
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#color/mainColor"
android:text="contact det"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:textSize="14dp"/>
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#id/title"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="#+id/txtSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#id/txtName"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="#+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="150dp"
android:layout_alignBaseline="#id/txtName"/>
</RelativeLayout>
Go to build.gradle and make sure you're using com.android.support:recyclerview-v7:22.2.0.
It doesn't work on earlier versions.