Issue on implementing action bar with tabs using support library - android

I am implementing action bar with support library in my app. Min api level of my app is 8 and max is 19. In main activity class, I used android.app.ActionBar to import action bar, but it shows following error on getting action bar "Call requires API level 11 (current min is 8): android.app.Activity#getActionBar". So I changed import to 'android.support.v7.app.ActionBar' for action bar, now error changed to this ' cannot convert from android.app.ActionBar to android.support.v7.app.ActionBar'.
My main activity code is as follow.
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
public class MainActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Android").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("iOS").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Windows").setTabListener(tabListener));
}
}
android manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learn2crack.tab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.learn2crack.tab.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main_activity is
<?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" />
windows_frag.xml is
<?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:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"/>
</LinearLayout>
One of my fragment is
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.TextView;
public class Windows extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View windows = inflater.inflate(R.layout.windows_frag, container, false);
((TextView)windows.findViewById(R.id.textView)).setText("Windows");
return windows;
}}
Kindly help me out. I have also implemented following tutorial but error remain same.
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
Please don't suggest ActionBarSherlock. I want to know my issue before trying other library.

if you are using android.support.v7.app.ActionBar support package, you must use
actionBar = getSupportActionBar();
and change your activity like this
public class MainActivity extends ActionBarActivity
implements ActionBar.TabListener {
// your code goes here
}

Related

Unfortunately projects has stopped: onClickListener

When I open the app it says "Unfortunately program has stopped". So i can't use the application. How can i repair this?
This is my code:
I put the incorrect code, this is the real code, same error
package com.RaulLara.projects;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.app.Activity;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button btn = (Button) findViewById(R.id.btn1);
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
setupmybutton();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
private void setupmybutton() {
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
#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;
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends 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;
}
}
}
This is my
fragment_main:
<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.RaulLara.projects.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:text="#string/button" />
</RelativeLayout>
And this is my
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.RaulLara.projects"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.RaulLara.projects.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You are using wrong id for the button. This is causing a NULL mybutton1 to be returned and hence a null pointer exception.
Change
Button mybutton1 = (Button) findViewById(R.id.button1);
to
Button mybutton1 = (Button) findViewById(R.id.btn1);
Good Luck
Since you are setting up your button in OnCreate of the MainActivity make sure your declare your btn1 in activity_main and not in the fragment's layout fragment_main

Implementing a custom indicator with ViewPageIndicator - How can I do it to a gallery?

I'm working on a project and I managed to get to the point where I can show my images in a gallery sort of style using PagerAdapter. Now what I want to do is add an indicator to which image is on the screen and I figure that VPI could help me out - the problem is that I'm really new to code and don't know how to implement it, could someone help?
Here's a gif of what I want to achieve - the green line indicator: http://imgur.com/GgYgY8I
I made a simpler version of my project to fit in this post, here it is:
ImageAdapter.java
package com.exp.viewpagersstest1;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImageAdapter extends PagerAdapter {
Context ssContext;
private int[] ssImages = new int[] { R.drawable.splash1,R.drawable.splash2, R.drawable.splash3 };
ImageAdapter(Context ssContext) {
this.ssContext = ssContext;
}
#Override
public int getCount() {
return ssImages.length;
}
#Override
public boolean isViewFromObject(View ssView, Object ssObject) {
return ssView == ((ImageView) ssObject);
}
#Override
public Object instantiateItem(ViewGroup ssContainer, int ssPosition) {
ImageView ssImageView = new ImageView(ssContext);
ssImageView.setScaleType(ImageView.ScaleType.FIT_XY);
ssImageView.setImageResource(ssImages[ssPosition]);
((ViewPager) ssContainer).addView(ssImageView, 0);
return ssImageView;
}
#Override
public void destroyItem(ViewGroup ssContainer, int ssPosition,
Object ssObject) {
((ViewPager) ssContainer).removeView((ImageView) ssObject);
}
}
SSTest.java
package com.exp.viewpagersstest1;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class SSTest extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sstest);
ViewPager ssViewPager = (ViewPager) findViewById(R.id.ss_view_pager);
ImageAdapter ssAdapter = new ImageAdapter(this);
ssViewPager.setAdapter(ssAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sstest, menu);
return true;
}
activity_sstest.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=".SSTest" >
<android.support.v4.view.ViewPager
android:id="#+id/ss_view_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true" />
</RelativeLayout>
ViewPagerSSTest1 Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exp.viewpagersstest1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.exp.viewpagersstest1.SSTest"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock.NoActionBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thanks in advance to anyone who replies.
Was much more simple than I expected, just needed to add the following to my xml:
<com.viewpagerindicator.UnderlinePageIndicator
android:id="#+id/pageIndic"
android:layout_height="3dp"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
app:selectedColor="#ef4154"/>
And the following to the oncreate under the java file:
UnderlinePageIndicator pageIndicator = (UnderlinePageIndicator) findViewById(R.id.pageIndic);
pageIndicator.setViewPager(ssViewPager);

