RSS

Tabs inside popup – Android

23 Oct

package com.yourpackage;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

public class TabPopupActivity extends ActivityGroup{
 TabHost tabHost;
 TabWidget tabWidget;
private AlertDialog alertDialog;
View view;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 try {
 openInDialog();
 createTab();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }


public void openInDialog(){
 try {
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 view = getLayoutInflater().inflate(R.layout.tab_popup_layout, null);
 tabHost = (TabHost)view.findViewById(R.id.tabhost);
 builder.setView(view);
 alertDialog = builder.create();
 alertDialog.show();
 DisplayMetrics metrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(metrics);
 int height = metrics.heightPixels;
 int width = metrics.widthPixels;
 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
 lp.copyFrom(alertDialog.getWindow().getAttributes());
 lp.width = width-30;
 lp.height = height-30;
 alertDialog.getWindow().setAttributes(lp);
 } catch (Exception e) {
 e.toString();
 }
 }

private void textAlignCenter(){
 try {
 int tabCount = tabHost.getTabWidget().getTabCount();
 for (int i = 0; i < tabCount; i++) {
 final View view = tabHost.getTabWidget().getChildTabViewAt(i);
 if ( view != null ) {
 // reduce height of the tab
 view.getLayoutParams().height *= 0.66;

// get title text view
 final View textView = view.findViewById(android.R.id.title);
 if ( textView instanceof TextView ) {
 // just in case check the type

// center text
 ((TextView) textView).setGravity(Gravity.CENTER);
 // wrap text
 ((TextView) textView).setSingleLine(false);

// explicitly set layout parameters
 textView.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT;
 textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
 }
 }
 }
 } catch (Exception e) {
 e.toString();
 }
 }

private void createTab(){
 try {
 // Tab 1
 TabHost.TabSpec Tab1spec = tabHost.newTabSpec("Tab 1");
 Tab1spec.setIndicator("Tab 1");
 Intent Tab1Intent = new Intent(this, Tab1Activity.class);
 Tab1spec.setContent(Tab1Intent);


// Adding all TabSpec to TabHost
 tabHost.setup(this.getLocalActivityManager());
 tabHost.addTab(Tab1spec); 

 textAlignCenter();
 } catch (Exception e) {
 e.toString();
 }
 }

}

Advertisements
 
Leave a comment

Posted by on October 23, 2013 in Android Source Code

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: