Open new activity by clicking the button in fragment android - android

I use swipe action tab I have 3 tabs (tab 1,tab 2,tab 3)
How can I open new activity by clicking the button in fragment android
in tab 1 page there is multiple buttons I want to click for example button 1 to open new page how can I do that?
how can I open new page from fragment ?
<?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:orientation="vertical"
android:background="#2e2e2e" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:background="#00b3ff"
android:text="button 1"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:textSize="22sp"
android:textStyle="bold" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="15dp"
android:background="#00b3ff"
android:text="button 2"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:textSize="22sp"
android:textStyle="bold"/>
</RelativeLayout>

You can do this:
yourButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent intent = new Intent(getActivity(),NextActivity.class);
getActivity().startActivity(intent);
}
});

You just have to do your Intent.
this.getActivity.startActivity(this.getApplicationContext(), ClassName.class);

public class TipsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_quiz_game2, container, false);
return rootView;
}
}

#SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "About", "Tips", "QuizGame" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}

Related

Android - PageViewer View stuck on screen

i'm trying to get this pageviewer with fragments to work however i'm stumbled on a problem where the view stays on screen on swipe. Ideally, i'd want it to only be on a particular layout yet i'm not too sure why it "carries over" to other screens (it also stays on screen during the transition between fragments).
Any feedback will be appreciated!
Link to Imgur Screenshots
Main:
private void initUI()
{
// ---------Page Viewer with Fragments -----------
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
Adapter:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Main Frag
return new Fragment1();
case 1:
// Second Frag
return new Fragment2();
case 2:
// Third Frag
return new Fragment3();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
Fragment classes are basically this:
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
return rootView;
}
}
Main Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
The EditText is in the main layout, the same layout that the ViewPager is in. It should be inflated within the tab. You'll need to create a separate layout for each tab.
Main layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
layout_fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Then you need to inflate the new layout in fragment 1.
View rootView = inflater.inflate(R.layout.layout_fragment_1, container, false);
return rootView;

Android Swipe View with tabs, fragments and ListView

I am facing problem with my android app and I cannot find working solution for this particular problem - I am starting to believe that it is not possible to achive what I try to do.
I want to have an app with tabs (DONE!). I want to swipe through those tabs with finger gesture (partialy done) and I want to display different items on those tabs as a ListView (partialy done). The problem is that when I populate ListView with my db data I cannot swipe through tabs any longer. It is like that because I change the content view to one of my fragments.
How can I keep swiping through tabs and populate list view? The other thing is that I want to call different select statement on each of my tabs dynamically - so user can mark something as his favourite and when he'll navigate to "favourites" tab he will find proper content.
Here is my code (I think this are the most important bits):
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
TabsPagerAdapter mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
setContentView(R.layout.fragment_main);
myList = (ListView) findViewById(R.id.listViewFromDB);
registerForContextMenu(myList);
openDB();
myDb.populateDataBaseOnStart();
populateListViewFromDB();
registerClickCallback();
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
Here is my ClassFragment.java:
public class FilmyFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_filmy, container, false);
return rootView;
}
}
And Here is layout xml for main activity and fragment class:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
Fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:id="#+id/listViewFromDB"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"/>

How to invoke second fragment tab from first fragment tab in view pager

This is MyTabActivity.java class
public class MyTabActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_layout);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
Toast.makeText(getBaseContext(), "Tab ReSelected: " + tab.getPosition(), Toast.LENGTH_SHORT).show();
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
Toast.makeText(getBaseContext(), "Tab Selected: " + tab.getPosition(), Toast.LENGTH_SHORT).show();
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
Toast.makeText(getBaseContext(), "Tab UnSelected: " + tab.getPosition(), Toast.LENGTH_SHORT).show();
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Tab1").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Tab2").setTabListener(tabListener));
}
}
This is TabPagerAdapter.java
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Tab1Activity();
case 1:
return new Tab2Activity();
}
return null;
}
#Override
public int getCount() {
//No of Tabs
return 2;
}
}
public class Tab1Activity extends Fragment {
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
/// code to display LIstView
.
.
//here i m trying to invoke other tab on click of list item...this where i stuck
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adptr, View view, int pos,
long id) {
/// this code here is not working for me
Tab2Activity myDetailFragment = new Tab2Activity ();
Bundle bundle = new Bundle();
bundle.putString("KEY_DETAIL", "Hi");
myDetailFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.attach(myDetailFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}});
return monthlyView;
}
}
This is Tab2Activity.java
public class Tab2Activity extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View dailyView = inflater.inflate(R.layout.activity_mian, container, false);
// displaying list item based on the input from tab1
return dailyView;
}
}
This is activity_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"
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=".MainActivity" >
<ListView
android:id="#+id/list_View1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
This is pager.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view starts -->
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Now my question is, i am displaying a list of item using listview in tab1
and i want to invoke tab2 when user click on the list item of tab1
and display some data on tab2 by sending some data from tab1 to tab2.
I an mew to android, please guide me with proper solutions. Thanks :)
Use viewPager.setCurrentItem(position, true); to switch between tabs.
Then, use this to get the current fragment.
Then you call a public method of that fragment that you create to do whatever you want.

Android swipe view with view pager

