How can I start a new activity in a ScreenSlidePagerAdapter? - java

I Have a FragmentActivity where I have a ScreenSlidePagerAdapter that shows many fragments with the getItem() method, for swipe between fragments. I want that once the user finalized swipping the fragments (from 0 to 15 for example) there's going to open a new activity showing the results of what the user had done navigating the fragments (it's a course).
What I need is an intent for open a New Activity after the fragments.
This is my code:
package activities;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import com.flixarts.ar.*.R;
import com.flixarts.ar.*.fragment1;
import com.flixarts.ar.*.fragment10;
import com.flixarts.ar.*.fragment11;
import com.flixarts.ar.*.fragment12;
import com.flixarts.ar.*.fragment13;
import com.flixarts.ar.*.fragment14;
import com.flixarts.ar.*.fragment15;
import com.flixarts.ar.*.fragment2;
import com.flixarts.ar.*.fragment3;
import com.flixarts.ar.*.fragment4;
import com.flixarts.ar.*.fragment5;
import com.flixarts.ar.*.fragment6;
import com.flixarts.ar.*.fragment7;
import com.flixarts.ar.*.fragment8;
import com.flixarts.ar.*.fragment9;
import com.flixarts.ar.*.menuscreen;
public class saludos extends FragmentActivity implements fragment1.OnFragmentInteractionListener, fragment2.OnFragmentInteractionListener, fragment3.OnFragmentInteractionListener, fragment4.OnFragmentInteractionListener {
private TextView espacioFrases;
private TextView espacioFrasesTraducidas;
private Button next;
private Button buttonHome;
private ViewPager mPager;
private PagerAdapter pagerAdapter;
private static final int NUM_PAGES = 15;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saludos);
mPager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(pagerAdapter);
}
#Override
public void onBackPressed(){
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
}
else {
mPager.setCurrentItem(mPager.getCurrentItem() -1);
}
}
#Override
public void onFragmentInteraction(Uri uri) {
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm){
super(fm);
}
#Override
public Fragment getItem(int position){
switch (position){
case 0: return new fragment1();
case 1: return new fragment2();
case 2: return new fragment3();
case 3: return new fragment4();
case 4: return new fragment5();
case 5: return new fragment6();
case 6: return new fragment7();
case 7: return new fragment8();
case 8: return new fragment9();
case 9: return new fragment10();
case 10: return new fragment11();
case 11: return new fragment12();
case 12: return new fragment13();
case 13: return new fragment14();
case 14: return new fragment15();
//Here I want to start new activity
default: return new fragment1();
}
}
#Override
public int getCount(){
return NUM_PAGES;
}
}
I've trying create an intent with getActivity() but this method wasn't loaded in Android Studio (maybe this is old).

You should write the startactivity code inside last showing fragment of adapter. (If you are using instances of same fragment, then you have to pass the position from adapter to fragment while creating it.
Intent intent = new Intent(getContext(), ThirdActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Related

Required: android.support.v4.app.Fragment Found: com.marketing.auratek.chatapp.RequestsFragment

I am making a chat app and created a new package. I have pasted the code below.
The Error is:
Required:android.support.v4.app.Fragment
Found:com.marketing.auratek.chatapp.RequestsFragmentcom.marketing.auratek.chatap
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* Created by Jagan on 7/14/2017.
*/
class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
RequestsFragment requestsFragment = new RequestsFragment();
return requestsFragment;
case 1:
ChatsFragment chatsFragment = new ChatsFragment();
return chatsFragment;
case 2:
FriendsFragment friendsFragment = new FriendsFragment();
return friendsFragment;
}
return null;
}
#Override
public int getCount() {
return 3;
}
}

Incompatible types: DetailOneFragment cannot be converted to Fragment