ActionBar menu on bottom bar

How can I get the menu icon to appear on the bottom bar?
It seems too wasteful to have a full bar, empty, just for the menu icon.
My target is Jelly Bean, I am not using Sherlock.
package com.example.test;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity implements ActionBar.TabListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = actionBar.newTab().setTabListener(this).setText("TAB LEFT");
actionBar.addTab(tab);
tab = actionBar.newTab().setTabListener(this).setText("TAB RIGHT");
actionBar.addTab(tab);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
}
#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(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In your AndroidManifest you have to set the targetSdkVersion to 13 or earlier like this:
<uses-sdk android:targetSdkVersion="13" />
The overflow menu then moves to the bottom since Android tries to be version compatible
You can add this entry under the "application" tag in your manifest:
android:uiOptions="splitActionBarWhenNarrow"
read about it here.

ActionBarSherlock tab based app crashes on older Android devices

After having read quite a lot about it I decided to use ActionBarSherlock to provide a modern look and at the same time support older Android versions.
I created an Android tab based application using the Eclipse Project wizard and modyfied it to use ActionBarSherlock. The result of the main activity is like this:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayHomeAsUpEnabled(false);
// For each of the sections in the app, add a tab to the action bar.
actionBar.addTab(actionBar.newTab().setText(R.string.title_tab_1).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.title_tab_2).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.title_tab_3).setTabListener(this));
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
The manifest file looks like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.comp.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.app.TabActivity" />
</activity>
</application>
</manifest>
When I run it on an Android 4.0.4 device it works fine but when running it on an Android 2.3.3 device it crashes in the "setContentView" line. Any ideas why?
Thanks.
If you want to use ActionBar Sherlock you'll have to use This Theme instead:
Theme.Sherlock.Light

ActionBarSherlock doesn't show anything on Android 2.3.3

I set up ABS with a simple Android project and can't find out why no single part of the action bar shows up on 2.3.3 (left), but works perfectly on 4.0 (right: title, tabs and menu).
What I did:
Created new Android Project in Eclipse
Added android:theme="#style/Theme.Sherlock" in <application>
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" />
Changed Java compliance to 1.6 (don't think this matters, should be the default anyway)
Added ABS as library project (compiled against SDK version 15 as well)
Did not add support library to my project (doesn't make a difference if I do)
Note: I have to use static attachment so I cannot extend one of the Sherlock* classes.
Source code:
package de.andidog.testthesherlock;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import com.actionbarsherlock.ActionBarSherlock;
import com.actionbarsherlock.ActionBarSherlock.OnCreateOptionsMenuListener;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
public class TestTheSherlockActivity extends Activity implements ActionBar.TabListener, OnCreateOptionsMenuListener
{
protected ActionBarSherlock sherlock = ActionBarSherlock.wrap(this);
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
sherlock.setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW);
showTabsNav();
// TODO: check order of these statements
sherlock.setContentView(R.layout.main);
setContentView(R.layout.main);
}
private void showTabsNav()
{
final ActionBar ab = sherlock.getActionBar();
ab.setDisplayUseLogoEnabled(true);
for(int i = 0; i < 5; ++i)
ab.addTab(ab.newTab().setIcon(R.drawable.ic_launcher).setTabListener(this));
ab.setDisplayShowHomeEnabled(true);
ab.setDisplayShowTitleEnabled(true);
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu)
{
return sherlock.dispatchCreateOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Refresh").setIcon(android.R.drawable.ic_menu_rotate).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
Log.d("TestTheSherlockActivity", "TAB RESELECTED");
}
#Override
public void onTabSelected(Tab tab, android.support.v4.app.FragmentTransaction ft)
{
Log.d("TestTheSherlockActivity", "TAB CLICKED");
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
Log.d("TestTheSherlockActivity", "TAB UNSELECTED");
}
}
My AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.andidog.testthesherlock"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock" >
<activity
android:name=".TestTheSherlockActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
How do I get this to work on 2.x?
Remove the second setContentView call, this is overwriting the layout inflated by ABS.
You should only be using the static attachment technique if you require extending from a non-Android base class. Otherwise it's much easier to extend from SherlockActivity (or any of the others).

Categories

Resources