I try set gradient for background actionbar but background not match parent actionbar
Here is code gradient_actionbar
<gradient
android:angle="360"
android:centerColor="@color/color2"
android:endColor="@color/color3"
android:startColor="@color/color1"
android:type="linear"
/>
<corners
android:radius="0dp"/>
Here is code actionbar_layout
<?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:background="@drawable/gradient_actionbar"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Test"
android:textColor="#ffffff"
android:id="@+id/mytext"
android:textSize="18sp" />
</LinearLayout>
Here is code MainActivity.java
public class MainActivity extends AppCompatActivity {
DrawerLayout drawer;
private TabLayout tabLayout;
private ViewPager viewPager;
Toolbar toolbar;
TextView tvName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
tvName = (TextView) toolbar.findViewById(R.id.toolbar_title);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
viewPager = (ViewPager)findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
toolbar = (Toolbar) findViewById(R.id.toolbar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
}
Create a GradientDrawable in res/drawable/mygradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false" >
<gradient
android:angle="360"
android:centerColor="@color/color2"
android:endColor="@color/color3"
android:startColor="@color/color1"
android:type="linear" />
</shape>
Then set it to action bar in your Activity's onCreate():
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.mygradient));
For support v7 library (your case):
// make sure to import android.support.v7.app.ActionBar
ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.mygradient));