I have problem in my code
error is
incompatible types: DetailOneFragment cannot be converted to Fragment
in Android
incompatible types: DetailTwoFragment cannot be converted to Fragment
in Android
Which is wrong?
this is DetailActivity.java
import android.content.Intnet;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.adnroidtown.albaplanet.R;
import org.adnroidtown.albaplanet.review_view.Review1Activity;
public class DetailActivity extends AppCompatActivity {
ViewPager pager;
TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
//메인 액티비티에서 넘겨받은 인텐트 -시작-
ImageView iv = (ImageView) findViewById(R.id.detail_img);
TextView tvName = (TextView) findViewById(R.id.detail_store);
Intent intent = getIntent();
iv.setImageResource(intent.getIntExtra("img", 0));
tvName.setText(intent.getStringExtra("name"));
pager = (ViewPager) findViewById(R.id.detail_viewpager);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
FragmentManager manager = getSupportFragmentManager();
PagerAdapter adapter = new PagerAdapter(manager);
pager.setAdapter(adapter);
tabLayout.setupWithViewPager(pager);
// mTabLayout.setupWithViewPager(mPager1);
pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setTabsFromPagerAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {#Override
public void onClick(View view) {
Intent intent = new Intent(DetailActivity.this, Review1Activity.class);
startActivity(intent);
}
});
}
}
this is PagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment frag = null;
switch (position) {
case 0:
frag = new DetailOneFragment();
break;
case 1:
frag = new DetailTwoFragment();
break;
}
return frag;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
String title = " ";
switch (position) {
case 0:
title = "Game";
break;
case 1:
title = "Movie";
break;
}
return title;
}
}
this is DetailOneFragment.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.adnroidtown.albaplanet.R;
public class DetailOneFragment extends Fragment {
public DetailOneFragment(){
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_detail_one, container, false);
}
}
This is error ScreenShot
See your screenshot. You Import/extend android.app.Fragment;in DetailOneFragment and use android.app.support...Fragment in your other class
replace
import android.app.Fragment;
with
import android.support.v4.app.Fragment;
in your fragments
As of 02-2021 the solution above did work for me but i had to replace :
import android.app.Fragment;
with
import androidx.fragment.app.Fragment;
instead

how to find last fragment in MainActivity [duplicate]

