I am going to develop a android app in Android 4.0 like the below image.
I have to create these tabs dynamically, that dynamically call addTab() method and create tab bar dynamically. Is the ActionBarWithTab right for my requirement.?Guide me to the right way. Please provide tutorials.
EDIT 1: I need to load webview in the separate tab bar created in the ActionBar.
EIDT 2: For creating the tab bar using the TabHost, first i have created the ViewGroup ffor TabHost and added the tab bar items like the below code. In the tab bar items I have created the layout, inside the layout a webview and loaded the dynamic url into the tab bar item. Finally I have set the viewgroup as contentview();
sTabHost = new TabHost(context,null);
sTabHost.setLayoutParams(
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TabWidget tabWidget = new TabWidget(context);
tabWidget.setId(android.R.id.tabs);
sTabHost.addView(tabWidget, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setId(android.R.id.tabcontent);
final float scale = context.getResources().getDisplayMetrics().density;
int paddingtop = (int) (64 * scale + 0.5f);
frameLayout.setPadding(0, paddingtop, 0, 0);
sTabHost.addView(frameLayout, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
sTabHost.setup();
Then I have added the tab items like the below.
public void addTabItem(final String url, String tabTitle, Drawable tabIcon)
{
TabSpec ts1 = sTabHost.newTabSpec(tabTitle);
if(tabTitle.equals(""))
{
int childcount=sTabHost.getChildCount();
tabTitle="Tab" + String.valueOf(childcount+1);
}
if(tabIcon==null)
ts1.setIndicator(tabTitle);
else
ts1.setIndicator(tabTitle,tabIcon);
ts1.setContent(new TabHost.TabContentFactory(){
public View createTabContent(String tag)
{
LinearLayout panel = new LinearLayout(sActiveContext);
panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
panel.setOrientation(LinearLayout.VERTICAL);
FrameLayout layout=new FrameLayout();
// Here I am creating the webview loaded with the url within the layout and placed into the above FrameLayout.
panel.addView(layout);
return panel;
}
});
sTabHost.addTab(ts1);
sTabHost.setOnTabChangedListener(this);
}
Now, how can I achieve this in actionbar like the image I have added.?
I have created the Action Bar with tabs like the below code.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tabA = actionBar.newTab();
tabA.setText("Tab A");
tabA.setTabListener(new TabListener<MyFragmentA>(this, "Tag A", MyFragmentA.class));
actionBar.addTab(tabA);
}
The TabListener class is as below.
public static class TabListener<T extends Fragment>
implements ActionBar.TabListener{
private final Activity myActivity;
private final String myTag;
private final Class<T> myClass;
public TabListener(Activity activity, String tag, Class<T> cls) {
myActivity = activity;
myTag = tag;
myClass = cls;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
// Check if the fragment is already initialized
if (myFragment == null) {
// If not, instantiate and add it to the activity
myFragment = Fragment.instantiate(myActivity, myClass.getName());
ft.add(android.R.id.content, myFragment, myTag);
} else {
// If it exists, simply attach it in order to show it
ft.show(myFragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
if (myFragment != null) {
// Detach the fragment, because another one is being attached
ft.hide(myFragment);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
The Fragment class is as below.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_c, container, false);
WebView webview = (WebView) myFragmentView.findViewById(R.id.webview);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setBuiltInZoomControls(false);
webview.getSettings().setSupportZoom(false);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setDomStorageEnabled(true);
webview.loadUrl("http://jquerymobile.com/demos/1.2.1/");
return myFragmentView;
}
public class MyWebViewClient extends WebViewClient {
/* (non-Java doc)
* @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".mp4"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "video/*");
view.getContext().startActivity(intent);
return true;
}
else {
return super.shouldOverrideUrlLoading(view, url);
}
}
}
In the above implementation, I have created the layout and separate fragment class. Instead of creating like this how can I achieve this without creating the xml layout and separate fragment class. That is like the code i have posted for TabHost tab bar.
EDIT 3: I have created the action bar and tab items like the below code.
public void addTabBar(Context context)
{
sActiveContext=context;
sActionBar = getActionBar();
sActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
public void addTabItem(final String url, String tabTitle)
{
Tab tab = sActionBar.newTab();
if(tabTitle.equals(""))
{
int childcount=sActionBar.getTabCount();
tabTitle="Tab" + String.valueOf(childcount+1);
}
tab.setText(tabTitle);
//Here I need to create the Layout with the webview and loaded into the created tab.
tab.setTabListener(this);
sActionBar.addTab(tab);
}
How can I achieve to load the webview with the given url to the specific tab.?