I am trying to split the toolbar so I get a Bottom bar.
I got the Toolbar to work, but cant find how to split it.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FF4444" />
and here is my .java.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
EDIT:
This is what I did, I got 2 toolbars, one in top, one in bottom, but I dont know if it is the right way to go.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FF4444" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarBottom"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentBottom="true"
android:background="#FF4444" />
Related
I restarted to develop now after years so there were many changes.
Now I'm trying to modify the AppBar (Toolbar), for the activity. (I also see the CoordinatorLayout, but i don't know what differences have with the Linear and Relative).
So in the MainActivity.class i have:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView)
toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);
mTitle.setTextColor(Color.parseColor("#ff0000"));
and in activity_main.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=".MainActivity">
<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:popupTheme="#style/AppTheme.PopupOverlay" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
And it works fine, the color and the title (or it seems).
Then i also created two other activity (that i open from a button (i will explain one...they made similar problems):
SecondActivity.class:
public class ActivityMappa extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mappa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarmappa);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbarmappa_title);
setSupportActionBar(toolbar);
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);
mTitle.setTextColor(Color.parseColor("#ff0000"));
toolbar.setBackgroundColor(Color.parseColor("#fafad2"));
}
and activity_second.xml:
<android.support.constraint.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=".ActivityMappa">
<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/toolbarmappa"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_mappa"
android:layout_gravity="center"
android:id="#+id/toolbarmappa_title" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.constraint.ConstraintLayout>
In this second activity, the toolbar color it's right, but the text not change when i run the app (in the android studio preview it was changed), and the text it is in the #string/title_mappa and it exists.
So why the text does not change? The code is the same.
An other thing, when i add stuff in the content_main.xml, the position start under the Toolbar, and if i set the margin/padding, it starts from it, but from the other 2 activity, when i add other stuffs (like imageview), they starts from the app start at the top, above the toolbar, why?
Thank you so much for the help.
Your issue is that you are setting the text of toolbarmappa_title to the title of toolbarmappa but the title of toolbarmappa is null therefore you are setting the text to null. You need to set the text with a predefined string or set the title of toolbarmappa before you get the title.
It would be something like this:
public class ActivityMappa extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mappa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarmappa);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbarmappa_title);
setSupportActionBar(toolbar);
toolbar.setTitle("insert title here");
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);
mTitle.setTextColor(Color.parseColor("#ff0000"));
toolbar.setBackgroundColor(Color.parseColor("#fafad2"));
}
Also, general pratice to change the text of a toolbar is not to add a TextView. You can remove the TextView and call toolbar.setTitle("insert text here"); and that eliminates the need of the TextView.
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);
I have no idea why i am getting this exception.
Here is my code -
toolbar.xml
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
I am including this layout in my xml file -
<include android:id="#+id/photoGalleryToolbar"
layout="#layout/toolbar"/>
And then i am using following code in my activity -
Toolbar toolbar = (Toolbar) findViewById(R.id.photoGalleryToolbar);
setSupportActionBar(toolbar);
final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_keyboard_backspace_white_24dp);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle("Kidster Photo Gallery");
actionBar.setDisplayHomeAsUpEnabled(true);
}
The exception is coming at following line -
Toolbar toolbar = (Toolbar) findViewById(R.id.photoGalleryToolbar);
I had checked the imports and they all are right and i am using support library.
Replace you code :
Toolbar toolbar = (Toolbar) findViewById(R.id.photoGalleryToolbar);
with:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
You made a mistake on the id, R.id.photoGalleryToolbar is the root view of the layout which you include here is the AppBarLayout. The Toolbar's id is R.id.toolbar you defined in the toolbar.xml.
I want to add shadow to ToolBar and I use the following link:
ToolBar Shadow
but running app show me this error in LogCat :
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.v7.widget.Toolbar
Toolbar code :
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="200dp">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="200dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark"
app:theme="#style/MyCustomToolbarTheme"
android:background="#drawable/main_header">
</android.support.v7.widget.Toolbar>
MainActivity XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar_main"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/app_bar"
android:text="ytkbhjk"/>
MainActivity Java :
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
please help me
You are going to need to make one more xml file which contains your toolbar:
Name of this xml is toolbar
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"/>
Now change your relative layout's include tag like this
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And in your activity, do something like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
Where you use the <include /> in your MainActivity XML, you should not put the android:id="#+id/app_bar", but instead in the tag <android.support.v7.widget.Toolbar />.
I have removed relativeLayout instead just use android.support.v7.widget.Toolbar. And ts work.
Try downloading android_m2repository_r29.zip repository from https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/ with instruction given in same.
I was trying to add a shadow to the toolbar following this example over here
https://stackoverflow.com/a/26904102/4273056
My toolbar in activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
This is the xml file where I added the toolbar
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
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="wrap_content"
android:background="#color/primaryColor"
android:paddingTop="#dimen/app_bar_top_padding"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark"
app:theme="#style/MyCustomToolBarTheme">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- **** Place Your Content Here **** -->
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#drawable/shadow" />
</FrameLayout>
</LinearLayout>
I get this in the logcat
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v7.widget.Toolbar
How can I get rid of this error?
Add the id attribute in your Toolbar and check if you have another element with the same id.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
.....
/>
Also it is better:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null)
setSupportActionBar(toolbar);