How to show two fragment views in one activity in Android? - android

Hi i'm new to android development i need to show to fragment views in one activity. i need to show this activity immediately after launching app. Please find the attached screen. For your reference, i have added my both XML layouts below :
XML - 1
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:fillViewport="true"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/grey_dark"
android:gravity="center"
android:padding="15dp"
android:text="Day One"
android:textColor="#color/white"
android:textSize="16sp"
android:typeface="monospace" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey_line_border" />
<RelativeLayout
android:id="#+id/rlQuestion1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:text="09:00 - 09:15"
android:textColor="#drawable/list_text_top_color"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="monospace" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/time"
android:gravity="center"
android:maxLines="2"
android:padding="2dp"
android:text="Introduction & Briefing"
android:textAllCaps="true"
android:textColor="#drawable/list_text_color"
android:textSize="16sp"
android:typeface="monospace" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
XML - 2
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:fillViewport="true"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/grey_dark"
android:text="Thank you for attending this event.Kindly take a few minutes to share your feedback with us.Please fill in your contact details clearly or attach your business card.Kindly submit this form at the end of the event."
android:textColor="#color/white"
android:textSize="12sp"
android:typeface="monospace" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey_line_border" />
<com.andreabaccega.widget.FormEditText
android:id="#+id/etUserId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:ems="10"
android:hint="Name(Mr/Ms/Mdm/Dr)"
android:inputType="text"
android:padding="12dp"
android:textColor="#color/blue"
android:textColorHint="#color/grey_dark"
android:textSize="16sp"
android:typeface="monospace" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey_line_border" />
<com.andreabaccega.widget.FormEditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:ems="10"
android:hint="Company"
android:inputType="text"
android:padding="12dp"
android:textColor="#color/blue"
android:textColorHint="#color/grey_dark"
android:textSize="16sp"
android:typeface="monospace" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey_line_border" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
here is my code of main activity that i have tried :
package com.pmg.eventapp;
import android.os.Bundle;
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 com.actionbarsherlock.app.SherlockFragmentActivity;
import com.pmg.eventapp.R;
import com.viewpagerindicator.TabPageIndicator;
public class MainActivity extends SherlockFragmentActivity {
private static final String[] CONTENT = new String[] { " AGENDA ",
" FEEDBACK " };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_main);
FragmentPagerAdapter adapter = new GoogleMusicAdapter(
getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
}
#Override
public boolean onOptionsItemSelected(
com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; finish activity to go home
finish();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onResume() {
super.onResume();
// Set title
getSupportActionBar().setTitle("EVENT APP");
}
class GoogleMusicAdapter extends FragmentPagerAdapter {
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
// Voting
return new AgendaFragment();
case 1:
// QA
return new FeedbackFragment();
default:
return null;
}
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toString();
}
#Override
public int getCount() {
return CONTENT.length;
}
}
}
Please help me to create remaining two fragments.Thanks!

This is container where your fragment will be shown : main_container_activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<com.astuetz.viewpager.extensions.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:background="#drawable/background_tab" />
<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="wrap_content"
android:layout_below="#+id/tabs" />
</RelativeLayout>
</LinearLayout>
lets say it is fragment : fragment_activity
<?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:gravity="center"
android:orientation="vertical" >
<ProgressBar
android:id="#+id/progressBar_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
here is your Container which contains two fragment lets say : MainContainer
public class MainContainer extends SherlockFragmentActivity{
private PagerSlidingTabStrip tabs;
ViewPager mViewPager;
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
public static double screenInches;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.main_container_activity);
tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
tabs.setIndicatorColorResource(R.color.color_website);
tabs.setShouldExpand(true);
mViewPager = (ViewPager)findViewById(R.id.pager);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager(), MainContainer.this);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
tabs.setViewPager(mViewPager);
mViewPager.setOffscreenPageLimit(1);
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
Context privatecontext;
public AppSectionsPagerAdapter(FragmentManager fm, Context context) {
super(fm);
privatecontext = context;
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
Fragment fragment = new FragmentActivity();
return fragment;
case 1:
Fragment fragment1 = new FragmentTagsActivity();
return fragment1;
default:
return new FragmentActivity();
}
}
#Override
public int getCount() {
return 2;
}
public CharSequence getPageTitle(int position) {
String name = null;
if (position==0) {
name = privatecontext.getString(R.string.main_tab1);
} else if (position==1) {
name = privatecontext.getString(R.string.main_tab2);
}
return name;
}
}
}
FragmentActivity.java
public class FragmentTagsActivity extends SherlockFragment {
ProgressBar progressBar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_activity, container, false);
return rootView;
}
}

