I want to change the action bar background color to a custom color. This is what I currently get:
How do I get this color on the full action bar width?
My action bar xml:
<?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="60dp"
android:background="#0c555c"
android:gravity="center"
android:id="#+id/action_bar_title"
android:paddingEnd="0dp"
android:paddingStart="0dp"
android:showAsAction="ifRoom">
</RelativeLayout>
onCreate method:
RelativeLayout actionBar = (RelativeLayout)findViewById(R.id.action_bar_title);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);
If you are designing your custom actionBar than write your code like this:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8B58A3")));
LayoutInflater inflater = LayoutInflater.from(this);
#SuppressLint("InflateParams")
View v = inflater.inflate(R.layout.titleview, null);
actionBar.setCustomView(v);
Hope it helps !
please add this line at the end .
Toolbar parent =(Toolbar) actionBar.getParent();
parent.setContentInsetsAbsolute(0,0);
Related
I tried all the solutions but still unable to show icon on toolbar, in my main project I have a toolbar and under it toolbar and inside toolbar, fragments, no matter what I tried I'm unable to show icon on toolbar, here is mainActivity.java which extends AppCompatActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_main_screen);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
ActionBar ab = getSupportActionBar();
ab.setTitle("Title");
tabs = (TabLayout) findViewById(R.id.tabs);
tabs.addTab(tabs.newTab().setText("Today"),true);
tabs.addTab(tabs.newTab().setText("Settings"));
tabs.setOnTabSelectedListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="address">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="always"
//android:showAsAction="always" tried this as well
android:icon="#drawable/share"/>
mainactivity.xml
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/toolbarColor"
android:elevation="3dp">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
/>
Nothing is working, not sure abou how to implement custom namespace but I changed name from app to custom but didn't work also there is enough space but still doesn't show.
I always do this for toolbar's title and icon :
Because I don't have access to a computer right now , I cant post a complete sample but I'll describe :
First set your toolbar height like this : layout_height="android:?attr/actionBarSize" then in the android.support.v7.widget.Toolbar tag put a LinearLayout with these attributes : android:layout_width="match_parent and android:layout_height="match_parent" and android:gravity="center_vertical|left" then put an ImageView in the LinearLayout with these attributes:
width="wrap_content"
height="match_parent"
scaleType="fitXY"
adjustviewbounds="true"
src="#drawable/icon"
if you want to add a title just add a text view after adding the ImageView.
On your activity class you need this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
Try to use both
android:showAsAction="always"
app:showAsAction="always"
Ignore the warning that you should use app:showAsAction
Does anyone know how to programmatically darken a View's background like alertDialog or NavigationDrawer doing it? I need the background overlaps the ToolBar (1) and StatusBar, but the icons for the StatusBar should be visible(2). In my case it should not be a dialog. For example, I had to darken the background around the Button or CustomView.
Example Image
I was looking for the answer to this question for a long time, but finally I found a solution.
I researched code "DialogPlus" library and find out what is the key point.
orhanobut/dialogplus
getWindow().getDecorView();
final LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View background = inflater.inflate(R.layout.frame, null);
final Animation a = AnimationUtils.loadAnimation(this, R.anim.in);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewGroup decorView = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
decorView.addView(background);
background.startAnimation(a);
}
});
background resoource file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:alpha="0.5"></FrameLayout>
</LinearLayout>
I have been experimenting the following issue: I have created a custom action bar:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/com_facebook_blue" >
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_centerInParent="true"
android:textSize="30dp"
android:textColor="#ffffff"
android:text="#string/app_title"/>
</RelativeLayout>
Which is inflated by a GraphicsBuilder class in the following way:
public static void setupActionBar(String title, Activity a) {
final ViewGroup actionBarLayout = (ViewGroup) a.getLayoutInflater()
.inflate(R.layout.action_bar_layout, null);
final ActionBar actionBar = a.getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
final int actionBarColor = a.getResources().getColor(R.color.main_red);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));
TextView tv = (TextView) actionBarLayout.findViewById(R.id.action_bar_title);
Typeface font = Typeface.createFromAsset(a.getAssets(), "fonts/Molle-Regular.ttf");
tv.setText(title);
tv.setTypeface(font);
}
Which leads to the following results (I have kept the title bar background blue to highlight the issue):
Basically it looks like that the custom action bar does not fill the parent width, therefore any try to align the title at the center is useless. How can I fix this?
I know this is a long time gone.
Solved it by changing the layout params once it's been set as custom view.
My logic was that when you inflate it the first time, it assumes the size of the original container.
When you call:
actionBar.setCustomView(titlebar);
It's already got its dimensions preset.
My solution was:
View titlebar = inflater.inflate(R.layout.titlebar, null);
// Do stuff to Title Bar //
ActionBar actionBar = getActionBar();
actionBar.setCustomView(titlebar);
View v = actionBar.getCustomView();
LayoutParams lp = v.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
v.setLayoutParams(lp);
Alternatively, you can first set the action bar layout without the inflater:
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.id.titlebar);
View v = actionBar.getCustomView();
// Do something to the view E.g Set button listeners blah //
// Rest of code
I have tried so many tutorials, but I could not find article that would tell me how to create spinner in action bar (sherlock version), but on the right side.
Is there a way to do it? Do I need to create additional views and adapters? I just want to know easy way to create that spinner on the right side, nothing else, just it.
You'll need to create a custom layout for the view containing the spinner. Inflate and place it on the action bar and you're good to go.
Here you have some sample code for this(this is what you do inside your activity to initialize and place your layout on the action bar):
LayoutInflater inflater = (LayoutInflater) getSupportActionBar().getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View spinnerView = inflater.inflate(R.layout.layout_spinner, null);
Spinner spinner = (Spinner) spinnerView.findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.spinner_items_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do whatever you want with your selected item. You can get it as: parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
getSupportActionBar().setIcon(getResources().getDrawable(R.drawable.ic_actionbar_logo));//set your actionbar logo
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE );
LayoutParams layoutParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.RIGHT; // set your layout's gravity to 'right'
getSupportActionBar().setCustomView(spinnerView, layoutParams); //place your layout on the actionbar
Your layout should look something like this (layout_spinner.xml):
<?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" >
<Spinner
style="#style/Widget.Sherlock.Light.Spinner.DropDown.ActionBar"
android:id="#+id/my_spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right" />
</LinearLayout>
Your array stored in a res folder (spinner_items_array):
<string-array name="spinner_items_array">
<item>Item1</item>
<item>Item2</item>
</string-array>
The spinner custom item (spinner_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cab_spinner_item"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textAlignment="inherit"
android:textColor="#android:color/white" /> <!-- Set whatever color you want for the text -->
And finally the drop-down list item (spinner_dropdown_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="48dp"
android:ellipsize="marquee"
android:singleLine="true"
android:textAlignment="inherit"
android:textColor="#android:color/white" />
I hope this answer will help you!!
Good luck!
In my main activity I'm trying to access a LinearLayout I have within my main ScrollView. My ScrollView is implemented as follows:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/graph"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
/>
</ScrollView>
In my Main activity I inflate the scrollView and then try to access the linear layout as follows:
LayoutInflater inflater;
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ScrollView mainLayout = (ScrollView) inflater.inflate(R.layout.scrollview,
null);
LinearLayout layout = (LinearLayout) findViewById(R.id.graph);
Why would my findViewById be returning null here? I thought at first it was trying to find it before it had finished inflating, but I've tried having a while loop to wait for it to finish inflating, and this doesn't help either.
you have to give it the inflated layout
LinearLayout layout = (LinearLayout) mainLayout.findViewById(R.id.graph);
You can double check if specify the wrong layout in inflater.inflate() first.