Based on android Creating Swipe Views with Tabs
http://developer.android.com/training/implementing-navigation/lateral.html
How can I Take listview with custom adapters and use in this example? I mean instead showing simple textview , display as.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
</RelativeLayout>
Result should be as Play store application.
Thanks.
You can try to check my other posts:
Android List view layout Similar to Google play
Unable to use AndroidDrawer (sidebar like facebook)
Maybe this is what you are looking for:
How to use swipe gesture in a view pager's page with android?
Try these links..maybe this is what you are looking for.
Here's an Update
1) Make an Adapter to handle your fragments:
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FirstFragment();
case 1:
return new SecondFragment();
case 2:
return new ThirdFragment();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
2) Add fragments: I am just adding the code for one fragment. You can follow that.
public class FirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.somefragment, container, false);//You will have your custom layout and add more items.
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
TextView tv = (TextView)getActivity().findViewById(R.id.textview1);
//You can add different items in here according to your needs.
//You will have your own code to do stuff. Like to use a listview or a gridview etc. You will make a separate adapter in a separate class and use that adapter in this onActivityCreated() method.
}
}
3) Modify the MainActivity:
public class AllActivities extends FragmentActivity implements ActionBar.TabListener {
public ViewPager viewPager;
private MyAdapter mAdapter;
private ActionBar actionBar;
private String [] tabs = {"FirstFragment","SecondFragment","ThirdFragment"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing all stuff
viewPager = (ViewPager)findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Add the tabs here
for(String tab_name:tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position){
//on Page change, that particular page should be selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0,float arg1,int arg2){
}
#Override
public void onPageScrollStateChanged(int position){
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
}
After you have implemented this example, you don't have to worry about this MainActivity. All you have to add more fragments is that add fragments to your adapter and build pages according to that. Also, add private String [] tabs = {"FirstFragment","SecondFragment","ThirdFragment","You other fragments when you will define"};
To make google play like navigation drawer, you can refer to my first two links and make then work as you like.
Hope this one helps for what you were looking for.

Android - Trying to open a row from SQLite into multiples fragments

I want to set the text of some EditTexts after clicking on an item of a list.
The activity:
- CadPersonActivity
And three fragments, each one having fields:
- FragmentPersonId
- FragmentPersonAddress
- FragmentPersonPhones
However those EditTexts insist on not showing what I expect.
There's no error, but blank fields.
I hope someone can help me.
Thanks in advance.
THE ACTIVITY
CadPersonActivity recover the Id in Bundle extras:
public class CadPersonActivity extends FragmentActivity implements TabListener {
private ActionBar actionBar;
private ViewPager viewPager;
private PersonPageAdapter pageAdapter;
private Person person;
private PersonDAO bd;
private Bundle extras;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cad_person);
extras = getIntent().getExtras();
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
pageAdapter = new PersonPageAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(pageAdapter);
viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
});
actionBar.addTab(actionBar.newTab().setText(getString(R.string.tab_id)).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(getString(R.string.tab_address)).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(getString(R.string.tab_phones)).setTabListener(this));
if (extras.getInt("id") > 0) {
bd = new PersonDAO(getApplicationContext());
person = bd.getPerson(extras.getInt("id"));
callSetFields();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if (bd != null)
bd.close();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
private void callSetFields() {
try {
FragmentPersonId fragId = (FragmentPersonId) getSupportFragmentManager().findFragmentById(R.id.fragment_id);
fragId.setFields(person);
FragmentPersonAddress fragEndereco = (FragmentPersonAddress) getSupportFragmentManager().findFragmentById(R.id.fragment_endereco);
fragEndereco.setFields(person);
FragmentPersonPhones fragPhones = (FragmentPersonPhones) getSupportFragmentManager().findFragmentById(R.id.fragment_phones);
fragPhones.setFields(person);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "An error occurred while trying to open the record.", Toast.LENGTH_SHORT).show();
}
}
}
CallSetFields is called when a record is clicked and its Id is passed to CadPersonActivity.
The activity_cad_person.xml:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/fragment_id"
android:name="com.fragment.FragmentPersonId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="#layout/fragment_id" />
<fragment
android:id="#+id/fragment_address"
android:name="com.fragment.FragmentPersonAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="#layout/fragment_address" />
<fragment
android:id="#+id/fragment_phones"
android:name="com.fragment.FragmentPersonPhones"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="#layout/fragment_phones" />
</android.support.v4.view.ViewPager>
THE FRAGMENTS:
And each fragment have a code like this:
public class FragmentPersonId extends Fragment {
private EditText edtName;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_id, container,
false);
return view;
}
public void setFields(Pessoa pessoa) {
try {
edtName = (EditText) getView().findViewById(R.id.edtName);
edtName.setText(person.getName());
} catch (Exception e) {
Toast.makeText(getActivity().getApplicationContext(), "Error while setting fields.", Toast.LENGTH_SHORT).show();
}
}
}
Well, after some days searching and trying, I have this
Declare the class Person in CadPersonActivity as public static;
public static Person person;
Now, instantiate these EditTexts and call person.getName directly in onCreateView;
edtName = (EditText) view.findViewById(R.id.edtName);
edtName.setText(CadPersonActivity.person.getName());
I can't see this as a good solution, but it's the best I could find.

Categories

Resources