I have impelemented the actionbar using fragementactivity for view pager action bar using swipe working fne in api leve1 17 or above but coming to the lower version 2.2 to 4.0.3 it showing noclassdef error it is tourching so much please help me to how to retify this code pls help....
target version is 17 The person who solves answer i wll give party dnt miss my party get ready for answer
this is my code
public class Cbntabs extends FragmentActivity implements ActionBar.TabListener{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
ActionBar actionBar;
String result;
// Tab titles
private String[] tabs = { "CBN Voice", "CBN Message" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tabs);
Bundle bundle = getIntent().getExtras();
if(bundle!=null)
{
result=bundle.getString("results");
}
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager(),result);
/**/
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.header_bg));
actionBar.setTitle("TELUGU DESAM PARTY ELITE CLUB");
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
viewPager.setAdapter(mAdapter);
you should use getSupportActionBar.
just import the library project into your workspace
Path to import project is : Android SDK -> Extras -> android ->support - > v7 -> appcompat and the after importing this project right click on your project -> select properties -> android -> add library and he just add the imported library here
Related
I am using Android studio 3.3 canary 8 and my API level is 28. I have an activity which has the action bar and another PagerSlidingTabStrip. The action bar has a title and a close button to go home. Now, I want to get rid of these two toolbars and one toolbar either the action bar or the tabstrip, but I want the close button and the tabs as well. Can the action bar contain tabs? Or can I include the close button in the tabstrip anyhow? If it is not possible then can I do any trick to have something like this? The relevant part of my activity is here:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
ViewPager viewPager = findViewById(R.id.viewPager);
InfoFragmentPagerAdapter adapter = new InfoFragmentPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new InfoAboutFragment (), getString(R.string.about));
adapter.addFragment(new InfoHelpFragment(), getString(R.string.help));
PagerSlidingTabStrip tabsStrip = findViewById(R.id.tabs);
tabsStrip.setViewPager(viewPager);
tabsStrip.setShouldExpand(true);
tabsStrip.setAllCaps(true);
tabsStrip.setTextColor(Color.WHITE);
tabsStrip.setDividerColor(Color.TRANSPARENT);
tabsStrip.setDividerPadding(20);
tabsStrip.setIndicatorColor(Color.WHITE);
tabsStrip.setIndicatorHeight(12);
tabsStrip.setUnderlineColor(Color.TRANSPARENT);
setupActionBar();
}
private void setupActionBar() {
getLayoutInflater().inflate(R.layout.toolbar, findViewById(android.R.id.content));
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(R.string.about);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_white_36dp);
}
}
i am developing android application. i extend my class to Tab Activity.
while i am using getActionBar() or getSupportActionBar(), the activity stops working. when i extend my activity to Activity or ActionBarActivity getTabHost() will not work. Any one pls help me. thanks in advance.
Here is my coding.
public class CommonTabActivity extends TabActivity
implements OnTabChangeListener {
TabHost tabHost;
Bundle response = new Bundle();
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_common_tab);
....
tabHost = getTabHost();
....
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
you can extend your activity to AppCompatActivity (ActionbarActivity is deprecated now), use tablayout from android design library to show tabs and associate viewpager to tablayout to switch between fragments corresponding to each tab.
your activity layout would be something like this.
android.support.v7.widget.Toolbar
android.support.design.widget.TabLayout
android.support.v4.view.ViewPager
There are plenty of examples available to implement this.
http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/
I'm moving my app from minSdk 8 targetSdk 19 built in Eclipse to minSdk 14 targetSdk 22 built in Android Studio, since the announcement of withdrawal of Eclipse support.
Rather than allowing Android Studio to perform a conversion, I wanted to create a new project and manually port my code over.
I'm stuck.
I have a main activity which extends from AppCompatActivity and uses a navigation drawer and a toolbar, which all works fine. My fragment loads ok too, until I try to reference the toolbar.
My main activity code is:
public class MainActivity extends AppCompatActivity implements BlankTestFragment.OnFragmentInteractionListener {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
...
...
My loaded fragment code is:
public class BlankTestFragment extends Fragment {
...
private Toolbar toolbar;
...
public static BlankTestFragment newInstance(String param1, String param2) {
BlankTestFragment fragment = new BlankTestFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public BlankTestFragment() {
// Required empty public constructor
}
public final static String TAG = BlankTestFragment.class.getSimpleName();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
toolbar = ((AppCompatActivity)getActivity()).getSupportActionBar();
}
The last line reports:
Incompatible Types:
Required: android.support.v7.widget.Toolbar
Found: android.support.v7.app.Actionbar
So, what's going wrong? Where is the Actionbar reference coming from?
SOLUTION
In MainActivity I added:
public static Toolbar getToolbar(){
return toolbar;
}
In my test fragment, in onAttach, I added:
toolbar = MainActivity.getToolbar();
toolbar.setTitle("hello");
SOLUTION 2
It turns out that just a small error in my test fragment prevented my original implementation from working.
In MainActivity I had set the toolbar to be a SupportActionBar. So when I tried
toolbar = ((AppCompatActivity)getActivity()).getSupportActionBar();
in the fragment, I had set toolbar to be
private Toolbar toolbar;
but by then it wasn't a ToolBar, so I changed it to
private ActionBar toolbar;
and everything worked fine.
Where is the Actionbar reference coming from?
The return value of getSupportActionBar() is an instance of android.support.v7.app.ActionBar, as is indicated in the JavaDocs. You cannot assign that to a Toolbar field, as Toolbar does not inherit from ActionBar.
Under the covers, setSupportActionBar() takes your Toolbar and wraps it in another object. I do not see an easy way to get the Toolbar back. So, try to work with the ActionBar wrapper that getSupportActionBar() returns.
Remove code getActivity()).getSupportActionBar();. Simply replace it with code getting the toolbar from MainActivity by calling a public get method from that Activity. CommonsWare gave a good hint already.
I've incuded in my app a viewpager which contains 3 fragements with 3 different layouts. Navigating on the net i found these really nice example, i want to add them to my fragments in order to offer a more intersting expercience to the user.
Github resource 1
Github resource 2
Unfortunely they are both compiled in grandle, but i'm working with eclpise. So, my first question is: is possible to "convert" these project and import them to eclipse? And the second: if not, can you give me a working example or a first point to start?
This is my Activity:
public class WelcomeScreen extends FragmentActivity{
List<Fragment> fragments = new Vector<Fragment>();
List<String> fragmentTitles = new Vector<String>();
private PagerAdapter mPagerAdapter;
private ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcomescreen);
fragments.add(Fragment.instantiate(this, welcome1.class.getName()));
fragmentTitles.add("Step 1");
fragments.add(Fragment.instantiate(this, welcome2.class.getName()));
fragmentTitles.add("Step 2");
fragments.add(Fragment.instantiate(this, welcome3.class.getName()));
fragmentTitles.add("Step 3");
this.mPagerAdapter = new PagerAdapter1(super.getSupportFragmentManager(),fragments,fragmentTitles);
mPager = (ViewPager) super.findViewById(R.id.pager);
View pagerStrip = findViewById(R.id.pagerTabStrip);
mPager.setAdapter(this.mPagerAdapter);
}
}
And this one of the fragments:
public class welcome1 extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.welcome1,container,false);
return view;
}
Thanks to all! :)
Hey~ I'm the author of the second repo you've mentioned above. ;-D
Though I've abandoned eclipse for years. I can definitely assure you that eclipse supports gradle projects as well as intellij-like IDE, such as android studio.
But ah ... given android studio is the officially recommended dev tool, why don't you use it? Or at least use the intellij community edition is much better than the clumsy eclipse.
I couldn't manage to change the action bar title for every swipe action, there's always a problem. Either they get mixed or just some of them does not show up. Here are my tab codes:
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
SectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
ViewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPager.setAdapter(mSectionsPagerAdapter);
ViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
Tab tab = actionBar.newTab()
.setIcon(R.drawable.home)
.setTabListener(this);
.
.
.
see answer below before going further into the question.
You can try using this in your Fragment's onResume method :
if(getUserVisibleHint()){
getActionBar().setTitle(title);
}
defining String title as a variable inside your fragment.
You should read the documentation on it: http://developer.android.com/training/implementing-navigation/lateral.html#horizontal-paging
and
http://developer.android.com/training/implementing-navigation/lateral.html#swipe-tabs
Download the source code and use it. It worked for me. I had the same problem.