Related

ViewPager with FragmentPagerAdapter Not showing Content

I am trying to use Fragment
This is my activity class:
public class InterfaceAct extends FragmentActivity implements View.OnClickListener {
private ViewPager mViewPager;
private List<Fragment> datas;
private ViewPagerFragmentAdapter viewPagerFragmentAdapter;
private LinearLayout mLLHome,mLLTimer,mLLEdit,mLLAbout;
private ImageView mImageViewHome,mImageViewTimer,mImageViewEdit,mImageViewAbout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activiy_interface);
initDatas();// Init Data For Fragments
initView();// Init Component
initEvent();// Sign for Click Listener
viewPagerFragmentAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),datas);// Init Adpater Class
mViewPager.setAdapter(viewPagerFragmentAdapter);
}
private void initDatas() {
datas=new ArrayList<Fragment>();
datas.add(new MyFragment1());
datas.add(new MyFragment2());
datas.add(new MyFragment3());
datas.add(new MyFragment4());
}
private void initEvent() {
mLLHome.setOnClickListener(this);
mLLTimer.setOnClickListener(this);
mLLEdit.setOnClickListener(this);
mLLAbout.setOnClickListener(this);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {//ViewPager Scrolling Switch Listner
#Override
public void onPageSelected(int arg0) {
int currentItem=mViewPager.getCurrentItem();
resetImag();
switch (currentItem) {
case 0:
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case 1:
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case 2:
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case 3:
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mLLHome = (LinearLayout) findViewById(R.id.ll_home);
mLLTimer = (LinearLayout) findViewById(R.id.ll_timer);
mLLEdit = (LinearLayout) findViewById(R.id.ll_edit);
mLLAbout = (LinearLayout) findViewById(R.id.ll_about);
mImageViewHome = (ImageView) findViewById(R.id.img_home);
mImageViewTimer = (ImageView) findViewById(R.id.img_timer);
mImageViewEdit = (ImageView) findViewById(R.id.img_edit);
mImageViewAbout = (ImageView) findViewById(R.id.img_about);
}
#Override
public void onClick(View v) {
resetImag();
switch (v.getId()) {
case R.id.ll_home:
mViewPager.setCurrentItem(0);
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case R.id.ll_timer:
mViewPager.setCurrentItem(1);
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case R.id.ll_edit:
mViewPager.setCurrentItem(2);
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case R.id.ll_about:
mViewPager.setCurrentItem(3);
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
private void resetImag() {// Reset Picture
mImageViewHome.setImageResource(R.drawable.home_no);
mImageViewTimer.setImageResource(R.drawable.timer_no);
mImageViewEdit.setImageResource(R.drawable.edit_no);
mImageViewAbout.setImageResource(R.drawable.about_no);
}
}
The ViewPagerFragmentAdapter Looks like this:
public class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter {
private List<Fragment> datas;
public ViewPagerFragmentAdapter(FragmentManager fm,List<Fragment> datas) {
super(fm);
this.datas=datas;
}
#Override
public Fragment getItem(int position) {// Return View Object
return datas.get(position);
}
#Override
public int getCount() {// Return View Num
return datas.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {// Init View
return super.instantiateItem(container, position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {// Destroy View
super.destroyItem(container, position, object);
}
}
The layout for the page, it includes a viewpager and also a menu bar in linear layout:
?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.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#ffffffff"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/ll_home"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_home"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/home_yes" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_timer"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_timer"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/timer_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timer"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_edit"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_edit"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/edit_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_about"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_about"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/about_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:textColor="#b6b3b3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
And This is one of the fragment:
public class MyFragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1,null);
}
}
I have four fragment prepared for viewpager(they are basiclly the same but with differnt layout), here is what the tab1 layout might look like:
<?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:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Chat"
android:textSize="30sp" >
</TextView>
</LinearLayout>
And I only got the menu bar when I run it, the viewpager is not set and I don't know why
Remove android:layout_weight="1" <Viewpager> and add android:orientation="vertical" to Linear layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
></android.support.v4.view.ViewPager>
//-----------

CircleIndicator to show page number in ViewPager

I am creating an Activity that uses ViewPager to show a set of fragments as a slideshow. To indicate which page to show I am using : https://github.com/ongakuer/CircleIndicator
This is the XML for the activity :
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/introduction_view_pager"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<me.relex.circleindicator.CircleIndicator
android:id="#+id/circle_indicator"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"/>
<Button
android:layout_weight="1"
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/skip_to_login"
android:onClick="skipToLogin"/>
</LinearLayout>
This is the .java file for the activity :
public class IntroductionActivity extends FragmentActivity {
static final int NO_OF_SLIDES = 4;
ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
Button mButton;
CircleIndicator mCircleIndicator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_introduction);
mButton = (Button)findViewById(R.id.login);
mCircleIndicator = (CircleIndicator)findViewById(R.id.circle_indicator);
mViewPager = (ViewPager)findViewById(R.id.introduction_view_pager);
mPagerAdapter = new SlideScreenPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mPagerAdapter);
mCircleIndicator.setViewPager(mViewPager);
mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
}
#Override
public void onBackPressed() {
if (mViewPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
}
}
public class SlideScreenPagerAdapter extends FragmentPagerAdapter {
public SlideScreenPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0 : return new IntroductionViewPagerFragmentOne();
case 1 : return new IntroductionViewPagerFragmentTwo();
case 2 : return new IntroductionViewPagerFragmentThree();
case 3 : return new IntroductionViewPagerFragmentFour();
}
return null;
}
#Override
public int getCount() {
return NO_OF_SLIDES;
}
}
}
The fragments work perfectly, but the CircleIndicator does not show. How do i get the CircleIndicator?
Use this gradle dependency
'com.viewpagerindicator:parent:2.4.1'
Paste the code snippet below in your file (I've used the same circle indicator and it works like charm).
mViewPager.setOnPageChangeListener(this);
#Override
public void onPageSelected(int position) {
circleIndicator.onPageSelected(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
XML file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<android.support.v4.view.ViewPager
android:id="#+id/introduction_view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:layout_alignParentTop="true" />
<me.relex.circleindicator.CircleIndicator
android:id="#+id/circle_indicator"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="#android:color/darker_gray" />
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.1"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:onClick="skipToLogin"
android:text="login" />
</LinearLayout>

How to hide layout when click outside of it?

In my app in need to hide layout when i click out side (MobileFragment) of that layout. You can understand the whole problem from given below images.
This is my app sturcture:
This is the problem:
Here is my code:
MainActivity.java
package com.example.admin.smsrupee;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
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.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TabLayout tabLayout;
private ViewPager viewPager;
private ImageView menu_icon,profile_icon;
private LinearLayout layout,profile_layout;
ViewPagerFinder adapter;
CoordinatorLayout coordinatorLayout;
private int[] tabIcons = {
R.drawable.mobile_icon,
R.drawable.dth_icon,
R.drawable.data_card_icon,
R.drawable.landline_icon,
R.drawable.electricity_icon,
R.drawable.gas_icon
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager)findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
menu_icon = (ImageView)findViewById(R.id.menu_icon);
profile_icon = (ImageView)findViewById(R.id.profile_icon);
layout = (LinearLayout)findViewById(R.id.menu_list);
profile_layout = (LinearLayout)findViewById(R.id.profile_menu_list);
menu_icon.setOnClickListener(this);
profile_icon.setOnClickListener(this);
viewPager.setOnClickListener(this);
coordinatorLayout.setOnClickListener(this);
setupTabIcons();
FragmentStatePagerAdapter a = (FragmentStatePagerAdapter)viewPager.getAdapter();
Fragment ft = (Fragment)a.instantiateItem(viewPager, viewPager.getCurrentItem());
System.out.println("Fragment---" + ft);
}
private void setupTabIcons() {
Drawable mobile_img;
Resources res1 = getResources();
mobile_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.mobile_icon);
mobile_img.setBounds(0, 0, 20, 25);
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("Mobile");
tabOne.setCompoundDrawables(null,mobile_img, null, null);
tabLayout.getTabAt(0).setCustomView(tabOne);
Drawable dth_img ;
Resources res2 = getResources();
dth_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.dth_icon);
dth_img.setBounds(0, 0, 20, 25);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("DTH");
tabTwo.setCompoundDrawables(null, dth_img, null, null);
tabLayout.getTabAt(1).setCustomView(tabTwo);
Drawable data_img ;
Resources res3 = getResources();
data_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.data_card_icon);
data_img.setBounds(0, 0, 20, 25);
TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText("Data Card");
tabThree.setCompoundDrawables(null, data_img, null, null);
tabLayout.getTabAt(2).setCustomView(tabThree);
Drawable landline_img ;
Resources res4 = getResources();
landline_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.landline_icon);
landline_img.setBounds(0, 0, 20, 25);
TextView tabFour = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabFour.setText("LandLine");
tabFour.setCompoundDrawables(null,landline_img, null, null);
tabLayout.getTabAt(3).setCustomView(tabFour);
Drawable electricity_img ;
Resources res5 = getResources();
electricity_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.electricity_icon);
electricity_img.setBounds(0, 0, 20, 25);
TextView tabFive = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabFive.setText("Electricity");
tabFive.setCompoundDrawables(null,electricity_img, null, null);
tabLayout.getTabAt(4).setCustomView(tabFive);
Drawable gas_img ;
Resources res6 = getResources();
gas_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.gas_icon);
gas_img.setBounds(0, 0, 20, 25);
TextView tabSix = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabSix.setText("Gas");
tabSix.setCompoundDrawables(null,gas_img , null, null);
tabLayout.getTabAt(5).setCustomView(tabSix);
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerFinder(getSupportFragmentManager());
adapter.addFragment(new MobileFragment(), "Mobile");
adapter.addFragment(new DTHFragment(), "DTH");
adapter.addFragment(new DataCardFragment(), "Data Card");
adapter.addFragment(new LandLineFragment(), "Landline");
adapter.addFragment(new ElectricityFragment(), "Electricity");
adapter.addFragment(new GasFragment(), "Gas");
viewPager.setAdapter(adapter);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.menu_icon:
if(layout.getVisibility() == View.INVISIBLE){
layout.setVisibility(View.VISIBLE);
}else{
layout.setVisibility(View.INVISIBLE);
}
break;
case R.id.profile_icon:
if(profile_layout.getVisibility() == View.INVISIBLE) {
profile_layout.setVisibility(View.VISIBLE);
} else {
profile_layout.setVisibility(View.INVISIBLE);
}
break;
default:
break;
}
}
class ViewPagerFinder extends FragmentStatePagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerFinder(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position){
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public Fragment returnFragment(int position){
return mFragmentList.get(position);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
/* if (id == R.id.action_settings) {
return true;
}*/
return super.onOptionsItemSelected(item);
}
}
MobileFragemnt.java
package com.example.admin.smsrupee;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
/**
* Created by admin on 11/4/2015.
*/
public class MobileFragment extends android.support.v4.app.Fragment {
EditText editText;
private ImageView menuicon;
private LinearLayout layout;
private RelativeLayout relativeLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.mobile_fragment, container, false);
return view;
}
public static MobileFragment newInstance(int index) {
MobileFragment f = new MobileFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cordinator"
android:clickable="true"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay">
<include layout="#layout/content_main"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#2f5392"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
</android.support.v4.view.ViewPager>
<!--<include layout="#layout/dummy_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>-->
<include layout="#layout/menu_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<include layout="#layout/profile_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
mobile_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mobile_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#2f5392"
android:id="#+id/view" />
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/linearlayout_one1"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prepaid"
android:textColor="#2f5392"
android:layout_marginRight="20dp"
android:id="#+id/prepaid_radio1"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:textColor="#2f5392"
android:layout_height="wrap_content"
android:text="Postpaid"
android:id="#+id/postpaid_radio1"
android:checked="false" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/linearlayout_two1"
android:layout_below="#+id/linearlayout_one1"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="20dp"
android:orientation="vertical"
>
<EditText
android:layout_width="300dp"
android:hint="Enter Your Mobile Number"
android:textSize="15sp"
android:text="123xxxxxx"
android:id="#+id/mobile_number1"
android:paddingLeft="30dp"
android:layout_height="wrap_content"
android:drawableRight="#drawable/editbox_icon"
/>
<EditText
android:layout_width="300dp"
android:hint="Select Operator"
android:paddingLeft="30dp"
android:id="#+id/operator1"
android:textSize="15sp"
android:layout_height="wrap_content"
android:drawableRight="#drawable/down_arrow"
/>
<EditText
android:layout_width="300dp"
android:drawableLeft="#drawable/indian_rupee"
android:hint=" Amount"
android:id="#+id/amount1"
android:textSize="15sp"
android:layout_height="wrap_content"
android:drawableRight="#drawable/browse_plan"
/>
</LinearLayout>
<Button
android:layout_below="#+id/linearlayout_two1"
android:layout_marginTop="35dp"
android:layout_width="250dp"
android:id="#+id/proceed1"
android:layout_centerHorizontal="true"
android:layout_height="37dp"
android:background="#2f5392"
android:textColor="#ffffff"
android:text="PROCEED"/>
</RelativeLayout>
content_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"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/relative_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sms_rupee_logo"
/>
<ImageView
android:id="#+id/menu_icon"
android:layout_width="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:src="#drawable/menu_icon"
/>
<ImageView
android:id="#+id/wallet_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/menu_icon"
android:src="#drawable/wallet_icon" />
<ImageView
android:id="#+id/profile_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/wallet_icon"
android:src="#drawable/profile_icon" />
</RelativeLayout>
</RelativeLayout>
profile_content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/profile_menu_list"
android:layout_width="165dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:clickable="true"
android:layout_alignParentTop="true"
android:background="#2f5392"
android:orientation="vertical"
android:visibility="invisible"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:layout_height="wrap_content"
android:src="#drawable/add_user"/>
<TextView
android:id="#+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:text="Register"
android:textColor="#fff"
android:textSize="17dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:padding="8dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/login"/>
<TextView
android:id="#+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="3dp"
android:padding="8dp"
android:text="Login"
android:textColor="#ffffff"
android:textSize="17dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:padding="8dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/referral"/>
<TextView
android:id="#+id/referrals"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:text="Referrals"
android:textColor="#ffffff"
android:textSize="17dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:padding="8dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:src="#drawable/recharge"/>
<TextView
android:id="#+id/configure_easychange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:text="Configure EasyCharge"
android:textColor="#ffffff"
android:textSize="17dp"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Firstly, you can get an instance of an Activity inside its fragment by help of getActivity() method. Use this instance to call any method of the activity from your fragment like this :-
((MainActivity) getActivity()).methodName();
Secondly, I would suggest you to convert that bar into a NavigationDrawer which will benefit you by providing you with a much cleaner user interface with more cleaner transitions.
MainActivity
public class HomeActivity extends AppCompatActivity {
public static BottomNavigationView mNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//-----------------------------------------------| Bottom navigation menu
mNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
mNavigationView.setOnNavigationItemSelectedListener(new ActionHandler());
}
private class ActionHandler implements BottomNavigationView.OnNavigationItemSelectedListener {
//-----------------------------------------------| Bottom navigation menu listener
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_menu:
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_from_left, 0).add(android.R.id.content, new MenuFragment(), "myFragmentTag").addToBackStack("tag").commit(); // without FrameLayout
return true;
case R.id.action_account:
//
return true;
}
return false;
}
}
}
fragment_menu.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:id="#+id/root_view"
android:background="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".views.fragments.MenuFragment">
<LinearLayout
android:orientation="vertical"
android:layout_marginEnd="150dp"
android:background="#color/colorBackgroundGrey"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:background="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="250dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/header_photo"
app:civ_border_width="1dp"
app:civ_border_color="#color/colorLightGrey"
app:tint="#color/colorWhite"
android:src="#drawable/ic_add_photo"
android:layout_centerInParent="true"
android:layout_width="140dp"
android:layout_height="140dp"/>
<TextView
android:id="#+id/nav_header_title"
android:textColor="#color/colorWhite"
android:layout_marginTop="10dp"
android:text="#string/app_name"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/items_recycler_view"
tools:listitem="#layout/item_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
MemuFragment
public class MenuFragment extends Fragment {
private UserModel mUser;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_menu, container, false);
//getActivity().onBackPressed();
((RelativeLayout) view.findViewById(R.id.root_view)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Utility.getInstance().onBackFragment(Objects.requireNonNull(getActivity()).getSupportFragmentManager(), new MenuFragment());
}
});
return view;
}
}
Utility
public class Utility {
private String TAG = this.getClass().getSimpleName();
private static Utility mUtility;
public static Utility getInstance() {
if (mUtility == null) {
mUtility = new Utility();
}
return mUtility;
}
//===============================================| Add fragment | Replace fragment | Remove fragment
//https://stackoverflow.com/questions/18634207/difference-between-add-replace-and-addtobackstack
public void onAddFragment(FragmentManager mManager, Fragment mFragment){
//getSupportFragmentManager().beginTransaction().add(android.R.id.content, new FragmentHome(), "myFragmentTag").addToBackStack("tag").commit(); // without FrameLayout
mManager.beginTransaction().setCustomAnimations(R.anim.fragment_fade_enter, 0).add(R.id.home_container, mFragment).addToBackStack(mFragment.getTag()).commitAllowingStateLoss(); //with back press
//getActivity().getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fragment_fade_enter, 0).replace(layout, mFragment).addToBackStack(mFragment.getTag()).commitAllowingStateLoss(); //with back press
}
public void onReplaceFragment(FragmentManager mManager, Fragment mFragment){
mManager.beginTransaction()
.setCustomAnimations(R.anim.fragment_fade_enter, 0)
.replace(R.id.home_container, mFragment)
.addToBackStack(null)
.commit();
}
public void onRemoveFragment(FragmentManager mFragmentManager, Fragment mFragment){
mFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.fragment_fade_enter, 0)
.remove(mFragment)
.addToBackStack(null)
.commit();
}
public void onBackFragment(FragmentManager mFragmentManager, Fragment mFragment){
if(mFragmentManager.getBackStackEntryCount() > 0) {
mFragmentManager.popBackStack(mFragment.getTag(), 0);
//getActivity().getSupportFragmentManager().popBackStack(new CampaignFragment().getTag(), 0);
}
}
public void onShowFragmentDialog(FragmentManager mFragmentManager, BottomSheetDialogFragment mFragment){
mFragment.setCancelable(false);
mFragment.show(mFragmentManager, mFragment.getTag());
}
}

