Android url connection getContentLength() returning negative value (-1) ?
SOLUTION
(ÇÖZÜM)
add this line
(Bu satırı ekle)
urlConnection.setRequestProperty("Accept-Encoding","identiy");
.
.
.
as it's shown below
(aşağıda gösterildiği gibi)
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Accept-Encoding","identiy"); // <--- Add this
int length = urlConnection.getContentLength();
31 Aralık 2016 Cumartesi
22 Aralık 2016 Perşembe
To get Certificate fingerprint(MD5) (Parmak izi sertifikası nasıl alınır) for android
how to run the key hash for facebook
To get android key hash for facebook app (Facebook app için android key hash nasıl alınır)
- Download open ssl for windows here (dosyayı indirin)
- Unzip it to C drive as "openssl " file (Dosyayı "openssl" olarak C sürücüsüne çıkartın)
- Open cmd prompt(Komut istemini açın)
- Type cd C:\Program Files\Java\jdk1.7.0_79\bin (note jdk version might be different in your pc)(komutu yazın- jdk version sizde farklı olabilir ayrıca bakın)
- And Finally type keytool -export -alias myAlias -keystore C:\Users\your user name\.android\myKeyStore | C:\openssl\bin\openssl sha1 -binary | C:\openssl\bin\openssl enc -a -e(Kodu yazın)
- If it asks keystore password it's default " android " type it ok (Şifre sorarsa "android" yazın )
- You are done! ( Bitti ! )
12 Aralık 2016 Pazartesi
android.content.res.Resources$NotFoundException:
" FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x4
at android.content.res.Resources.getText "
This exception occours Even when the resource is presents It's most likely setting anything except string in text like here;
(Büyük ihtimalle setText() ederken aldığı değer string değil örnek;)
int i=10;
mtextView.setText(i);
it gets error above.It should be converted to string
(Yukarıdaki hatalı.Değerin tipi stringe çevirilmelidir)
SOLUTION:
mtextView.setText(String.valueOf(i))
10 Mayıs 2016 Salı
parent.getChildAt(positon) returns null visible child çözüldü
listview.getChildAt(positon) returns null hatasının çözümü
Aşağıdaki kodu listivew içinde yazın
View view;
int firstPos = listview.getFirstVisiblePosition();
int wantedPos = position - firstPos;
if ((wantedPos >= 0) && (wantedPos <= listview.getChildCount())
{
view =listview.getChildAt(wantedPos);
if (view == null){
return;}
else{
// burada view ' ı kullanın }
}
Aşağıdaki kodu listivew içinde yazın
View view;
int firstPos = listview.getFirstVisiblePosition();
int wantedPos = position - firstPos;
if ((wantedPos >= 0) && (wantedPos <= listview.getChildCount())
{
view =listview.getChildAt(wantedPos);
if (view == null){
return;}
else{
// burada view ' ı kullanın }
}
6 Mayıs 2016 Cuma
ConcurrentModificationException for ArrayList
ConcurrentModificationException for ArrayList Error
Böyle bir for döngüsü kullanıyorsanız
for (iterable_type iterable_element : iterable)
{
}
Yerine
for (int i = 0; i < array.length; i++) {
}
bu for döngüsünü kullanın sorun ortdan kalkacaktır
ConcurrentModificationException occurs when using iterators
Böyle bir for döngüsü kullanıyorsanız
for (iterable_type iterable_element : iterable)
{
}
Yerine
for (int i = 0; i < array.length; i++) {
}
bu for döngüsünü kullanın sorun ortdan kalkacaktır
ConcurrentModificationException occurs when using iterators
30 Nisan 2016 Cumartesi
Unmarshalling unknown type code error(Parcel)
How to fix Unmarshalling unknown type code xxx at ofset
RuntimeException -Unmarshalling unknown type
Bunun nedeni çok saçma ama read ve write sıralanması aynı olmamasından kaynaklanan bir hata örnek aşagıdaki gibi;
This can be caused by data being read in a different order than it's
been written. For example:
been written. For example:
WRONG
parcel.writeInt(...)
parcel.writeString()
parcel.writeString()
...
parcel.readString()
parcel.readInt()
parcel.readInt()
CORRECT
parcel.writeString(...)
parcel.writeInt()
parcel.writeInt()
...
parcel.readString()
parcel.readInt()
parcel.readInt()
28 Mart 2016 Pazartesi
android adapter reuse view problem
List Vİew reusing view when ... I dont want it to..
,Handling ListView Recycle on Android
just adding following codes to your adapter so get rid of the damn it :-)
@Override
public int getItemViewType(int position) {
// Define a way to determine which layout to use, here it's just evens and odds.
return position ;
}
@Override
public int getViewTypeCount() {
return getCount();
}
1 Mart 2016 Salı
Android No view found for id, for fragment çözüldü java.lang.IllegalArgumentException:
Muhmetemelen child fragment dan fragment çağırıyosunuz bu genelde de tabhost kullanırken başka frame layout kullanılır onun içindeki child fragmentden başka fragment geçmek isterkende activity üzerinde ki frame layout yani diger frame loyout referans gösteririz bu durumda da bu hata (No view found for id, for fragment) ile karşılaşılır
Çözüm
Çözüm
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
FragmentTransaction ft = getFragmentManager().beginTransaction();
yerine
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
FragmentTransaction ft =
getActivity().getSupportFragmentManager().beginTransaction();
kullanmanız gerekir. :))
getActivity() ile referansınızın yolunu belirtmiş olursunuz...
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>
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;
--------------------------------------------------------------------------------------------------------------------------
<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
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
Kaydol:
Kayıtlar (Atom)