Add icon(logo) in app toolbar - empty activity - java

I used these lines of code in Java:
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.logo);
But my logo looks like this:
LOGO:
How can I set width of this logo or so it actually looks like a logo? :)

You can add image in toolbar and arrange in the center accordingly.
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/logo"/>
</android.support.v7.widget.Toolbar>

Related

How to make scrollable overlay

I am trying to achieve something like the image below. The goal is to have an overlay with an editText that that can scroll and hide the content under it. I'm not sure how to do this with Java or XML. The image on the left is the desired initial screen and the second screen is when the scrollView overlays the content, and a header appears.
You might achieve that with a Collapsing Toolbar
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="top"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
android:src="#drawable/appbar_image" />
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>

Android Application - Center Title

I am currently trying to center my Title in a new Android application. This is the basic title (top of the screen) that android has assigned to the app. The application is a very simple cookie-cutter "Tabbed" application created in Android Studio using the "Tabbed" template.
The title is simple text (about 30 characters). The title shows up in the main.xml when I select the "Design" option, but it is impossible for me to edit. Any help is appreciated.
Below is a copy of my main.xml:
<menu 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"
tools:context="com.my.app.MainActivity">
<item
android:id="#+id/action_settings"
android:layout_width="match_parent"
android:orderInCategory="100"
android:layout_gravity="center"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
You can create custom ToolBar as shown below
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/action_bar_bkgnd"
app:theme="#style/ToolBarTheme" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Title"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
You can set text of the TextView like below
TextView tv = (TextView) findViewById(R.id.toolbar_title);
tv.setText("Your Text");
You can use setTitle("my title") in activity. However there is no method to set it in center as it's not recommendend. But if you still want it then you can just pad with spaces. Like (" my title")
After playing around with the code, I figured out how to center the Toolbar Title & change the Toolbar Title text.
1st you have to edit the "onCreate" for the activity to turn off the default title provide by android. See sample below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
2nd you have to delete any references in the Toolbar XML (app:title = "your title" )for the app title. A TextView will then need to be added to the Toolbar XML. Below is a sample of the code that is working for me:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#67ccb2"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="22dp"
android:text="MY APP NAME"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
Notice in the TextView above that I have recreated the title, changed its text size, made it bold & centered it in the middle (where it looks awesome).
I hope this helps someone :)

Custom layout in Toolbar

Recently I migrated from the old ActionBar to the new Toolbar.
I have a custom activity (actually a fragment inside my main activity) in which I'm allowing user to click on an icon in the toolbar, and then a search box appears (in the toolbar), using custom layout.
Problem is, it worked fine before the migration, but now once I click the button and switch to the custom action bar layout with the search box, the layout spans on all screen, instead of the normal size of the toolbar.
Here's the code I'm using to replace the toolbar:
LayoutInflater inflator = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View actionBarView = inflator.inflate(R.layout.map_action_layout, null);
actionBar = ((DrawerActivity)getActivity()).getSupportActionBar();
actionBar.setCustomView(actionBarView);
Here's the map action layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
android:id="#+id/autoComplete"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:hint="#string/search_adress_hint"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete|textAutoCorrect"
android:paddingRight="50dp"
android:textColor="#android:color/white"
android:textCursorDrawable="#null" >
<requestFocus />
</AutoCompleteTextView>
<ImageButton
android:id="#+id/clear_address"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="17dp"
android:background="#drawable/ic_action_cancel"
android:gravity="center_horizontal|center_vertical"
android:scaleType="fitCenter" />
</RelativeLayout>
Note: no matter what value I put in the height of the above, whether it's fixed dp value or wrap_content, the layout still spans on whole screen.
Here's my toolbar layout being replaced:
<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/ColorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways"
android:elevation="4dp"
app:layout_collapseMode="pin">
</android.support.v7.widget.Toolbar>
The toolbar resides inside an AppBarLayout.
Does anyone have an idea what am I doing wrong? How can I achieve the result I'm looking for? Thanks.
It is standard to have the toolbar height set to ?attr/actionBarSize.
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />

How to center a button at the bottom of a FrameLayout in a RelativeLayout

This is my xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/nero"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:layout_width="130dp"
android:layout_height="130dp" />
....
</RelativeLayout>
<FrameLayout
android:id="#+id/imprint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal">
<Button
android:layout_width="230dp"
android:layout_height="230dp" />
</FrameLayout>
</LinearLayout>
I want to place the FrameLayout at the bottom of the LinearLayout and all elements in the FrameLayout should be centered. How can I do that?
In Frame Layout Use Layout_Gravity will work
android:layout_gravity="bottom|center"
Also Use Bottom margin to make it in perfect location from bottom.
android:layout_marginBottom="20dp"
To center the button inside the FrameLayout you should add:
android:layout_gravity="center"
inside the Button or:
android:gravity="center"
inside the FrameLayout. And btw, if a Button is the only child why are you using a FrameLayout?
I don't know exactly if that would solve the issue, but you can use "wrap_content" as your layout_width in your FrameLayout at the bottom. That would center the FrameLayout, so the views which are in the layout would get centered.

How can I have a toolbar stay at top of screen instead of showing up on the whole screen?

This could be a problem with the way I'm handling layouts. Sort of new to Java and Android SDK. I'm using Android Studio.
My goal at this point is to have an app going which has the screen showing a navigation bar at the bottom with an icon, and a toolbar at the top with an "OFF" button and a "Save" button. Between the toolbar and the navigation bar should be a blank, white screen.
But I'm having trouble getting the toolbar to just stay at the top of the screen. The grey background takes up the entire screen in front of what was the white background. Here's a screenshot.
And here's what I have:
<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=".ContactActivity">
<RelativeLayout
android:id="#+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/toolbar_background">
<ToggleButton
android:id="#+id/toggleButtonEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentleft="true"
android:layout_marginLeft="20dp"
android:text="#string/toggle_button" />
<Button
android:id="#+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:text="Save" />
<RelativeLayout
android:id="#+id/navbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#color/navbar_background"
android:gravity="center_horizontal">
<ImageButton
android:id="#+id/imageButtonList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:contentDescription="Contact List Button"
android:src="#drawable/contactlisticon" />
<ImageButton
android:id="#+id/imageButtonMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRight0f="#id/imageButtonList"
android:contentDescription="Contact Map Button"
android:src="#drawable/mapicon" />
<ImageButton
android:id="#+id/imageButtonSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:contentDescription="Settings Button"
android:layout_toRight0f="#id/imageButtonMap"
android:src="drawable/settingsicon" />
</RelativeLayout>
You are kind of missing the point of a Toolbar. read about it at Toolbar preview. You on the other end are using RelativeLayout as a Toolbar which is wasteful.
to have a Bottom toolbar you can use another standalone toolbar (as described in the link) or use a LinearLayout like this:
<LinearLayout
android:id="#+id/check_in_bottom_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
...
</LinearLayout>
This is assuming that your root Layout is a RelativeLayout

Categories

Resources