This question already has an answer here:
How can i find last fragment in the mainActivity
(1 answer)
Closed 6 years ago.
I want to make my viewPager support Rtl ,and set the current item on the last fragment, but when I write this code in my MainActivity.java , I don't know how I can access the last fragment.
iViewPager.setCurrentItem();
this is my MainActivity.java where I define the viewPager
package com.example.imajid.grid2.activities;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.imajid.grid2.R;
import com.example.imajid.grid2.adapters.PagerAdapter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//----------------------- set toolbar
Toolbar iToolbar = (Toolbar) findViewById(R.id.xmlToolbar);
setSupportActionBar(iToolbar);
//------------------------------ initialize toolbar
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.menu);
actionBar.setDisplayHomeAsUpEnabled(true);
//**************************** find tablayout *****************************//
TabLayout iTabLayout = (TabLayout) findViewById(R.id.xmlTabLayout);
iTabLayout.addTab(iTabLayout.newTab());
iTabLayout.addTab(iTabLayout.newTab());
iTabLayout.addTab(iTabLayout.newTab());
iTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//******************************* Array list for sending titles *********************************//
ArrayList<String> viewPagersTitle = new ArrayList<String>();
viewPagersTitle.add("صفحه اصلی");
viewPagersTitle.add("فیلم");
viewPagersTitle.add("آهنگ");
//******************************* find view pager in xml *****************************//
ViewPager iViewPager = (ViewPager) findViewById(R.id.xmlViewPager);
//************************************** Rtlizer view pager ************************************
====>iViewPager.setCurrentItem();
//************************************** find adapter for view pager *******************************//
PagerAdapter viewPagerAdapter = new PagerAdapter(getSupportFragmentManager() , iTabLayout.getTabCount() , viewPagersTitle);
//************************************ set adapter for view pager *******************************//
iViewPager.setAdapter(viewPagerAdapter);
//*********************************** setup tablayout to view pager********************************//
iTabLayout.setupWithViewPager(iViewPager);
mDrawerLayout = (DrawerLayout) findViewById(R.id.xmlDrawerLayoutRoot);
//-------------------------------------- find navigation view
NavigationView navigationView = (NavigationView)findViewById(R.id.xmlNavigation);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
Toast.makeText(MainActivity.this , menuItem.getTitle() , Toast.LENGTH_SHORT).show();
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
//onoptionItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
String btnName = null;
switch (itemId){
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.menu_setting:
btnName = "Setting";
break;
case R.id.menu_compass:
btnName = "Compass";
break;
case R.id.menu_help:
btnName = "Help";
break;
}
Toast.makeText(MainActivity.this , "You clicked at " + btnName , Toast.LENGTH_SHORT).show();
return true;
}
}
and this is my view pager adapter
package com.example.imajid.grid2.adapters;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import com.example.imajid.grid2.fragments.TabFragment1;
import com.example.imajid.grid2.fragments.TabFragment2;
import com.example.imajid.grid2.fragments.TabFragment3;
import java.util.ArrayList;
/**
* Created by imajid on 12/8/2015.
*/
public class PagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<String> titles;
int numOfTabs;
public PagerAdapter(FragmentManager fm , int numOfTabs , ArrayList<String> titles) {
super(fm);
this.numOfTabs =numOfTabs;
this.titles = titles;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return this.numOfTabs;
}
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
You can change your some portion of code::
#Override
public Fragment getItem(int position) {
switch (position){
case 2://change it from 0 -> max
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 0:
TabFragment3 tab3 = new TabFragment3();
return tab3;
default:
return null;
}
}
or you can even change the fragment name too::
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
TabFragment3 tab3 = new TabFragment3();
return tab3;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment1 tab1 = new TabFragment1();
return tab1;
default:
return null;
}
}
try this out ..
you can use
HomeListFragment fragment =
(HomeListFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.viewpager+":0");
//"android:switcher:"+R.id.viewpager+":0" returns current fragment
if(fragment != null) // could be null if not instantiated yet
{
if(fragment.getView() != null)
{
ViewPager.LayoutParams params = (ViewPager.LayoutParams) group.getLayoutParams();
params.setLayoutDirection(0x00000001);
}
}
notice that 0x00000001 is const value for rtl refer to here
this solution is little tricky but , this is the best answer for this question
first of all , we need to access to getCount() of PagerAdapter.java
for this in the mainActivity.java and in the onCreate() after the defining the Instance of PagerAdapter and setting the PagerAdapter on ViewPager ( this is important write exactly after the setting the adapter) we write :
iViewPager.setCurrentItem(viewPagerAdapter.getCount());
so in my code , MainActivity be like this :
package com.example.imajid.grid2.activities;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.imajid.grid2.R;
import com.example.imajid.grid2.adapters.PagerAdapter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//----------------------- set toolbar
Toolbar iToolbar = (Toolbar) findViewById(R.id.xmlToolbar);
setSupportActionBar(iToolbar);
//------------------------------ initialize toolbar
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.menu);
actionBar.setDisplayHomeAsUpEnabled(true);
//**************************** find tablayout *****************************//
TabLayout iTabLayout = (TabLayout) findViewById(R.id.xmlTabLayout);
iTabLayout.addTab(iTabLayout.newTab());
iTabLayout.addTab(iTabLayout.newTab());
iTabLayout.addTab(iTabLayout.newTab());
iTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//******************************* Array list for sending titles *********************************//
ArrayList<String> viewPagersTitle = new ArrayList<String>();
viewPagersTitle.add("صفحه اصلی");
viewPagersTitle.add("فیلم");
viewPagersTitle.add("آهنگ");
//******************************* find view pager in xml *****************************//
ViewPager iViewPager = (ViewPager) findViewById(R.id.xmlViewPager);
//************************************** Rtlizer view pager ************************************
//************************************** find adapter for view pager *******************************//
PagerAdapter viewPagerAdapter = new PagerAdapter(getSupportFragmentManager() , iTabLayout.getTabCount() , viewPagersTitle);
//************************************ set adapter for view pager *******************************//
iViewPager.setAdapter(viewPagerAdapter);
iViewPager.setCurrentItem(viewPagerAdapter.getCount());
//*********************************** setup tablayout to view pager********************************//
iTabLayout.setupWithViewPager(iViewPager);
mDrawerLayout = (DrawerLayout) findViewById(R.id.xmlDrawerLayoutRoot);
//-------------------------------------- find navigation view
NavigationView navigationView = (NavigationView)findViewById(R.id.xmlNavigation);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
Toast.makeText(MainActivity.this , menuItem.getTitle() , Toast.LENGTH_SHORT).show();
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
//onoptionItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
String btnName = null;
switch (itemId){
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.menu_setting:
btnName = "Setting";
break;
case R.id.menu_compass:
btnName = "Compass";
break;
case R.id.menu_help:
btnName = "Help";
break;
}
Toast.makeText(MainActivity.this , "You clicked at " + btnName , Toast.LENGTH_SHORT).show();
return true;
}
}

