Action bar Transparent in pre Lollipop devices - java

So I'm done with my app and doing final tests. On Android 5.0+ it runs fine, but on pre Lollipop devices i get strange bug.
At first, action bar was not visible on activities with ScrollView and I fixed it by putting ScrollView inside of RelativeLayout. But that solves only half of the problem. Action bar color is transparent and I can tell that because background color in my activities is #f2f2f2 and text on Action bar is white.
I see action bar title, back button and all images I put in action bar, but just can't set the color of it to ColorPrimary which is material light blue.
Here's the ACTIVITY code (not content)
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
tools:context="hr.app.liftme.liftmehr.VjezbeTricepsTricepsEkstenzijaKablovima">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:elevation="10dp"
app:elevation="10dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_vjezbe_triceps_triceps_ekstenzija_kablovima" />
</android.support.design.widget.CoordinatorLayout>
And here's java file where i declare
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vjezbe_triceps_triceps_ekstenzija_kablovima);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Can anyone please help me with this?

/** In your xml,color/background ,change according to your Requirement **/
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="55dp"
android:padding="#dimen/appbar_padding"
android:background="#color/appbarColor">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/place_order"
android:layout_gravity="center"
android:textSize="#dimen/appbar_txt_size"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_marginTop="15dp"
android:id="#+id/toolbar_title"/>
</android.support.v7.widget.Toolbar>
/** In your java file **/
toolbar.setBackgroundColor(Color.parseColor("#617191"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getActivity().getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(getActivity().getResources().getColor(R.color.toolbar));
}

Related

How to put custom toolbar inside fragment?

I created custom toolbar inside fragment,but i got view like this:
In themes.xml i disabled ActionBar:
<style name="Theme.Project" parent="Theme.MaterialComponents.DayNight.NoActionBar">
This is my custom toolbar layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/white"
>
<TextView
android:id="#+id/toolbar_title"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Home"
android:fontFamily="#font/google_sans"
android:textColor="#color/black"
android:textSize="20sp" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/fragment_home" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And this is HomeFragment.java:
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.toolbar_in_home, container, false);
return root;
}
}
How to remove this white thing on top of screen?
UPD:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".ui.home.HomeFragment">
</androidx.constraintlayout.widget.ConstraintLayout>
This is fragment home(just ConstraintLayout)
UPD2:i also was trying use toolbar in activity_main and it works:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toolbar);
BottomNavigationView navView = findViewById(R.id.nav_view);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_examination, R.id.navigation_profile)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(navView, navController);
}
But i can't do changes if i want to go on other fragment.
I need to set another custom toolbar layout with button like this:
So this is how I did it. My app is based on single activity-multi-fragment model.
I had set activity theme to AppTheme.NoActionBar, so it enabled me to set a custom toolbar.
Later for each fragment I had a separate layout and within which added a custom toolbar like below
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary">
....
....
</androidx.appcompat.widget.Toolbar>
Within this layout I had added menu items.
If you don't want to follow this, may be you should look at getActionBar().setCustomView(). Keep your activity theme set, so getActionBar() is not null.
You can control action bar layout by calling getActionBar().setCustomView() in fragment onResume() or probably when you are adding or removing a fragment in your activity.
You can use your custom toolbar inside the main layout replace it from com.google.android.material.appbar.AppBarLayout.
Here is an example code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="#color/colorPrimary">
<ImageView
android:id="#+id/navi"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_marginStart="20dp"
android:contentDescription="#string/todo"
android:src="#drawable/navi" />
<ImageView
android:layout_width="180dp"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:contentDescription="#string/todo"
android:src="#drawable/header2" />
</RelativeLayout>
<include layout="#layout/fragment_home" />
</RelativeLayout>

Empty space on top of toolbar. Android, Java

I just included an options menu in my app, but in one of my activities it seems to create a white empty space- can anyone tell me why??
It work in other activities in the same app, but not this single activity.
I Hope anyone can help :-)
public class GifsActivity extends OptionsMenu {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gifs);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ImageView gifImageView = (ImageView) findViewById(R.id.iv_gif);
Glide.with(this)
.load("http://i.imgur.com/Vth6CBz.gif")
.asBitmap()
.placeholder(R.drawable.ic_cloud_off_red)
.error(R.drawable.ic_cloud_off_red)
.into(gifImageView);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center_vertical"
android:orientation="vertical"
tools:context="com.example.examand1.GifsActivity">
<include layout="#layout/toolbar_layout"
/>
<ImageView
android:id="#+id/iv_gif"
android:layout_width="match_parent"
android:layout_height="220dp" />
</RelativeLayout>
Picture of the activity
Remove android:gravity="center_vertical" from your layout. Then your layout will be as below:
<?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">
<include
android:id="#+id/include"
layout="#layout/toolbar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<ImageView
android:layout_below="#id/include"
android:id="#+id/iv_gif"
android:layout_width="match_parent"
android:layout_height="220dp" />
</RelativeLayout>

