I'm currently trying to make a small application in java, and I have encountered a small problem.
I need to activate a togglebutton when I touch a particular area of the image that I have provided.
I enclose the code of my pages.
main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btnPag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lights"
tools:ignore="HardcodedText" >
</Button>
<Button
android:id="#+id/btnPag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Automation"
tools:ignore="HardcodedText" >
</Button>
<Button
android:id="#+id/btnPag3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Garden"
tools:ignore="HardcodedText" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
</LinearLayout>
page1.xml
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 1"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 2"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 3"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 4"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/casa"
tools:ignore="ContentDescription" />
</LinearLayout>
page2.xml
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gate"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activates"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
page3.xml
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Irrigation"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MercuryActivity.java
package it.anddev.bradipao.mercury;
// derived from http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/
import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MercuryActivity extends FragmentActivity implements Page1Fragment.OnPageListener {
// list contains fragments to instantiate in the viewpager
List<Fragment> fragments = new Vector<Fragment>();
// page adapter between fragment list and view pager
private PagerAdapter mPagerAdapter;
// view pager
private ViewPager mPager;
// activity data
public String p2text,p3text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// creating fragments and adding to list
fragments.add(Fragment.instantiate(this,Page1Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Page2Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Page3Fragment.class.getName()));
// creating adapter and linking to view pager
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(),fragments);
mPager = (ViewPager) super.findViewById(R.id.pager);
mPager.setAdapter(this.mPagerAdapter);
// upper bar button listener, allows direct page access
Button button = (Button)findViewById(R.id.btnPag1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0); // go to first page
}
});
button = (Button)findViewById(R.id.btnPag2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(1); // go to second page
}
});
button = (Button)findViewById(R.id.btnPag3);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(2); // go to third page
}
});
}
// page 1 fragment listener implementation
#Override
public void onPage1(String s) {
// set activity data with received string
p2text = new String(s+" 2");
p3text = new String(s+" 3");
// page 2 fragment update
Page2Fragment f2 = (Page2Fragment) fragments.get(1);
f2.ptext = p2text;
// if page 2 view is already created, update
View v2 = f2.getView();
if (v2!=null) {
}
// page 3 fragment update
Page3Fragment f3 = (Page3Fragment) fragments.get(2);
f3.ptext = p3text;
// if page 3 view is already created, update
View v3 = f3.getView();
if (v3!=null) {
}
}
}
Page1Fragment.java
package it.anddev.bradipao.mercury;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class Page1Fragment extends Fragment {
public String ptext="..PAGE 1..";
// activity listener interface
private OnPageListener pageListener;
public interface OnPageListener {
public void onPage1(String s);
}
// onAttach : set activity listener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// if implemented by activity, set listener
if(activity instanceof OnPageListener) {
pageListener = (OnPageListener) activity;
}
// else create local listener (code never executed in this example)
else pageListener = new OnPageListener() {
#Override
public void onPage1(String s) {
Log.d("PAG1","Button event from page 1 : "+s);
}
};
}
// onCreateView :
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page1,container,false);
// update text
return view;
}
// set text helper function
}
Page2Fragment.java
package it.anddev.bradipao.mercury;
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.LinearLayout;
public class Page2Fragment extends Fragment {
public String ptext="..PAGE 2..";
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page2,container,false);
// update text
return view;
}
// set text helper function
}
Page3Fragment.java
package it.anddev.bradipao.mercury;
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.LinearLayout;
public class Page3Fragment extends Fragment {
Button btnWrite;
public String ptext="..PAGE 3..";
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page3,container,false);
// update text
return view;
}
}
PageAdapter.java
package it.anddev.bradipao.mercury;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter {
// fragments to instantiate in the viewpager
private List<Fragment> fragments;
// constructor
public PagerAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
// return access to fragment from position, required override
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
// number of fragments in list, required override
#Override
public int getCount() {
return this.fragments.size();
}
}
Related
I've created an Activity that has three tabs. On clicking Tab 1 it opens up a fragment (TaskFragment) containing two buttons, each with an associated text.
When I click on the "Create Appraisal Report" button it properly inflates another Fragment's (AppraisalReportFragment) two text lines (that now supercede the previous two), and a page title (Appraisal Reports), but the two initial buttons remain in place.
I'm trying to get everything on the initial Fragment to be changed over to an entirely new Fragment, with it's own buttons and text), but right now only some of these changes take place.
TaskFragment
package usjersey.com.jerseyscore.fragments;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
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.RelativeLayout;
import usjersey.com.jerseyscore.R;
/**
* Created by dhiggins on 11/20/2017.
*/
public class TasksFragment extends Fragment {
private static final String TAG = "TasksFragment";
Intent intent;
private RelativeLayout layoutToAdd;
// public void onCreate (Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
//}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tasks, container, false);
//View rootView = inflater.inflate(R.layout.fragment_tasks, null);
//layoutToAdd = (RelativeLayout) findViewById(R.id.TaskFragment);
Button ID = (Button) rootView.findViewById(R.id.btn_create_appraisal_rpt);
ID.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AppraisalReportFragment appraisalRptAct = new AppraisalReportFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//fragmentTransaction.setTransition(fragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(null);
//Fragment newFragment = new AppCompatDialogFragment();
//FragmentTransaction transaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.tasks_page_fragment, appraisalRptAct);
//transaction.replace(R.id.child_fragment, newFragment);
fragmentTransaction.commit();
}
});
Button ID2 = (Button) rootView.findViewById(R.id.btn_check_in_for_today);
ID2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TasksFragment checkInForToday = new TasksFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.herds_check_in_for_today_fragment4, checkInForToday);
//fragmentTransaction.setTransition(fragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//fragmentTransaction.addToBackStack(null);
//Fragment newFragment = new AppCompatDialogFragment();
//FragmentTransaction transaction = getFragmentManager().beginTransaction();
//transaction.replace(R.id.child_fragment, newFragment);
fragmentTransaction.commit();
}
});
/*rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(getActivity(), AppraisalReportActivity.class);
final Button button = (Button) rootView.findViewById(R.id.button_create_appraisal_rpt);
}
});*/
return rootView;
}
/*public void buttonOnClick(View v){
}*/
}
fragment_tasks.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/tasks_page_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:orientation="vertical">
<!-- First A -->
<Button
android:id="#+id/btn_check_in_for_today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="27dp"
android:layout_marginTop="44dp"
android:background="#android:color/black"
android:elevation="0dp"
android:onClick="onCreateView"
android:text="#string/btn_check_in_for_today"
android:textAlignment="viewStart"
android:textColor="#android:color/white"
android:textSize="18sp" />
<!-- First B -->
<Button
android:id="#+id/btn_create_appraisal_rpt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/btn_check_in_for_today"
android:layout_below="#+id/btn_check_in_for_today"
android:layout_marginTop="40dp"
android:background="#android:color/black"
android:onClick="onCreateView"
android:text="#string/btn_create_appraisal_report"
android:textColor="#android:color/white"
android:textSize="18sp" />
<!-- First B-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/textView3"
android:layout_below="#+id/btn_check_in_for_today"
android:text="#string/tasks_check_day_score"
android:textColor="#android:color/white"
android:id="#+id/textView2" />
<!-- First B-->
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_create_appraisal_rpt"
android:layout_centerHorizontal="true"
android:text="#string/tasks_print_email_regmail"
android:textColor="#android:color/white" />
</RelativeLayout>
**AppraisalReportFragment**
package usjersey.com.jerseyscore.fragments;
import android.app.Activity;
//import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import usjersey.com.jerseyscore.R;
/**
* Created by dhiggins on 11/29/2017.
*/
public class AppraisalReportFragment extends Fragment {
private static final String TAG = "Appraisal Report";
TextView textView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//return inflater.inflate(R.layout.fragment_information, container, false);
View view = inflater.inflate(R.layout.fragment_appraisal_report, container, false);
TextView output = (TextView)view.findViewById(R.id.textView3);
//output.setText("Frag 2");
return view;
}
}
fragment_appraisal.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/tasks_page_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:orientation="vertical">
<Button
android:id="#+id/btn_check_in_for_today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/appraisal_report"
android:layout_marginStart="27dp"
android:layout_marginTop="19dp"
android:background="#android:color/black"
android:text="#string/btn_all_cows_in_herd"
android:textColor="#android:color/white"
android:textSize="18sp" />
<Button
android:id="#+id/btn_cows_scored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/btn_check_in_for_today"
android:layout_below="#+id/textView5"
android:layout_marginTop="23dp"
android:background="#android:color/black"
android:onClick="onCreateView"
android:text="#string/btn_cows_scored"
android:textColor="#android:color/white"
android:textSize="18sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/textView3"
android:layout_below="#+id/btn_check_in_for_today"
android:text="#string/label_all_cows_in_herd_report"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_cows_scored"
android:layout_centerHorizontal="true"
android:text="#string/label_checked_scored_cows"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/appraisal_report"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:fontFamily="serif"
android:text="#string/title_appraisal_reports"
android:textAlignment="viewStart"
android:textColor="#android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
When you inflate your fragment you should setVisibility to gone to your buttons from the previous layout after committing the FragmentManager's transaction in your activity
You can do it like this and then change them to visible again when returning
b1.setVisibility(View.GONE);
b2.setVisibility(View.GONE);
Change the names of the buttons according to your code
I'm new to Android..
I am trying to create a simple application with a sliding tab to scroll between 2 fragments. The fragments are populated with dummy buttons just to see if the scrolling works. However when I run all I see are the viewPager indexes on top without the fragments. I am also using these two files SlidingTabLayout.java and SlidingTabStrip.java that are given by Android. Here are my classes:
MyFragmentPagerAdapter.java
package billdozer.com.testxml;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
// Holds tab titles
private String tabTitles[] = new String[] { "Frag #1", "Frag #2"};
private Context context;
public MyFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return 2;
}
// Return the correct Fragment based on index
#Override
public Fragment getItem(int position) {
if(position == 0){
return new TabFragment1();
} else if(position == 1) {
return new TabFragment2();
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
// Return the tab title to SlidingTabLayout
return tabTitles[position];
}
}
TabFragment1.java
package billdozer.com.testxml;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.tab_fragment_1, container, false);
return view;
}
}
TabFragment2.java
package billdozer.com.testxml;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.tab_fragment_2, container, false);
return view;
}
}
tab_fragment_1.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">
<TableLayout
android:id="#+id/tl"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center" >
<Button
android:id="#+id/button1"
android:layout_weight="1"
android:text="#string/_1" />
<Button
android:id="#+id/button2"
android:layout_weight="1"
android:text="#string/_2" />
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:text="#string/_3" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/button5"
android:layout_weight="1"
android:text="#string/_4" />
<Button
android:id="#+id/button6"
android:layout_weight="1"
android:text="#string/_5" />
<Button
android:id="#+id/button7"
android:layout_weight="1"
android:text="#string/_6" />
</TableRow>
</TableLayout>
</LinearLayout>
tab_fragment_2.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">
<TableLayout
android:id="#+id/tl"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center" >
<Button
android:id="#+id/button1"
android:layout_weight="1"
android:text="#string/_7" />
<Button
android:id="#+id/button2"
android:layout_weight="1"
android:text="#string/_8" />
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:text="#string/_9" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/button5"
android:layout_weight="1"
android:text="#string/_10" />
<Button
android:id="#+id/button6"
android:layout_weight="1"
android:text="#string/_11" />
<Button
android:id="#+id/button7"
android:layout_weight="1"
android:text="#string/_12" />
</TableRow>
</TableLayout>
</LinearLayout>
MainActivity.java
package com.example.user.billdozer_ui;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import com.example.user.billdozer_ui.stab.SlidingTabLayout;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
slidingTabLayout.setViewPager(viewPager);
}
}
activity_main.xml
<?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_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.user.billdozer_ui.MainActivity">
<com.example.user.billdozer_ui.stab.SlidingTabLayout
android:id="#+id/sliding_tabs"
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="0px" />
</LinearLayout>
I'm trying to add Parse API data with a tab Fragment. The ListView
is working fine and I need to add 2 filter Buttons between the
Fragment tab Buttons and the ListView. Below I attached what I'm
trying to get
MainActivity.java
import android.support.design.widget.TabLayout;
import android.support.v4.app.ListFragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
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.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.Parse;
public class MainActivity extends AppCompatActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId("")
.server("")
.build()
);
}
#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);
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private EventFragment eventFragment = new EventFragment();
private IndividualsFragment individualsFragment = new IndividualsFragment();
private OrganizationFragment orgFragment = new OrganizationFragment();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
OrganizationFragment tab1= orgFragment;
return tab1;
case 1:
EventFragment tab2= eventFragment;
return tab2;
case 2:
IndividualsFragment tab3= individualsFragment;
return tab3;
}
return null ;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Directory";
case 1:
return "EVENTS";
case 2:
return "INDIVIDUALS";
}
return null;
}
}
}
This is a Fragment tab.
I want to 2 Buttons between the Fragment tab Buttons and the ListView items
EventFragment.java
import android.app.ListActivity;
import android.app.ListFragment;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.util.ArrayList;
import java.util.List;
public class EventFragment
extends android.support.v4.app.ListFragment
implements FindCallback<ParseObject> {
private List<ParseObject> mOrganization = new ArrayList<ParseObject>();
#Override
public void onViewCreated(View view, Bundle b) {
super.onViewCreated(view, b);
EventAdapter adaptor = new EventAdapter(getActivity(), mOrganization);
setListAdapter(adaptor);
// This is like calling fetchList()
ParseQuery.getQuery("Event").findInBackground(this);
}
/**
* This is needed by implementing the callback on the class
**/
#Override
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
Log.d("score", "Retrieved " + scoreList.size() + " Event");
mOrganization.clear();
mOrganization.addAll(scoreList);
((EventAdapter) getListAdapter()).notifyDataSetChanged();
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
}
this is the xml file of the ListView
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.swipe.com.MainActivity">
<com.example.rihan.swip.RoundRectCornerImageView
android:id="#+id/imageView"
android:layout_gravity="center"
android:layout_height="110dp"
android:layout_width="110dp"
tools:minWidth="30dp"
tools:maxWidth="50dp"
tools:minHeight="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:layout_marginTop="19dp"
android:scaleType="fitXY"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/organizationname"
tools:textSize="10sp"
android:textSize="10sp"
android:layout_below="#+id/idposition"
android:layout_alignLeft="#+id/idposition"
android:layout_alignStart="#+id/idposition"
android:paddingTop="10px" />
<TextView
android:id="#+id/user"
android:text="Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="12sp"
android:textColor="#000"
android:layout_alignBaseline="#+id/name"
android:layout_alignBottom="#+id/name"
android:layout_toRightOf="#+id/name"
android:layout_toEndOf="#+id/name"
android:paddingLeft="10dp"
android:fontFamily="sans-serif" />
<TextView
android:text="yy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/idposition"
android:textSize="10sp"
android:layout_below="#+id/name"
android:layout_alignLeft="#+id/name"
android:layout_alignStart="#+id/name" />
<TextView
android:id="#+id/name"
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:minWidth="5dp"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
tools:textStyle="bold"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
android:layout_alignTop="#+id/imageView"
android:layout_toRightOf="#+id/imageView"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="13dp"
android:fontFamily="sans-serif" />
</RelativeLayout>
this is 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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.swipe.com.MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_height="40dp"
android:background="#drawable/nav"
android:id="#+id/button2"
android:layout_weight="1"
android:layout_width="40dp"
android:layout_alignBottom="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:paddingLeft="25dp"
tools:layout_marginLeft="20dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/i"
android:id="#+id/button"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageView
app:srcCompat="#drawable/logo"
android:id="#+id/imageView2"
android:layout_weight="1"
tools:layout_marginLeft="15dp"
android:layout_width="280dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button"
android:layout_toStartOf="#+id/button"
android:layout_height="50dp" />
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
If we quote Android | ListFragment
ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle).
And also from the documentation
your view hierarchy must contain a ListView object with the id "#android:id/list"
Let's call this **some_list_view.xml**
Notice not #+id/list, but #android:id/list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<!-- TODO: Add your buttons here -->
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data"/>
</LinearLayout>
One of your error exists in onViewCreated. You have no inflater there.
Implement the other method like the documentation says.
And you don't need to find the ListView.
Use findViewById to find other views.
For example,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.some_list_view, parent, false);
}
#Override
public void onViewCreated(View view, Bundle b) {
super.onViewCreated(view, b);
// Button filter1 = (Button) view.findViewById(...);
// filter1.setOnClickListener....
EventAdapter adaptor = new EventAdapter(getActivity(), mOrganization);
setListAdapter(adaptor);
// This is like calling fetchList()
ParseQuery.getQuery("Event").findInBackground(this);
}
#Override
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
...
}
}
getListView() already will return you your ListView, therefore, notice I didn't need to find it. But if you did, this is how.
(ListView) view.findViewById(android.R.id.list);
So, that's just a matter of making an XML file with that mentioned ID value set to a ListView like shown above.
I've found multiple similar posts but have been unable to resolve so posting my code for help.
I have 3 fragments that use a ViewPager with a PagerAdapter to allow for swiping between fragments. This project started with no fragments and I was able to play sounds from buttons clicked all from MainActivity.
Here's the code that worked.
MainActivity
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
MediaPlayer example1Sound;
MediaPlayer example2Sound;
MediaPlayer example3Sound;
MediaPlayer example4Sound;
MediaPlayer example5Sound;
MediaPlayer example6Sound;
MediaPlayer example7Sound;
MediaPlayer example8Sound;
MediaPlayer example9Sound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
example1Sound = MediaPlayer.create(this, R.raw.example1);
example2Sound = MediaPlayer.create(this, R.raw.example2);
example3Sound = MediaPlayer.create(this, R.raw.example3);
example4Sound = MediaPlayer.create(this, R.raw.example4);
example5Sound = MediaPlayer.create(this, R.raw.example5);
example6Sound = MediaPlayer.create(this, R.raw.example6);
example7Sound = MediaPlayer.create(this, R.raw.example7);
example8Sound = MediaPlayer.create(this, R.raw.example8);
example9Sound = MediaPlayer.create(this, R.raw.example9);
}
public void playExampleSound1(View view) {
example1Sound.start();
}
public void playExampleSound2(View view) {
example2Sound.start();
}
public void playExampleSound3(View view) {
example3Sound.start();
}
public void playExampleSound4(View view) {
example4Sound.start();
}
public void playExampleSound5(View view) {
example5Sound.start();
}
public void playExampleSound6(View view) {
example6Sound.start();
}
public void playExampleSound7(View view) {
example7Sound.start();
}
public void playExampleSound8(View view) {
example8Sound.start();
}
public void playExampleSound9(View view) {
example9Sound.start();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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"
tools:context="com.anjosoft.MainActivity">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_column="0"
android:background="#drawable/button1"
android:onClick="playExampleSound1" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton2"
android:layout_column="1"
android:background="#drawable/button2"
android:onClick="playExampleSound2" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton3"
android:layout_column="2"
android:background="#drawable/button3"
android:onClick="playExampleSound3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton4"
android:layout_column="0"
android:background="#drawable/button4"
android:onClick="playExampleSound4" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton5"
android:layout_column="1"
android:background="#drawable/button5"
android:onClick="playExampleSound5" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton6"
android:layout_column="2"
android:background="#drawable/button6"
android:onClick="playExampleSound6" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton7"
android:layout_column="0"
android:background="#drawable/button7"
android:onClick="playExampleSound7" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton8"
android:layout_column="1"
android:background="#drawable/button8"
android:onClick="playExampleSound8" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton9"
android:layout_column="2"
android:background="#drawable/button9"
android:onClick="playExampleSound9" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Sound"
android:id="#+id/checkBox"
android:layout_column="1" />
</TableRow>
</TableLayout>
I know it's not pretty. Should have made an array but nevertheless it worked.
So here's my problem. Using the same ideology I tied the same thing with fragment.
MainActivity.java
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
ViewPager viewpager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager)findViewById(R.id.pager);
PagerAdapter padapter = new PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(padapter);
}
}
PagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter{
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
default:
break;
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
FragmentOne.java
import android.app.Activity;
import android.media.MediaPlayer;
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;
public class FragmentOne extends Fragment {
MediaPlayer example1Sound;
MediaPlayer example2Sound;
MediaPlayer example3Sound;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
example1Sound = MediaPlayer.create(this.getActivity(), R.raw.example1);
example2Sound = MediaPlayer.create(this.getActivity(), R.raw.example2);
example3Sound = MediaPlayer.create(this.getActivity(), R.raw.example3);
return inflater.inflate(R.layout.fragment_one_layout,container,false);
}
public void playExampleSound1(View view){
example1Sound.start();
}
public void playExampleSound2(View view) {
example2Sound.start();
}
public void playExampleSound3(View view) {
example3Sound.start();
}
}
fragnment_one_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#f70808">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_column="0"
android:background="#drawable/button1"
android:onClick="playExampleSound1"
android:nestedScrollingEnabled="false" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton2"
android:layout_column="1"
android:background="#drawable/button2" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton3"
android:layout_column="2"
android:background="#drawable/button3" />
</TableRow>
</TableLayout>
</RelativeLayout>
Clicking on imagebutton crashes the app. I know the MediaPlayer can work inside the app because I tested inserting example1Sound.start(); like this...
Snippet from FragmentOne.java
example1Sound = MediaPlayer.create(this.getActivity(), R.raw.example1);
example2Sound = MediaPlayer.create(this.getActivity(), R.raw.example2);
example3Sound = MediaPlayer.create(this.getActivity(), R.raw.example3);
example1Sound.start();
The sound plays when the fragment loads. How do I get the onclick to work?
You can use onClickListeners for each button, it works.
I think, fragnment_one_layout.xml is linked to FragmentOne.java using inflater
but the fragnment_one_layout.xml has no access to FragmentOne.java , and hence
the statement onClick="methodName" is not able to access the desired method.
mediaPlayer object has no connection with your problem.
You can make one function that redirect sound from Activity to Fragment!
in Activity
fun onSound(sound:Int){
mp = MediaPlayer.create(this,soundFile)
mp.start
}
from fragment
onResume{
(activity as ActivityName).onSound(R.raw.sound)
}
I am creating a sign up form which has been divided into three fragments.The three fragments are connected by a viewpager.I can traverse throught the fragments with next and previous buttons.I want the data from the first and second fragment to be available in the third fragment.One option is to use sharedpreferences.Is there any other approach.
The main class:
package pl.looksok.viewpagerdemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends FragmentActivity {
ViewPager pager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager = (ViewPager)findViewById(R.id.myViewPager);
pager.setAdapter(pageAdapter);
}
public void selectFragment(int position){
pager.setCurrentItem(position, true);
// true is to animate the transaction
}
}
The main xml
<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=".MainActivity" >
<pl.looksok.viewpagerdemo.CustomViewPager
android:id="#+id/myViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
The first fragment:
package pl.looksok.viewpagerdemo;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class FragmentBlue extends Fragment {
Button btnnext1;
RelativeLayout frag1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blue, container, false);
btnnext1=(Button) view.findViewById(R.id.btnnext1);
frag1=(RelativeLayout) view.findViewById(R.id.frag1);
btnnext1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
((MainActivity)getActivity()).selectFragment(1);
}
});
return view;
}
}
The first xml:
<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"
android:background="#4ECDC4"
android:id="#+id/frag1"
>
<Button
android:id="#+id/btnnext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="NEXT" />
<EditText
android:id="#+id/txtfname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtmname"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="FIRST NAME" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtmname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtfname"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="MIDDLE NAME" />
<EditText
android:id="#+id/txtlname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtmname"
android:layout_below="#+id/txtmname"
android:layout_marginTop="23dp"
android:ems="10"
android:hint="LAST NAME" />
<EditText
android:id="#+id/txtdob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtlname"
android:layout_centerVertical="true"
android:ems="10"
android:hint="DOB" />
</RelativeLayout>
The second fragment:
package pl.looksok.viewpagerdemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class FragmentGreen extends Fragment {
Button btnnext2,btnprev2;
RelativeLayout frag2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_green, container, false);
btnnext2=(Button) view.findViewById(R.id.btnnext2);
btnprev2=(Button) view.findViewById(R.id.btnprev2);
frag2=(RelativeLayout) view.findViewById(R.id.frag2);
btnnext2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
((MainActivity)getActivity()).selectFragment(2);
}
});
btnprev2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity)getActivity()).selectFragment(0);
}
});
return view;
}
}
The second fragment's xml:
<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"
android:background="#C7F464"
android:id="#+id/frag2"
>
<Button
android:id="#+id/btnnext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="60dp"
android:text="NEXT" />
<Button
android:id="#+id/btnprev2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="68dp"
android:layout_toLeftOf="#+id/btnnext2"
android:text="PREV" />
<EditText
android:id="#+id/txtgender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/btnnext2"
android:ems="10"
android:hint="GENDER" />
<EditText
android:id="#+id/txtmaritalstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtgender"
android:layout_below="#+id/txtgender"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="MARITAL STATUS" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtoccupation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtmaritalstatus"
android:layout_below="#+id/txtmaritalstatus"
android:layout_marginTop="23dp"
android:ems="10"
android:hint="OCCUPATION" />
<EditText
android:id="#+id/txtusername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtoccupation"
android:layout_centerVertical="true"
android:ems="10"
android:hint="USERNAME" />
<EditText
android:id="#+id/txtpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtusername"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="PASSWORD"
android:inputType="textPassword" />
</RelativeLayout>
The third fragment:
package pl.looksok.viewpagerdemo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;
public class FragmentPink extends Fragment {
Button btnnext3,btnprev3;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pink, container, false);
btnnext3=(Button) view.findViewById(R.id.btnnext3);
btnprev3=(Button) view.findViewById(R.id.btnprev3);
btnnext3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
btnprev3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity)getActivity()).selectFragment(1);
}
});
return view;
}
}
The third fragment's xml:
<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"
android:background="#FF6B6B">
<Button
android:id="#+id/btnnext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="NEXT" />
<Button
android:id="#+id/btnprev3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="PREV" />
<EditText
android:id="#+id/txtaddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="ADDRESS" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtdistrict"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtaddress"
android:layout_below="#+id/txtaddress"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="DISTRICT" />
<EditText
android:id="#+id/txtstate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtdistrict"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="STATE" />
<EditText
android:id="#+id/txtpincode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtstate"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="PINCODE" />
</RelativeLayout>
The adapter:
package pl.looksok.viewpagerdemo;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
this.fragments = new ArrayList<Fragment>();
fragments.add(new FragmentBlue());
fragments.add(new FragmentGreen());
fragments.add(new FragmentPink());
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
}
I also created a class which has string values.I was wondering if i could create an object and set the fields as properties for that object
The class with the fields:
package pl.looksok.viewpagerdemo;
import java.io.Serializable;
public class RegistrationValues implements Serializable {
String fname;
String mname;
String lname;
String dob;
String gender;
String marital_status;
String occupation;
String username;
String password;
String address;
String district;
String state;
String pincode;
String nationality;
String email;
String phno;
String bank_acno;
String bank_branch;
String bank_ifsc;
String bank_swift;
}
So the question is how to pass the values in the first and second fragment to the last fragment without using Sharedpreferences.
The easy approach would be to let the RegistrationValues instance reside in your MainActivity instance:
public class MainActivity extends FragmentActivity {
ViewPager pager;
RegistrationValues values; //add this
public RegistrationValues getValues() { //add this
return values; //add this
}; //add this
In your fragments, use ((MainActivity) getActivity()).getValues() to update the values and to actually get the values in the third fragment or so.
Note: You should probably save the values instance in your MainActivity's onSaveInstance and restore it in your MainActivity's onCreate.
i think that for values passing between fragments you can use listener and dispatcher patterns and singleton pattern, which will manage listeners and dispatchers, because fragments are not Activities and you can't pass values via Intents.
A very common pattern is for the fragments to provide an interface by which values may be passed to them, and to use the containing activity as a hub. To see an example of this, use Eclipse or Android Studio to instantiate a new Master/Detail Flow activity and look at the code for the ListActivity.
But I would argue that in this case, you probably don't want to do that. Your data record looks like something you want to maintain in your persistence layer (Model), maybe mutate through a service (Controller) and pull up as needed into the fragment (View). Separate UI from business logic, and use an Adapter to keep the data in sync.