ViewPager in Custom Dialog

I have custom dialog to show which has instruction about how to use the app.I am using ViewPager for this inside my custom dialog layout.
I am getting error
java.lang.IllegalArgumentException: No view found for id for fragment FragmentForInstruction1.
I have created a method which is called in onCreate(Bundle savedInstanceState) {} method .The method inflates the layout for dialog.
private void showCustomDialogForInstruction() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.dialog_layout_for_instruction_message, null, false);
layout.setAlpha(0.2f);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(dialogLayout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
ViewPager viewPager=(ViewPager) dialog.findViewById(R.id.pagerInstruction);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragmentForInstruction1());
adapter.addFrag(new Fragme`enter code here`ntForWelcomePage2());
adapter.addFrag(new FragmentForWelcomePage3());
adapter.addFrag(new FragmentForWelcomePage4());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new OnPageChangeListener()
{
#Override
public void onPageSelected(int pos)
{
}
#Override
public void onPageScrolled(int pos, float arg1, int arg2)
{
}
#Override
public void onPageScrollStateChanged(int arg0)
{
}
});
Button done = (Button) dialog.findViewById(R.id.done);
done.setText("Got It");
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button neverShow = (Button) dialog.findViewById(R.id.nevershow);
neverShow.setText("Never Show Again");
neverShow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
}
my view pager class:
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<Fragment>();
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)
{
mFragmentList.add(fragment);
}
}
and i call the method in MainActivity :
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferencesNeverShowAgain ;
boolean neverShowAgain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferencesNeverShowAgain = PreferenceManager.getDefaultSharedPreferences(this);
neverShowAgain = sharedPreferencesNeverShowAgain.getBoolean("NeverShowAgain", false);
if(!neverShowAgain){
showCustomDialogForMessage();
}
}
}
and the layout for dialog is:
<?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="wrap_content"
android:background="#drawable/dialog_shape"
android:orientation="vertical" >
<!-- layout title -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#00000000"
android:gravity="center"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/top_edge_rounded"
android:flipInterval="2000"
android:padding="20dp" >
<TextView
android:id="#+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/textColor"
android:textStyle="normal" />
<TextView
android:id="#+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/accent"
android:textStyle="normal" />
</ViewFlipper>
<View
android:id="#+id/tri"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/triangle"
android:rotation="180" />
</LinearLayout>
<!-- layout dialog content -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="300dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="#+id/pagerInstruction"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/layoutIndicater"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</ScrollView>
<!-- layout dialog buttons -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#drawable/all_rounded_edge_plum_for_dialog" >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:background="#color/textColor" />
<View
android:id="#+id/ViewColorPickerHelper"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/textColor" />
<Button
android:id="#+id/nevershow"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Close"
android:textColor="#color/textColor" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Done"
android:textColor="#color/textColor" />
</RelativeLayout>
</LinearLayout>
FragmentForInstruction1 code:
public class FragmentForInstruction1 extends Fragment{
#Override
#Nullable
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragmentinstructionpage1, container,false);
return view;
}
}
and its layout is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/backgroundColor"
android:gravity="center" >
<TextView
android:id="#+id/titleWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/messageWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="normal"
android:paddingLeft="20dp"
android:paddingRight="20dp"/>
</LinearLayout>
You should create an adapter that extends FragmentPagerAdapter:
private class CustomPagerAdapter extends FragmentPagerAdapter {
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentForInstruction1();
case 1:
return new FragmentForInstruction2();
case 2:
return new FragmentForInstruction3();
case 3:
return new FragmentForInstruction4();
default:
return new FragmentForInstruction1();
}
}
#Override
public int getCount() {
return 4;
}
}
While crating an instance of it, pass the getChildFragmentManager() instead of getSupportFragmentManager():
viewPager.setAdapter(new CustomPagerAdapter(getChildFragmentManager()));

