When writing this code I am having a constant error where it says "Type mismatch: cannot convert from Fragment_1 to Fragment" on the three lines like this "Fragment Fragment1 = new Fragment_1();". I believe this is the problem that is also causing my fragments to not appear when the code is run because the code doesn't know what corresponds to the Listener.
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.view.Menu;
public class MainActivity extends SherlockFragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setTitle("TabApp");
ActionBar.Tab Frag1Tab = actionbar.newTab().setText("Tab 1");
ActionBar.Tab Frag2Tab = actionbar.newTab().setText("Tab 2");
ActionBar.Tab Frag3Tab = actionbar.newTab().setText("Tab 3");
Fragment Fragment1 = new Fragment_1();
Fragment Fragment2 = new Fragment_2();
Fragment Fragment3 = new Fragment_3();
Frag1Tab.setTabListener(new MyTabsListener(Fragment1));
Frag2Tab.setTabListener(new MyTabsListener(Fragment2));
Frag3Tab.setTabListener(new MyTabsListener(Fragment3));
actionbar.addTab(Frag1Tab);
actionbar.addTab(Frag2Tab);
actionbar.addTab(Frag3Tab);
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment){
this.fragment = fragment;
}
#Override
public void onTabSelected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
}
Here is the Fragment_1 class:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment_1 extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_1, container, false);
}
}
Is Fragment_1 inheriting from android.support.v4.app.Fragment(or SherlockFragment)? Or does it inherit from android.app.Fragment? It should be android.support.v4.app.Fragment
Related
Phone's orientation and fragment translation bug.
I have an activity and some fragments. The first fragment comes right away over the activity with a logo and after 3 seconds the second fragment translates over. The problem is if I change the orientation of the phone, the 1st fragment reappears with the same delay and same behavior as I start the app.
MainActivity:
package com.mainpackage.pinbook;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.mainpackage.pinbook.com.autentification.LoginFragment;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container , new MainScreen());
fragmentTransaction.commit();
}
}
MainScreen:
package com.mainpackage.pinbook;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mainpackage.pinbook.com.autentification.*;
public class MainScreen extends Fragment{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_screen , container , false);
}
private TextView entry_text;
public static final String TAG = MainScreen.class.getSimpleName();
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
slide();
}
}, 3000);
}
private void slide(){
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(0,R.anim.slide_in_left);
fragmentTransaction.replace(R.id.main_container , new LoginFragment());
fragmentTransaction.commit();
}
#Override
public void onStop() {
super.onStop();
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
#Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
}
The second fragment can be blank.
What I want is simply to remain on the current fragment when I change the phone's orientation
When the device rotates onCreate() gets called again.
Wrap your fragment transaction in onCreate() like so:
if (savedInstanceState == null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container , new MainScreen());
fragmentTransaction.commit();
}
This will prevent the first fragment from being put back on top when you rotate.
When I run the Android Application there is some initialize and after that THERE IS connection to server when the connection end, I see on the screen Three Fragment Tabs.
Tab1 - Welcome to app.
Tab2 - Make some calculation.
tab3 - Game.
I want only when user type on "tab3 - game" to start/run active the LibGDX.
How can I do it?
Many Thanks For any help.
My Code MainActivity.java:
package com.example.stam;
import android.app.ActionBar;
import android.app.FragmentTransaction;
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.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ViewPager viewPager=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.pager);
final ActionBar actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
addTabs(actionBar);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
Log.d("VIVZ","onPageScrolled "+i+" "+v+" "+i2);
}
#Override
public void onPageSelected(int i) {
actionBar.setSelectedNavigationItem(i);
Log.d("VIVZ","onPageSelected "+i);
}
#Override
public void onPageScrollStateChanged(int i) {
if(i==ViewPager.SCROLL_STATE_IDLE)
Log.d("VIVZ","onPageScrollStateChanged scroll state idle "+i);
if(i==ViewPager.SCROLL_STATE_DRAGGING)
Log.d("VIVZ","onPageScrollStateChanged scroll state dragging "+i);
if(i==ViewPager.SCROLL_STATE_SETTLING)
Log.d("VIVZ","onPageScrollStateChanged scroll state settling "+i);
}
});
}
private void addTabs(ActionBar actionBar)
{
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("Tab 1");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("Tab 2");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionBar.newTab();
tab3.setText("Tab 3");
tab3.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
// Log.d("VIVZ","onTabSelected "+tab.getText());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// Log.d("VIVZ","onTabUnselected "+tab.getText());
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// Log.d("VIVZ","onTabReselected "+tab.getText());
}
}
class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment=null;
if(i==0)
{
fragment=new FragmentA();
}
if(i==1)
{
fragment=new FragmentB();
}
if(i==2)
{
fragment=new FragmentGame();
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
}
My code FragmentGame.java:
package com.example.stam;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentGame extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_c,container,false);
}
}
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));
To more understand more how fragments work, I created a mainactivity its layout has one<fragment>. and I created two java classes extend fragment and each of these classes has its own layout.
In the java class that extends fragment, I initailise my views textview inside onActivityCreated(), and as shown below in the code in the same class, i created two methods setbtnclicks(int clicks) and getbtnclicks(). From the mainactivity i assign number of clicks of a button to setbtnclicks() and i try to display the number of clicks on the textview of the class that extends fragment by calling getbtnclicks from inside onActivivtyCreated(). But the problem is, this method always displays zero, as if the number of clicks are not incremented.
MainActivtity:
//global variable
private int i = 0;
...
...
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment mSelectedFragment;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
switch (v.getId()) {
case R.id.btn00:
mSelectedFragment = new Fragment00();
mFragmentManager = getFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment00ID, mSelectedFragment);
mFragmentTransaction.commit();
mFragment00.setBtnClicks(i);
i++;
break;
Java_Class "Fragment00"
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated(): "+order);
order++;
mTv = (TextView) getView().findViewById(R.id.fragment00Tv00);
mTv.setText("the Button was clicked "+getBtnClicks()+ " time(s)");
Log.i(TAG, "onActivityCreated(): "+getBtnClicks());
}
Once i have implemented a more or less same program like yours.The objective was to increment the number of button clicks in both the fragments.Here is the code.Hope it helps:
MainActivity.java
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
public class MainActivity extends Activity implements OnClickListener{
Button btn1,btn2;
int click1=0,click2=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment fragment = null;
if (v == btn1) {
// do stuff for button left
click1++;
Bundle bundle=new Bundle();
String clickstring1=Integer.toString(click1);
bundle.putString("name1",clickstring1 );
fragment = new FragButton1();
fragment.setArguments(bundle);
}
if (v == btn2) {
// do stuff for button right
click2++;
Bundle bundle=new Bundle();
String clickstring2=Integer.toString(click2);
bundle.putString("name2",clickstring2 );
fragment = new FragButton2();
fragment.setArguments(bundle);
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
}
else{
Log.e("MainActivity", "Error in creating fragment");
}
}
}
FragButton1.java
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
#SuppressLint("NewApi")
public class FragButton1 extends Fragment{
TextView tv;
public FragButton1(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragbutton1, container, false);
String strtext=getArguments().getString("name1");
tv=(TextView)rootView.findViewById(R.id.tv_times1);
tv.setText(strtext);
return rootView;
}
}
FragButton2.java
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
#SuppressLint("NewApi")
public class FragButton2 extends Fragment{
TextView tv2;
public FragButton2(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragbutton2, container, false);
tv2=(TextView)rootView.findViewById(R.id.tv_times2);
String strtext=getArguments().getString("name2");
tv2.setText(strtext);
return rootView;
}
}
Here there are 2 buttons and onclick of a button a new fragment will be launched and the fragment will display the number of times a button is clicked in a textview.
increment i before the fragment Transaction
switch (v.getId()) {
case R.id.btn00:
i++;
mSelectedFragment = new Fragment00();
mFragmentManager = getFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment00ID, mSelectedFragment);
mFragmentTransaction.commit();
mFragment00.setBtnClicks(i);
break;
What I am doing:
I have a fragmentabhost in a fragment that has 3-tabs in it.
On click of tabs i am able to change the fragments dynamically
On click of first tab i am displaying RatingAscending.class
What I am trying to do:
Now Onclick of the same tab I want to display
RatingDescending.class
Note: in the onResume() I am able to detect click event for the firsttab
Now how can I change the tab to RatingDescending.class from RatingAscending.class when I click the tab for the second time
Fragment1.java
public class Fragment1 extends SherlockFragment{
private FragmentTabHost mTabHost;
private static int count=0;
//Mandatory Constructor
public Fragment1() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1,container, false);
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Rating"),
RatingAscending.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Price"),
PriceAscending.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Distance"),
DistanceAscending.class, null);
return rootView;
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
count++;
Log.d("You clicked ", count+"time");
}
});
}
}
I Finally found answer myself here (I refered this post), i hope this helps someone in future
The below piece of code is important to achieve this goal
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragment;
import com.dev.navigation.DistanceAscending;
import com.dev.navigation.DistanceDescending;
import com.dev.navigation.PriceAscending;
import com.dev.navigation.RatingAscending;
import com.dev.navigation.RatingDescending;
public class Fragment1 extends SherlockFragment{
private FragmentTabHost mTabHost;
private static int count=0;
//Mandatory Constructor
public Fragment1() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Log", "onCreate");
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1,container, false);
Log.d("Log", "onCreateView");
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Rating"),RatingAscending.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Price"),PriceAscending.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Distance"),DistanceAscending.class, null);
return rootView;
}
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d("Log", "onStart");
mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
count++;
Log.d("You clicked ", count+"time");
RatingDescending fObj=new RatingDescending();
//FragmentTransaction manager = getChildFragmentManager().beginTransaction();
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
//android.support.v4.app.FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.realtabcontent, fObj);
ft.commit();
/*
*-------OVERLAPPING----FRAGMENT------CODE
* RatingDescending fObj=new RatingDescending();
android.support.v4.app.FragmentManager manager = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.realtabcontent, fObj);
ft.commit();
*/
}
});
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d("Log", "onResume");
}
}