How do I set the color of the action bar using palette library?

I have a bitmap which I use to display an image from firebase in an imageview. I want both the status bar and the action bar's colors to be adjusted by the vibrant color of the pictures from the database. I searched for how to do this, and have the following code below
Palette.from(myBitmap).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
android.app.ActionBar bar=setBackgrounddrawable(palette.getVibrantColor(getResources().getColor(R.color.colorPrimary)));
getWindow().setStatusBarColor(palette.getVibrantColor(getResources().getColor(R.colorPrimary)));
}
});
But I get an error on the line of code for get action bar saying 'set background drawable cannot be applied to int' When I remove the line of code for setting color of the actionbar only the status bar color is set and not the action bar, how can I set the color of the action bar as well?
As You see, setBackgroundDrawable() receive a param what type is Drawable,
but you pass an Int Color value in, then you got the error.
I do not test it, but You can give it a try with:
setBackgroundDrawable(new ColorDrawable(palette.getVibrantColor(getResources().getColor(R.color.colorPrimary))));
Maybe, it will be easy to use Toolbar?
activity_main.xml:
<?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:id="#+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/mainPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="4dp"
/>
</LinearLayout>
app_bar_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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=".activities.MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="match_parent"
android:src="#drawable/logo_vector_dark"
/>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Setup toolbar in onCreate of Activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Change color:
Palette.from(myBitmap).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
toolbar.setBackgroundColor(palette.getVibrantColor(getResources().getColor(R.color.colorAccent)));
}
});

Headers in Activity

I am very new in Android. I want to put a header on the top of the activity, similar to UINavigationBar in iOS. I need to put an icon on the left side of the header and a title (either text or an image with logo & title). Please find the image link to know how I want the header.
https://www.dropbox.com/s/onsc3klzc0bafia/Screen%20Shot%202016-12-08%20at%201.00.13%20PM.png?dl=0
User this in your 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">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and then in the activity onCreate(); add this
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toolbar);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("YOUR_TITLE");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
This line will add the arrow at the left corner of the toolbar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Text won't start at top with gravity=top

I've been trying to set the title of my toolbar to start at the top of the toolbar, but while the I have set android:gravity="top" the text still start at the center (vertical) of the toolbar. I think this could have something to do with using a custom layout for the toolbar, but I use it because I need it, so I hope someone knows a way to set the text to start at the top.
Here is layout my toolbar is in:
<?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidDomInspection -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.percent.PercentRelativeLayout
android:id="#+id/layoutProjectOverview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:title="#string/menuLabel1a"
app:layout_widthPercent="37%"
app:layout_heightPercent="26%"
android:gravity="top"
app:titleTextAppearance="#style/toolbarTitleText"
android:background="#drawable/toolbar_basic"
android:theme="#style/AppTheme.AppBarOverlay"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.percent.PercentRelativeLayout
android:id="#+id/layoutObjectNav"
app:layout_widthPercent="63%"
app:layout_aspectRatio="400%"
android:layout_toRightOf="#id/toolbar2"
android:layout_toEndOf="#id/toolbar2"
android:background="#drawable/border_basic">
<!-- Some irrelevant xml code -->
</android.support.percent.PercentRelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/layoutObjectNav">
<!-- Some more irrelevant xml code -->
</RelativeLayout>
</android.support.percent.PercentRelativeLayout>
<!-- Some more irrelevant xml code -->
</RelativeLayout>
Here is the custom toolbar layout code:
<?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">
<TextView
android:id="#+id/action_bar_title"
android:textSize="20sp"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="top"
android:padding="5dp"
android:ellipsize="end"
android:maxLines="3"/>
</LinearLayout>
And here is the code where I set the custom layout (and some other stuff):
#Override
protected void onResume() {
super.onResume();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
currentProject = getIntent().getParcelableExtra("Project");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.layout_toolbar);
((TextView) findViewById(R.id.action_bar_title)).setText(currentProject.getTitle());
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = (int)( size.x * 0.37);
int height = Math.round(size.x * 0.63f * 0.25f);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
toolbar.setLayoutParams(params);
} else {
getSupportActionBar().setTitle(currentProject.getTitle());
}
// Some irrelevant java code
}
Does someone know what I'm doing wrong?
EDIT:
Here is the TextAppearance Style I use for the toolbar:
<style name="toolbarTitleText">
<item name="android:textSize">20sp</item>
<item name="android:maxLines">3</item>
</style>
add a textView in your toolbar
like this:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>
Try to do the following :
1- Disable the title by using getSupportActionBar().setDisplayShowTitleEnabled(false);
2- Add a textView under your toolbar as your toolbar title :
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"/>
</android.support.v7.widget.Toolbar>

Categories

Resources