I have two different styles of TabLayout in my app:
<style name="TabLayoutPower" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
<style name="TabLayoutFree" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/black</item>
<item name="tabTextColor">#android:color/black</item>
</style>
How can I define the default TabLayout style for a theme? I cannot find any info which item name should I use to build my theme. I'd like to add the TabLayout the same way I added my listview:
<style name="Free" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/black</item>
<item name="colorPrimaryDark">#color/black</item>
<item name="colorAccent">#color/main_red</item>
<item name="android:windowBackground">#color/main_bg_light</item>
<item name="android:listViewStyle">#style/MyListView</item>
</style>
For Support Library TabLayout, you can set tabStyle attribute in your theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ... -->
<item name="tabStyle">#style/AppTheme.TabLayout</item>
</style>
<style name="AppTheme.TabLayout" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
If you wan to use 2 different styles for your TabLayout based on theme of your application then you should define your style inside attrs.xml
here is the Sample code for that
First you create an attrs.xml file ( Like this )
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="textColor" format="reference|color" />
<attr name="backColor" format="reference|color" />
<attr name="myTabStyle" format="reference" />
</resources>
define various theme as per your requirements inside styles.xml
Note: i have used 3 different themes for demo
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="tabStyle">#style/TabLayoutPower</item>
<item name="textColor">#android:color/white</item>
<item name="backColor">#color/colorPrimary</item>
</style>
<style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#android:color/holo_green_dark</item>
<item name="colorPrimaryDark">#android:color/white</item>
<item name="colorAccent">#android:color/holo_blue_dark</item>
<item name="textColor">#android:color/white</item>
<item name="tabStyle">#style/TabLayoutFree</item>
<item name="backColor">#FF00</item>
</style>
<style name="AppTheme3" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#ff00</item>
<item name="colorPrimaryDark">#ff0</item>
<item name="colorAccent">#0a91d4</item>
<item name="tabStyle">#style/TabLayoutNew</item>
<item name="textColor">#FF00</item>
<item name="backColor">#android:color/white</item>
</style>
<style name="TabLayoutPower" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
<style name="TabLayoutFree" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/holo_blue_bright</item>
<item name="tabTextColor">#android:color/holo_blue_bright</item>
</style>
<style name="TabLayoutNew" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">#android:color/holo_green_dark</item>
<item name="tabTextColor">#android:color/holo_green_dark</item>
</style>
</resources>
Now use this style in your TabLayout like this inside layout.xml files
Use like this style="?myTabStyle" it will select style based on current theme of your application
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnThemeOne"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Theme One" />
<Button
android:id="#+id/btnThemeTwo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Theme Two" />
<Button
android:id="#+id/btnThemeThree"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Theme Three" />
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="?myTabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
android:background="#color/colorAccent"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
sample code of TabsActivity to change theme
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class TabsActivity extends AppCompatActivity {
PrefManager prefManager;
private TabLayout tabLayout;
private ViewPager viewPager;
private Button btnThemeOne, btnThemeTwo, btnThemeThree;
#Override
protected void onCreate(Bundle savedInstanceState) {
prefManager = new PrefManager(this);
getTheme().applyStyle(prefManager.getTheme(), true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs);
btnThemeOne = findViewById(R.id.btnThemeOne);
btnThemeOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(TabsActivity.this, "Applying theme one", Toast.LENGTH_SHORT).show();
prefManager.setTheme(R.style.AppTheme);
recreate();
}
});
btnThemeTwo = findViewById(R.id.btnThemeTwo);
btnThemeTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(TabsActivity.this, "Applying theme two", Toast.LENGTH_SHORT).show();
prefManager.setTheme(R.style.AppTheme2);
recreate();
}
});
btnThemeThree = findViewById(R.id.btnThemeThree);
btnThemeThree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(TabsActivity.this, "Applying theme three", Toast.LENGTH_SHORT).show();
prefManager.setTheme(R.style.AppTheme3);
recreate();
}
});
viewPager = findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new OneFragment(), "TWO");
adapter.addFragment(new OneFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
PrefManager class to save theme info
import android.content.Context;
import android.content.SharedPreferences;
public class PrefManager {
// Shared preferences file name
private final String PREF_NAME = "theme-pref";
private final String THEME = "theme";
SharedPreferences pref;
SharedPreferences.Editor editor;
public PrefManager(Context context) {
pref = context.getSharedPreferences(PREF_NAME, 0);
editor = pref.edit();
}
public int getTheme() {
return pref.getInt(THEME, R.style.AppTheme);
}
public void setTheme(int themeId) {
editor.putInt(THEME, themeId);
editor.commit();
}
}
you can check the output video of above code
https://www.youtube.com/watch?v=uup072IDGd0
You can find that name in Android theme -
<item name="android:tabWidgetStyle">#style/Your.Style</item>
<style name="Base.Widget.Design.TabLayout" parent="">
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextColor">#android:color/white</item>
</style>
<style name="Free" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/black</item>
<item name="colorPrimaryDark">#color/black</item>
<item name="colorAccent">#color/main_red</item>
<item name="android:windowBackground">#color/main_bg_light</item>
<item name="android:listViewStyle">#style/MyListView</item>
<item name="android:tabWidgetStyle">#style/Base.Widget.Design.TabLayout</item>
</style>
Related
In the log, I have
NullPointerException : Attempt to invoke virtual method 'void
android.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null
object reference.
Here is my following code :
public class MatchesActivity extends Activity implements ActionBar.OnNavigationListener {
private ActionBar actionBar;
private ArrayList<SpinnerNavItem> navSpinner;
private TitleNavigationAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_matches);
actionBar=getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
navSpinner = new ArrayList<SpinnerNavItem>();
navSpinner.add(new SpinnerNavItem("Botola Pro",R.drawable.ic_menu_camera));
navSpinner.add(new SpinnerNavItem("Coupe du trone",R.drawable.ic_menu_camera));
adapter = new TitleNavigationAdapter(getApplicationContext(),navSpinner);
actionBar.setListNavigationCallbacks(adapter,this);
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId){
return false;
}
}
here is my styles.xml and my stylesv21.xml :
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
<resources>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
To get you started, here's some pointers. Toolbar is the new ActionBar. You can define any custom Toolbar layout you want from XML.
For a Spinner, for example, save this as toolbar_spinner.xml.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark"
>
<Spinner
android:id="#+id/spinner_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
Now, you can simply include that Toolbar layout into your Activity. (Make sure that Activity uses a Theme without a Toolbar like Theme.AppCompat.NoActionBar)
<?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:orientation="vertical" >
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_spinner" />
<!-- Activity content here -->
</LinearLayout>
Then, you can start off your Activity class like so
public class SpinToolbarActivity extends AppCompatActivity {
private Toolbar mToolbar;
private Spinner mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spintoolbar);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mSpinner = (Spinner) findViewById(R.id.spinner_nav);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
// TODO: add items to mSpinner using an Adapter
}
}
I'm trying to implement Guillotine Menu by yalantis in my app.
I've put one button in the guillotine activity and have made a class for the guillotine activity. In that class I'm trying to implement the listener for the button .I've also a put a toast in that function but that's not getting displayed either.
Here is the guillotine activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/guillotine_background"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/guillotine_background"
android:layout_margin = "0dp"
android:divider="#666"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
style="#style/Toolbar"
app:contentInsetStart="0dp">
<ImageView
android:id="#+id/guillotine_hamburger"
style="#style/ImageView.Hamburger"
android:src="#drawable/ic_menu" />
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="#+id/profile_group"
style="#style/LinearLayout.GuillotineItem"
android:layout_marginTop="#dimen/guillotine_medium_margin">
<ImageView
style="#style/ImageView.GuillotineItem"
android:src="#drawable/ic_profile" />
<Button
style="#style/TextView.GuillotineItem"
android:text="MAIN PAGE"
android:background="#color/guillotine_background"
android:id="#+id/b1"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/feed_group"
style="#style/LinearLayout.GuillotineItem">
<ImageView
style="#style/ImageView.GuillotineItem"
android:src="#drawable/ic_feed" />
<TextView
style="#style/TextView.GuillotineItem"
android:text="#string/feed" />
</LinearLayout>
<LinearLayout
android:id="#+id/activity_group"
style="#style/LinearLayout.GuillotineItem">
<ImageView
style="#style/ImageView.GuillotineItem"
android:src="#drawable/ic_activity_active" />
<TextView
style="#style/TextView.GuillotineItem.Selected"
android:text="#string/activity" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/guillotine_divider_height" />
<LinearLayout
android:id="#+id/settings_group"
style="#style/LinearLayout.GuillotineItem">
<ImageView
style="#style/ImageView.GuillotineItem"
android:src="#drawable/ic_settings" />
<TextView
style="#style/TextView.GuillotineItem"
android:text="#string/settings" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
And here is the class:
package manipal.freshers;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;
import java.io.File;
/**
* Created by Ankit Vimal on 28-02-2016.
*/
public class Guillotine extends AppCompatActivity {
private Button b1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guillotine);
b1=(Button)findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), "a", Toast.LENGTH_LONG).show();
Intent i = new Intent(Guillotine.this, MIT_Sports_Club_Activity.class);
startActivity(i);
}
}
);
}
}
Styles.xml:
<resources>
<style name="LinearLayout" />
<style name="LinearLayout.GuillotineItem">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">horizontal</item>
<item name="android:layout_marginTop">#dimen/guillotine_small_margin</item>
</style>
<style name="TextView" />
<style name="TextView.ToolbarTitle">
<item name="android:gravity">center</item>
<item name="android:textSize">24sp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="TextView.GuillotineItem">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">#dimen/guillotine_small_margin</item>
<item name="android:textSize">#dimen/guillotine_item_text_size</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:textColor">#android:color/white</item>
</style>
<style name="TextView.GuillotineItem.Selected">
<item name="android:textColor">#color/selected_item_color</item>
</style>
<style name="Toolbar">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="android:minHeight">?attr/actionBarSize</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">#color/guillotine_background</item>
</style>
<style name="ImageView" />
<style name="ImageView.ContentItem">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
</style>
<style name="ImageView.Hamburger" parent="Widget.AppCompat.ActionButton">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_marginBottom">#dimen/abc_action_bar_overflow_padding_start_material</item>
</style>
<style name="ImageView.GuillotineItem">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_marginLeft">#dimen/guillotine_large_margin</item>
</style>
</resources>
Themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.Guillotine"/>
<style name="Theme.Guillotine" parent="Theme.AppCompat.NoActionBar">
<item name="android:color">#color/guillotine_background</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorPrimaryDark">#color/guillotine_background</item>
<item name="android:colorPrimary">#color/guillotine_background_dark</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
</style>
</resources>
MainActivity.java:
package manipal.freshers;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.yalantis.guillotine.animation.GuillotineAnimation;
public class MainActivity extends AppCompatActivity {
private static final long RIPPLE_DURATION = 250;
#InjectView(R.id.toolbar)
Toolbar toolbar;
#InjectView(R.id.root)
FrameLayout root;
#InjectView(R.id.content_hamburger)
View contentHamburger;
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private TextView t;
int []imageArray={R.drawable.manipal1,R.drawable.manipal2,R.drawable.manipal3,R.drawable.manipal4,R.drawable.manipal5};
private ImageView i1;
private final Handler handler = new Handler();
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
i1=(ImageView) findViewById(R.id.imageView1);
t = (TextView) findViewById(R.id.textView3);
t.setMovementMethod(new ScrollingMovementMethod());
if(toolbar!= null){
if (android.os.Build.VERSION.SDK_INT >= 11){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
}}
View guillotineMenu = LayoutInflater.from(this).inflate(R.layout.guillotine, null);
root.addView(guillotineMenu);
new GuillotineAnimation.GuillotineBuilder(guillotineMenu, guillotineMenu.findViewById(R.id.guillotine_hamburger), contentHamburger)
.setStartDelay(RIPPLE_DURATION)
.setActionBarViewForAnimation(toolbar)
.setClosedOnStart(true)
.build();
Runnable runnable = new Runnable() {
int i=0;
public void run()
{
i1.setImageResource(imageArray[i]);
i++;
if(i>imageArray.length-1)
{
i=0;
}
handler.postDelayed(this, 3000); //for interval...
}
};
handler.postDelayed(runnable,0);
}
}
package com.example.vinaymaneti.button;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
}
});
}
}
I've read over a fair few other threads regarding this issue, but have not been able to work out my problem. On Android 5.x my app works fine. But when testing on Android 4.4 Kit Kat I have an issue.
Problem:
- My toolbar/action bar is functional, and still works however the activity name text and the hamburger menu item does not display. I have attached images below to demonstrate the problem.
I have a main activity, with a nav bar and each item on the nav bar loads a new fragment. I am using the below setup for fragment definition.
public class <ClassName> extends android.support.v4.app.Fragment {}
I have attached my 2 x styles XML files & my main fragment file, but i'm not sure if that is where the problem lies. I have also tried removing all content in the layout on the affected screen but that hasn't helped either. This is my first time trying to get layout issues fixed across older version of Android so i'm a tad lost.
Do I need to create a new toolbar object?
values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#color/whiteSystem</item>
<item name="tabTextColor">#android:color/white</item>
<item name="tabSelectedTextColor">#android:color/white</item>
</style>
</resources>
v21/styles.xml
<resources>
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#color/whiteSystem</item>
<item name="tabTextColor">#android:color/white</item>
<item name="tabSelectedTextColor">#android:color/white</item>
</style>
</resources>
Fragment class which is having the issue
import android.app.Activity;
//import android.app.Fragment;
//import android.app.FragmentManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ParentFragment extends Fragment {
TabLayout tabLayout;
ViewPager viewPager;
private View myFragmentView; //this will enable 'findviewbyid' to be accessed
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.parentFragment, container, false);
viewPager = (ViewPager) myFragmentView.findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdaptor(getChildFragmentManager(), getActivity()));
tabLayout = (TabLayout) myFragmentView.findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
});
return myFragmentView;
}
private class CustomAdaptor extends FragmentPagerAdapter {
private String fragments[] = {"1", "2", "3", "4"};
public CustomAdaptor(FragmentManager fragmentManager, Activity activity) {
super(fragmentManager);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
case 3:
return new FragmentFour();
default:
return null;
}
}
#Override
public int getCount() {
return fragments.length;
}
#Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
}
I am using an androidhive tutorial to design my tabs since the tabhost has been deprecated. It is my first time using the new features. However, I keep getting this error:
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
I have read solutions to similar problems here and the proposed solution is to set some properties in the theme however everything looks good in my styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item> <!--change here-->
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="button_text" >
<item name="android:layout_width" >fill_parent</item>
<item name="android:layout_height" >wrap_content</item>
<item name="android:textColor" >#ffffff</item>
<item name="android:gravity" >center</item>
<item name="android:layout_margin" >3dp</item>
<item name="android:textSize" >30sp</item>
<item name="android:textStyle" >bold</item>
<item name="android:shadowColor" >#000000</item>
<item name="android:shadowDx" >1</item>
<item name="android:shadowDy" >1</item>
<item name="android:shadowRadius" >2</item>
</style>
</resources>
Below is a section of my manifest file:
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar" >
</activity>
<activity android:name=".MaintenanceActivity" >
</activity>
<activity android:name=".ServicingActivity">
</activity>
Here is the activity_maintenance.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.application.sweetiean.stlmaintenance.MaintenanceActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
and finally, here is the MaintenanceActivity.java
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class MaintenanceActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maintenance);
init();
}
public void init(){
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OverviewFragment(), "Overview");
adapter.addFragment(new BaseDataFragment(), "Base Data");
adapter.addFragment(new TaskFragment(), "Task");
adapter.addFragment(new ImageSignFragment(), "Images/Sign");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I cannot see what is going on wrong in my code because I am not receiving the same error when i start up the main activity so I believe it has something to do with the tab implementation. I would be grateful if anyone can help me out. Thank you.
Error about wrong theming: you are trying to set Actionbar, while it already exists. So:
<activity android:name=".MaintenanceActivity" >
</activity>
You set no theme for this activity. Just add:
android:theme="#style/AppTheme.NoActionBar"
Like you do to main activity. GL
You need to change your AppTheme style from:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to :
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
to discard the already-existing actionBar.
http://tinypic.com/r/2isk4tz/8
This link will provide the image for the viewpager indicator lined style.
My question is how do I get rid of the default white background and amke it transparent?
For example, to look like this : http://24.media.tumblr.com/d0816263966dcb14f4b6a655f1f6bbc0/tumblr_mwzn1tKAAU1r2wjwko1_1280.png
Here is my code by the way.
Themed Lines Code Layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.viewpagerindicator.LinePageIndicator
android:id="#+id/indicator"
style="#style/CustomLinePageIndicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.04"
android:padding="5dip"
app:lineWidth="30dp"
app:selectedColor="#FF24248F"
app:strokeWidth="4dp"
app:unselectedColor="#FF8FB28F"
/>
</LinearLayout>
Main Activity Code:
import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.LinePageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import co.sav.prime.R;
public class MainActivity extends FragmentActivity {
FragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
int Number = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.themed_lines);
mAdapter = new FragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (LinePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
#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;
}
Styles:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="StyledIndicators" parent="#android:style/Theme.Light">
<item name="vpiCirclePageIndicatorStyle">#style/CustomCirclePageIndicator</item>
<item name="vpiLinePageIndicatorStyle">#style/CustomLinePageIndicator</item>
<item name="vpiTitlePageIndicatorStyle">#style/CustomTitlePageIndicator</item>
<item name="vpiTabPageIndicatorStyle">#style/CustomTabPageIndicator</item>
<item name="vpiUnderlinePageIndicatorStyle">#style/CustomUnderlinePageIndicator</item>
</style>
<style name="CustomTitlePageIndicator">
<item name="android:background">#18FF0000</item>
<item name="footerColor">#FFAA2222</item>
<item name="footerLineHeight">1dp</item>
<item name="footerIndicatorHeight">3dp</item>
<item name="footerIndicatorStyle">underline</item>
<item name="android:textColor">#AA000000</item>
<item name="selectedColor">#FF000000</item>
<item name="selectedBold">true</item>
</style>
<style name="CustomLinePageIndicator">
<item name="strokeWidth">4dp</item>
<item name="lineWidth">30dp</item>
<item name="unselectedColor">#FF8FB28F</item>
<item name="selectedColor">#FF24248F</item>
<item name="android:background">#android:color/transparent</item>
</style>
<style name="CustomCirclePageIndicator">
<item name="fillColor">#FF888888</item>
<item name="strokeColor">#FF000000</item>
<item name="strokeWidth">2dp</item>
<item name="radius">10dp</item>
<item name="centered">true</item>
</style>
<style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator">
<item name="android:textAppearance">#style/CustomTabPageIndicator.Text</item>
<item name="android:textColor">#FF555555</item>
<item name="android:textSize">16sp</item>
<item name="android:dividerPadding">10dp</item>
<item name="android:showDividers">middle</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">8dp</item>
<item name="android:fadingEdge">horizontal</item>
<item name="android:fadingEdgeLength">8dp</item>
</style>
<style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
<item name="android:typeface">monospace</item>
</style>
<style name="CustomUnderlinePageIndicator">
<item name="selectedColor">#FFCC0000</item>
<item name="android:background">#FFCCCCCC</item>
<item name="fadeLength">1000</item>
<item name="fadeDelay">1000</item>
</style>
</resources>
I think I got. You're using LinearLayout, your ViewPager is above your PagerIndicator, so the white color is the background color of the view. You can solve this using RelativeLayout and put:
android:layout_alignParentBottom="true"
in your PagerIndicator.
Try something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<com.viewpagerindicator.LinePageIndicator
android:id="#+id/indicator"
style="#style/CustomLinePageIndicator"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:padding="5dip"
app:lineWidth="30dp"
app:selectedColor="#FF24248F"
app:strokeWidth="4dp"
app:unselectedColor="#FF8FB28F"
/>
</RelativeLayout>