22 Şubat 2016 Pazartesi

tabhost with fragment (changeable tab underline color etc.) in fragment Android

 1. MainFragment oluşturalım aşagıdaki  kodu ekleyin .

Mavi yazılı kodlardaki kodlar color renklerini tutan degişkenlerdir.Bunlar
  tabhostun arkaplan renklerini
  child tabların renklerini
  altçizgi rengini
  tıklanan textin rengini
  tıklanmayan textin rengini   tutar
Dilediginiz renk kodlarını yazarak değiştirebilirsiniz

import java.util.ArrayList;

import android.R.color;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
public class MainFragment extends Fragment{

     TabManager mTabManager;
     private static  View myvidget;
     private static TabWidget widget;
   //tıklanmayan tabın text colorı
  private static String UnselectedTabTitleColor="#d9d9d9";
  //tıklanan tabın text colorı
  private static String selectedTabTitleColor="#FFFFFF";
  //tabın arkaplan  colorı
  private String tab_color="#008b9d";
  //child tabın arkaplan  colorı
  private String child_tab="#FFFFFF";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTabManager = new TabManager(getActivity(), getChildFragmentManager(),
                R.id.realtabcontent);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.mainfragment, container, false);
        TabHost host = mTabManager.handleCreateView(v);
        mTabManager.addTab(host.newTabSpec("tab1").setIndicator("tab1"),
                TabFirstFragment.class, null);
        mTabManager.addTab(host.newTabSpec("tab2").setIndicator("tab2"),
                TabSecondFragment.class, null);
        mTabManager.addTab(host.newTabSpec("tab3").setIndicator("tab3"),
               TabThirdFragment.class, null);
     
      widget = host.getTabWidget();
      TextView tv ;
        for(int i = 0; i < widget.getChildCount(); i++) {
             myvidget = widget.getChildAt(i);
         
            // Look for the title view to ensure this is an indicator and not a divider.
             tv = (TextView)myvidget.findViewById(android.R.id.title);
            tv.setGravity(Gravity.CENTER);
          //ilk yüklendiğinde ilk tabın texteki varsayılan color ı
            tv.setTextColor(Color.parseColor(UnselectedTabTitleColor));
         
            LayoutParams params = tv.getLayoutParams();
            params.height=LayoutParams.MATCH_PARENT;
            params.width=LayoutParams.MATCH_PARENT;
         
           tv.setBackgroundColor(Color.parseColor(tab_color));
            myvidget.setBackgroundColor(Color.parseColor(child_tab));
            myvidget.setPadding(0, 0, 0, 4);
         
         
        }
        //ilk yüklendiğinde ilk tabın varsayılan color ı
        tv=(TextView)widget.getChildAt(0).findViewById(android.R.id.title);
        tv.setTextColor(Color.parseColor(selectedTabTitleColor));
        //ilk yüklendiğinde ilk tabın varsayılan altçizgisi
        myvidget = widget.getChildAt(0);
        myvidget.setPadding(0, 0, 0, 8);
     
     
        return v;
    }
    @Override
    public void onViewStateRestored(Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        mTabManager.handleViewStateRestored(savedInstanceState);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTabManager.handleDestroyView();
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mTabManager.handleSaveInstanceState(outState);
    }
    /**
     * This is a helper class that implements a generic mechanism for
     * associating fragments with the tabs in a tab host.  DO NOT USE THIS.
     * If you want tabs in a fragment, use the support v13 library's
     * FragmentTabHost class, which takes care of all of this for you (in
     * a simpler way even).
     */
    public static class TabManager implements TabHost.OnTabChangeListener {
        private final Context mContext;
        private final FragmentManager mManager;
        private final int mContainerId;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
        private TabHost mTabHost;
        private TabInfo mLastTab;
        private boolean mInitialized;
        private String mCurrentTabTag;
        static final class TabInfo {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;
            private Fragment fragment;
            TabInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }
        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;
            public DummyTabFactory(Context context) {
                mContext = context;
            }
            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }
        public TabManager(Context context, FragmentManager manager, int containerId) {
            mContext = context;
            mManager = manager;
            mContainerId = containerId;
        }
        public TabHost handleCreateView(View root) {
            if (mTabHost != null) {
                throw new IllegalStateException("TabHost already set");
            }
            mTabHost = (TabHost)root.findViewById(android.R.id.tabhost);
            mTabHost.setup();
            mTabHost.getTabWidget().setDividerDrawable(null);
            mTabHost.setOnTabChangedListener(this);
            return mTabHost;
        }
        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(mContext));
            String tag = tabSpec.getTag();
            TabInfo info = new TabInfo(tag, clss, args);
            mTabs.add(info);
            mTabHost.addTab(tabSpec);
        }
        public void handleViewStateRestored(Bundle savedInstanceState) {
            if (savedInstanceState != null) {
                mCurrentTabTag = savedInstanceState.getString("tab");
            }
            mTabHost.setCurrentTabByTag(mCurrentTabTag);
            String currentTab = mTabHost.getCurrentTabTag();
            // Go through all tabs and make sure their fragments match
            // the correct state.
            FragmentTransaction ft = null;
            for (int i=0; i<mTabs.size(); i++) {
                TabInfo tab = mTabs.get(i);
                tab.fragment = mManager.findFragmentByTag(tab.tag);
                if (tab.fragment != null && !tab.fragment.isDetached()) {
                    if (tab.tag.equals(currentTab)) {
                        // The fragment for this tab is already there and
                        // active, and it is what we really want to have
                        // as the current tab.  Nothing to do.
                        mLastTab = tab;
                    } else {
                        // This fragment was restored in the active state,
                        // but is not the current tab.  Deactivate it.
                        if (ft == null) {
                            ft = mManager.beginTransaction();
                        }
                        ft.detach(tab.fragment);
                    }
                }
            }
            // We are now ready to go.  Make sure we are switched to the
            // correct tab.
            mInitialized = true;
            ft = doTabChanged(currentTab, ft);
            if (ft != null) {
                ft.commit();
               
                mManager.executePendingTransactions();
            }
        }
        public void handleDestroyView() {
            mCurrentTabTag = mTabHost.getCurrentTabTag();
            mTabHost = null;
            mTabs.clear();
            mInitialized = false;
        }
        public void handleSaveInstanceState(Bundle outState) {
            outState.putString("tab", mTabHost != null
                    ? mTabHost.getCurrentTabTag() : mCurrentTabTag);
        }
        @Override
        public void onTabChanged(String tabId) {
       
            if (!mInitialized) {
           
                return;
            }
            FragmentTransaction ft = doTabChanged(tabId, null);
            if (ft != null) {
                ft.commit();
              drawUnderLine();
             
            }
        }
        private void drawUnderLine(){
        TextView dtv ;
        switch(mTabHost.getCurrentTab()){
       
        case 0:
        myvidget = widget.getChildAt(0);
                 myvidget.setPadding(0, 0, 0, 8);
                 myvidget = widget.getChildAt(1);
                 myvidget.setPadding(0, 0, 0, 4);
                 myvidget = widget.getChildAt(2);
                 myvidget.setPadding(0, 0, 0, 4);
                 dtv=(TextView) mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
                 dtv.setTextColor(Color.parseColor(selectedTabTitleColor));
                 dtv=(TextView) mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title);
                 dtv.setTextColor(Color.parseColor(UnselectedTabTitleColor));
                 dtv=(TextView) mTabHost.getTabWidget().getChildAt(2).findViewById(android.R.id.title);
                 dtv.setTextColor(Color.parseColor(UnselectedTabTitleColor));
                       
        break;
        case 1:
        myvidget = widget.getChildAt(0);
                myvidget.setPadding(0, 0, 0, 4);
                myvidget = widget.getChildAt(1);
                myvidget.setPadding(0, 0, 0, 8);
                myvidget = widget.getChildAt(2);
                myvidget.setPadding(0, 0, 0, 4);
                dtv=(TextView) mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
                dtv.setTextColor(Color.parseColor(UnselectedTabTitleColor));
                dtv=(TextView) mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title);
                dtv.setTextColor(Color.parseColor(selectedTabTitleColor));
                dtv=(TextView) mTabHost.getTabWidget().getChildAt(2).findViewById(android.R.id.title);
                dtv.setTextColor(Color.parseColor(UnselectedTabTitleColor));
                       
        break;
        case 2:
        myvidget = widget.getChildAt(0);
                myvidget.setPadding(0, 0, 0, 4);
                myvidget = widget.getChildAt(1);
                myvidget.setPadding(0, 0, 0, 4);
                myvidget = widget.getChildAt(2);
                myvidget.setPadding(0, 0, 0, 8);
                dtv=(TextView) mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
                dtv.setTextColor(Color.parseColor(UnselectedTabTitleColor));
                dtv=(TextView) mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title);
                dtv.setTextColor(Color.parseColor(UnselectedTabTitleColor));
                dtv=(TextView) mTabHost.getTabWidget().getChildAt(2).findViewById(android.R.id.title);
                dtv.setTextColor(Color.parseColor(selectedTabTitleColor));
        break;
        default:
        break;
       
        }
       
         
       
        }
        private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
            TabInfo newTab = null;
            for (int i=0; i<mTabs.size(); i++) {
                TabInfo tab = mTabs.get(i);
                if (tab.tag.equals(tabId)) {
                    newTab = tab;
                }
            }
            if (newTab == null) {
                throw new IllegalStateException("No tab known for tag " + tabId);
            }
            if (mLastTab != newTab) {
                if (ft == null) {
                    ft = mManager.beginTransaction();
                }
                if (mLastTab != null) {
                    if (mLastTab.fragment != null) {
                        ft.detach(mLastTab.fragment);
                    }
                }
                if (newTab != null) {
                    if (newTab.fragment == null) {
                        newTab.fragment = Fragment.instantiate(mContext,
                                newTab.clss.getName(), newTab.args);
                        ft.add(mContainerId, newTab.fragment, newTab.tag);
                    } else {
                        ft.attach(newTab.fragment);
                    }
                }
                mLastTab = newTab;
            }
            return ft;
        }
    }
}
-----------------------------------------------------------------------------------------------------------------

 1.1  mainfragment.xml oluşturun aşağıdaki kodu ekleyin

