How to display an image in navigation drawer - java

I am trying to make my first hamburger menu with some images, but they are displayed in grey scale, not the real image. Here is a photo of how it looks:
And here is the code for the XML file:
<item
android:id="#+id/desp_mercedes"
android:icon="#drawable/mercedes"
android:title="Mercedes" />
<item
android:id="#+id/desp_audi"
android:icon="#drawable/audi"
android:title="Audi" />
<item
android:id="#+id/desp_bmw"
android:icon="#drawable/bmw"
android:title="BMW" />
I want to know how to display the original images. I have both SVG and PNG files for better quality.

In your nav_header_main.xml layout resource file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nav_header"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#color/colorAccent"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:gravity="bottom"
android:orientation="vertical"
android:padding="16dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
//here you can add your image src
<ImageView
android:id="#+id/nav_header_imageView"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/ic_imageview" />
<TextView
android:id="#+id/nav_header_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="Chike Mgbemena"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
</LinearLayout>

You can maybe try this out: How do I customize the navigation drawer in Android Studios?
TL;DR: Create a ListView in your DrawerLayout with the appropriate implementation, Adapter, item.xml and such.

I found the way to display the original images, here is the fragment of code:
navigationView.setItemIconTintList(null);
"navigationView" is the name for my drawer, change it to yours on your MainActivity

Related

How to change font,size of text and set text position center ToolBar Android?

I need to change the font size, the font itself, and center the text in the ToolBar.
I took a template from Bottom Navigation Activity studio.
I was trying setting the text size in themes.xml (I was able to change the background color and text color there)
<style name="CustomToolBarStyle" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="titleTextColor">#color/black</item>
<item name="android:background">#color/white</item>
I was trying to use this option in themes.xml:
<item name="android:gravity">center</item>
It didnt help. I tried to follow this instruction:
https: //stackoverflow.com/questions/26533510/android-toolbar-center-title-and-custom-font Throws android.support.v7.widget.Toolbar into an error and get a crash.
ToolBar
NavbarTemplate
For API level 21+ you can use app:titleTextAppearance property of toolbar
<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="?actionBarSize"
android:id="#+id/toolbar"
app:titleTextAppearance="#style/yourstyle" />
and override the default title size in a custom style:
<style name="yourstyle" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">18sp</item>
<item name="android:fontFamily">yourFont</item>
</style>
For centering, you should follow this answer and if this and none of the other answer works for you from the same post https://stackoverflow.com/a/38175403/9917400, than just use a linear layout inside Toolbar.
You can set constraint-layout inside the toolbar
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_collapseMode="pin">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textColor="#color/blue"
android:textSize="16sp"
app:fontPath="fonts/xxx" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
If you're using androidx don't use android.support.v7.widget.Toolbar.
You have to use androidx.appcompat.widget.Toolbar.
Create toolbar.xml in res/layout.
Put this code:
<?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="yourToolBarBackgroundColor"
>
<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="TestToolBar"
android:fontFamily="yourFont"
android:textSize="yourSizeOfText"
android:textColor="yourTextColor" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/activity_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Disable ActionBar in themes.xml:
<style name="Theme.YourProject" parent="Theme.MaterialComponents.DayNight.NoActionBar">
For night theme you have to do same thing in themes.xml(night)
Then in MainActivity.java in onCreate:
setContentView(R.layout.toolbar);

Android Floating Action button : Resize inside icon

Hi I want to set image into FloatingActionButton i did this :
<android.support.design.widget.FloatingActionButton
android:id="#+id/qick_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="#drawable/quick_menu"
app:elevation="7dp"
app:layout_anchor="#id/mapview"
app:layout_anchorGravity="bottom|right|end" />
But I have a small image in my button
try this: in Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dp"
android:scaleType="fitXY"
android:src="#drawable/map"
app:backgroundTint="#android:color/transparent"
app:borderWidth="0dp"
app:useCompatPadding="true" />
</RelativeLayout>
Now in res/values/dimens.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="fab_margin">16dp</dimen>
<dimen name="design_fab_image_size" tools:override="true">56dp</dimen>
</resources>
This line is important <dimen name="design_fab_image_size" tools:override="true">56dp</dimen>
output:
you can use fabSize attribute
app:fabSize="normal"
Try this: android:scaleType="center" and try image size 56dp. It worked for me.
The image/icon inside FAB can be resized in XML.
app:maxImageSize = "32dp" This will be your image size inside Floating Action Button.
app:fabCustomSize = "64dp" This will be your Floating Action Button size.
You can change the image if you want to change the size of the inner image, just choose the image of your choice and add it to floating button.