How to Handle Button OncClick Fragments Android

Can someone help me how to handle button clicks on fragments? I'm having an error when I go to my fragment in profile. When I click the button on fragment_profile, I always get an error.
Here's my code:
For my Main Activity Class:
package com.the.healthescort;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
Context CTX = this;
public int see;
public String cc="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Mod();
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
//
}
public void Mod(){
DatabaseOperation DOp = new DatabaseOperation(CTX);
Cursor cr = DOp.getInformation(DOp);
if (cr != null && cr.getCount()>0){
cr.moveToFirst();
startManagingCursor(cr);
for(int i=0;i<cr.getCount();i++){
String x = cr.getString(3);
String y = cr.getString(4);
String c = x.replace("\"", "");
String r = c.replace("' ", ".");
double get = Double.parseDouble(r);
int z = (int) get;
double w = get % z;
double roundOff = Math.round(w*10);
int ans = (z*12)+ (int)roundOff;
String g = y.replace(" lbs","");
double h = Double.parseDouble(g);
double total = (h/(ans*ans))*703;
}
}else{
Intent k = new Intent(MainActivity.this, Add_Profile.class);
startActivity(k);
finish();
}
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); // For AppCompat use getSupportFragmentManager
switch(position) {
case 0:
mTitle = getString(R.string.title_section1);
tx.replace(R.id.container,PlaceholderFragment.newInstance(position + 1));
tx.commit();
break;
case 1:
mTitle = getString(R.string.title_section2);
tx.replace(R.id.container,new profile_fragment());
tx.commit();
break;
case 2:
fragment = PlaceholderFragment.newInstance(position + 1);
break;
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
break;
case 2:
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#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 placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public int check;
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
My Fragment code:
package com.the.healthescort;
import android.content.Context;
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.TextView;
public class profile_fragment extends Fragment {
public static Fragment newInstance(Context context) {
profile_fragment f = new profile_fragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View root = (View) inflater.inflate(R.layout.fragment_profile_fragment, container, false);
Button a = (Button) root.findViewById(R.id.sana);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
TextView b = (TextView) view.findViewById(R.id.gumana);
b.setText("Click!");
}
});
return root;
}
}
public void onClick(View view){
TextView b = (TextView) view.findViewById(R.id.gumana);
b.setText("Click!");
}
in this case, view is the object on which you called setOnClickListener, the button you called a. If you call view.findViewById(R.id.yourid), on a View's subclass, what the system does is to check if the id you passed as parameter is equal to the one of the view on which you are calling findViewById. It the view is subclass of ViewGroup's, then the system looks for the provided id among the ViewGroup's children. In your case the view's id is R.id.sana, and you are passing R.id.gumana as parameter. Since the ids don't match the system returns null. You probably have declared the TextView, inside fragment_profile_fragment.xml.
Change
TextView b = (TextView) view.findViewById(R.id.gumana);
with
TextView b = (TextView) getView().findViewById(R.id.gumana);

Error in PageAdapter and viewpager in eclipse android?

So i am new developer in Android and started on a projct in which at a pont I needed to make scrollable tabs (like in google play store). So I used the following code. I can't work out the error. I use eclipse ADT bundle(like everyone else of course)!
Here is the mainActivity
package com.deathreaper.main;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import com.deathreaper.com.tabs.Tab1;
import com.deathreaper.com.tabs.Tab2;
import com.deathreaper.com.tabs.Tab3;
public class MainActivity extends FragmentActivity {
ViewPager viewPager= null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager = getSupportFragmentManager();
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(MyAdapter(fragmentManager));
}
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragment= null;
switch (arg0) {
case 1:
fragment = new Tab1();
break;
case 2:
fragment = new Tab2();
break;
case 3:
fragment = new Tab3();
break;
default:
break;
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}`
}
The error is in line 25 viewPager.setAdapter(MyAdapter(fragmentManager)); i.e. the last line in manActivity class.
The error it is giving is as follows
"The method MyAdapter(Fragment) is undefined for the type Mainactivity"
Please if anyone could help, it would be really awesome!Thanks:D :D
Use new keyword.
viewPager.setAdapter(new MyAdapter(fragmentManager));

Categories

Resources