<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>
        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
</TabHost>


2.1 TabFirstFragment  oluşturun aşagıdaki kodu ekleyin

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabFirstFragment  extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View v=inflater.inflate(R.layout.tabfirstfragment, container, false);
         return v;
    }

}
2.2 tabfirstfragment oluşturun aşagıdaki  kodu ekleyin 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="#d9d9d9">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="tab1"
        android:textAppearance="?android:attr/textAppearanceLarge"/>


</RelativeLayout>
3.1 TabSecondFragment  oluşturun aşagıdaki kodu ekleyin

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabSecondFragment  extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View v=inflater.inflate(R.layout.tabsecondfragment, container, false);
         return v;
    }

}
3.2 tabsecondfragment oluşturun aşagıdaki  kodu ekleyin 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="#d9d9d9">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="tab2"
        android:textAppearance="?android:attr/textAppearanceLarge"/>


</RelativeLayout>
4.1 TabThirdFragment  oluşturun aşagıdaki kodu ekleyin

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabThirdFragment  extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View v=inflater.inflate(R.layout.tabthirdfragment, container, false);
         return v;
    }

}
2.2 tabthirdfragment oluşturun aşagıdaki  kodu ekleyin 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="#d9d9d9">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="tab3"
        android:textAppearance="?android:attr/textAppearanceLarge"/>


</RelativeLayout>

Remove shadow below actionbar android

 res/style.xml  açın.Eksik olanı ekleyin

Eger Light yazıyorsa  silin
Theme.AppCompat.Light

 Android 5.0 için;
<item name="android:elevation">0dp</item>
Support library compatibility için;
<item name="elevation">0dp</item>
kullanın.

--------------------------------------------------------------------------------------------------------------------------
<style name="AppBaseTheme" parent="Theme.AppCompat">
   
      <item name="elevation">0dp</item>

    </style>
<style name="AppTheme" parent="AppBaseTheme">
     
         <item name="android:windowContentOverlay">@null</item>
    </style>
--------------------------------------------------------------------------------------------------------------------------
Androidmanifest dosyasını açın orada application  taglari içinde olan   android:theme="@style/AppTheme"  olarak degiştirin