PagerSlidingTabStrip indicator light does not change with the page?

It is working for my search activity page but not for my profile activity: The indicator light under the selected tab, do I have to add something
HEre is the code:
public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
String[] tabTitles = {"Completed", "Joined"};
adapter = new TabAdapter(getSupportFragmentManager(), profileCompletedFrag, profileJoinedFrag, tabTitles);
// // Initialize the ViewPager and set an adapter
pager = (ViewPager) findViewById(R.id.profilePager);
pager.setAdapter(adapter);
// Bind the tabs to the ViewPager
tabs = (PagerSlidingTabStrip) findViewById(R.id.profileTabs);
tabs.setViewPager(pager);
/**
* on swiping the viewpager make respective tab selected
* */
tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
if (position == 0) {
currentPage = 0;
pager.setCurrentItem(0);
}
if (position == 1) {
currentPage = 1;
pager.setCurrentItem(1);
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
/////////////
tabs.setOnTabReselectedListener(new PagerSlidingTabStrip.OnTabReselectedListener() {
#Override
public void onTabReselected(int position) {
Toast.makeText(ProfileActivity.this, "Tab reselected: " + position, Toast.LENGTH_SHORT).show();
if (position == 0) {
currentPage = 0;
pager.setCurrentItem(0);
}
if (position == 1) {
currentPage = 1;
pager.setCurrentItem(1);
}
}
});
He is the 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"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/profileParentLayout">
<LinearLayout
android:orientation="horizontal"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/profileNameLayout"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/profileName"
android:layout_centerInParent="true"
android:text="Bob Marley"/>
</LinearLayout>
<RelativeLayout
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginBottom="10dp"
android:layout_centerHorizontal="true"
android:id="#+id/profileImageLayout"
android:layout_below="#+id/profileNameLayout">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="#+id/profileImage"
android:src="#drawable/camera_w"
android:layout_centerInParent="true"/>
<ImageView android:id="#+id/editProfileImage"
android:layout_height="30dp"
android:layout_width="30dp"
android:src="#drawable/ic_launcher"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginBottom="10dp"
android:id="#+id/profileDetailsLayout"
android:layout_below="#id/profileImageLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/profileFollowers"
android:text="13 followers"
android:textSize="20dp"/>
<View android:id="#+id/separator"
android:layout_marginLeft="5dip"
android:background="#ffffff"
android:layout_width = "1dip"
android:layout_height="fill_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/profileFollowing"
android:text="45 following"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="20dp"
/>
<View android:id="#+id/separator2"
android:layout_marginRight="5dip"
android:background="#ffffff"
android:layout_width = "1dip"
android:layout_height="fill_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/profilePoints"
android:text="124 points"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:orientation="horizontal"
android:id="#+id/tabsLayout"
android:layout_below="#+id/profileDetailsLayout">
<!-- for Tabs -->
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/profileTabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:pstsShouldExpand="true"
android:background="?attr/colorPrimary"/>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profilePager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
<GridView
android:layout_below="#+id/tabsLayout"
android:id="#+id/profileGridView"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
public class TabAdapter extends FragmentStatePagerAdapter {
String[] tabTitles = {"Completed", "Joined"};
public TabAdapter(FragmentManager fragmentManager) {
// TODO Auto-generated constructor stub
super(fragmentManager);
}
#Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return tabTitles[position];
}
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return ProfileDataFagment.newInstance(0);
case 1:
return ProfileDataFagment.newInstance(1);
default:
break;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2; // Number of views
}
}
// Also create ProfileDataFagment and set you data according to your position

Categories

Resources