Android, how to change the default facebook button

I am using facebook login. I am trying to change the button, but it dont now work:
I want to make the height 100dp.
Login Page xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/splash_screen"
android:scaleType="centerCrop"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/label"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#FFF"/>
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/bt_skip_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Skip Login"
android:onClick="autoLogin"
android:layout_above="#+id/login_button"
android:layout_centerHorizontal="true"
android:te
xtColor="#FFF"/>
<com.facebook.login.widget.LoginButton
xmlns:fb="http://schemas.android.com/apk/res-auto"
android:id="#+id/login_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:scaleType="centerInside"
style="#style/FacebookLoginButton"/>
</RelativeLayout>
</RelativeLayout>
Style :
<style name="FacebookLoginButton">
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginBottom">0dp</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">90dp</item>
</style>
I want to look like this, but just to fill the width.
I have update my code, Still no change to the login bt
If you want to put custom Facebook button as per your requirement, best option is that you hide the Facebook Login button and Customize the native Android button and put it above the orignal Facebook Login button. Now on click of newly customized button call loginbtn.performClick();. By this way you can change the text of Button also.

Make only a part of ListView element change when clicked

I find it very difficult to explain my problem to a search engine, or to anyone for that matter. I'll do my best to explain it. I have a ListView and I made custom views for elements in the ListView. This is the XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="#dimen/list_item_height"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="#color/black"
android:gravity="center"
android:background="#drawable/spinner_dropdown_style"/>
<View
android:id="#+id/divider"
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/text1"
android:background="#drawable/shadow" />
</RelativeLayout>
And this is how the ListView looks like:
As you you can see, the divider is always grey but when ListView element is clicked, the divider turns blue. This is the problem. I only want the white part of the element to turn green and the grey divider to stay grey.
This is the spinner_dropdown_style (selector):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="#android:color/transparent" />
<item android:state_pressed="true"
android:drawable="#color/button_color_pressed" />
</selector>
How do I make it so that selector will only work on the white area and the grey divider stays grey even when element is clicked?
add android:divider="#layout/id of layout" this line in your list view
make another layout for
<View android:id="#+id/divider"
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/text1"
android:background="#drawable/shadow" />
add remove element.
hope this will help out
add to your list view in xml:
android:drawSelectorOnTop="false"
android:cacheColorHint="#00000000"
The ListView has its own selector that it applies to selected items. You are just toggling the background of the TextView, not the entire selected item. To do that, you can try adding this to your listview:
android:drawSelectorOnTop="true"
android:listSelector="#color/button_color_pressed"

how to put button beside activity titile in android?

Hi i want to put button beside activity title in android. I can change the text using setTitle() but I haven't find anything which allows to put button beside title of Activity. I am using android 4.0.3. Can anybody help me?
Create xml file in your project name topbar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="vertical"
android:background="#android:color/white">
<TextView
android:id="#+id/title_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MyTitle: NameOfUser"
android:layout_centerVertical="true"
android:textColor="#android:color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginLeft="20dp"/>
<Button
android:id="#+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Btn"
android:textColor="#android:color/white"
android:layout_marginLeft="5dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
And add this layout in any layout by just writing:
<include layout="#layout/topbar"/>
Hope this will help you.
You can use custom title bar or you can use no title theme for your Activity and set a linear layout as child layout above of your Activity's layout,so it seems that your activity has title bar.
Creating custom title bar:
edumobile.org
londatiga.net
Hidding the title bar of your application.

Categories