change the size of back arrow in actionbar android - android

I have used a picture in the actionbar for back arrow but I want to resize the picture height and width which is shown below: how I can change the size of that. Also I attached the xml file as well
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:homeAsUpIndicator">#drawable/images</item>

Let's say you have actionbar.xml layout file containing this code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_bar_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageButton
android:id="#+id/action_bar_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dimen_very_small"
android:background="#color/colorPrimary"
android:padding="#dimen/dimen_medium"
android:src="#drawable/abc_ic_ab_back_mtrl_am_alpha" />
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/dimen_medium"
android:text="#string/title_activity_details"
android:textColor="#ffffff"
android:textSize="24sp"
android:textStyle="normal" />
</LinearLayout>
</LinearLayout>
And you want to change size of view with id 'action_bar_back'. Here it's ImageButton, but 'ImageView' also would be good. You can do it i many ways:
change layout_width and layout_height with exact value like 60dp
scale an image using scaleX, scaleY, scaleType. It uses float values.
use this great tool: https://romannurik.github.io/AndroidAssetStudio/ to generate some needed sizes of image and put them in drawable-[density] folders
using programs like GIMP to resize the picture to desired size
Hope this article would be useful: Supporting Multiple Screens

Related

Button with icon and text centered

I want to have button with icon and text near icon both in center. I managed to do that with padding but since android has a lot of different screen sizes, it is not working well on small screen sizes. This is my code:
<Button
android:id="#+id/scanbtn"
android:text="#string/scan_btn_text"
android:textAlignment="center"
android:fontFamily="#font/open_sans_light"
android:drawableStart="#drawable/ic_photo_camera"
android:paddingStart="100dp"
android:paddingEnd="120dp"
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="25dp"
android:background="#drawable/border"
android:textColor="#color/white"
android:textSize="19sp"
android:transitionName="use/scanbtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="parent" />
This is my button. Please show me other (better) way to make this button be same on all screen sizes
Just wrap a TextView by FrameLayout and use the Framelayout as a button.
<FrameLayout
android:background="#E06666"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/icon"
android:text="Scan"
android:layout_gravity="center"
android:gravity="center"
/>
</FrameLayout>
Bellow is my result:
Maintain the button padding across various screen size
--- Create different values folders
values, values-sw350dp, values-sw480dp, values-sw600dp, values-sw720dp
-- Add a file dimens.xml in each folder
-- Add a dimension with same name but different values on each file
<!-- Add this to dimens.xml in values folder -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="button_padding_start">100dp</dimen>
</resources>
<!-- Add this to dimens.xml in values-sw350dp folder -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="button_padding_start">150dp</dimen>
</resources>
<!-- Do the same for all other folders and keep increasing the value of button_padding_start -->
Then reference the value from your layout instead of using a fixed dp and let android select the best choice based on the screen size to maintain similar look across various device.
<!-- paddingEnd may not be necessary -->
android:paddingStart="#dimen/button_padding_start"
Learn more about supporting different screens here
Use the following code and remove all paddings from button XML:
android:drawableLeft="#drawable/image
Hope you get the answer. Please select as right answer.
Just use the MaterialButton with the app:iconGravity attribute.
Use the textStart or textEnd value.
<!-- Icon gravity textStart -->
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/...."
app:iconGravity="textStart"
android:text="#string/..."
.../>
<!-- Icon gravity textEnd -->
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/..."
app:iconGravity="textEnd"
android:text="#string/...."/>

Combining two drawables, one over another which have a different size

I have a game which shows the avatars of the users as drawables. The avatars have a width of 150dp. Now I want to place on the bottom center a red or green dot which indicates if a user is online or not. I have the red/green dot as drawable as well. The dots have a width/height auf 30dp. How do I combine them?
Until now I was loading those avatars using the picasso library. I would really like to continue doing so, will that be possible?
I have a #drawable/avatar and either a #drawable/red_dot or #drawable/green_dot
Not sure what kind of source code I could provide here :-)
Thank you for your help in advance!
Edit:
I have been experimenting with Layer List which I saved as avatar.xml inside the drawable directory, but this only shows two same sized drawables ... so not something I really desire:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/avatarPic"
android:drawable="#drawable/avatar"
android:width="150dp"
android:height="wrap_content"
/>
<item
android:id="#+id/avatarBadge"
android:drawable="#drawable/redDot"
android:width="30dp"
android:height="30dp"
/>
</layer-list>
Second Edit:
Just wanted to mention that I went with a RelativeLayout here.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/avatarPic"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:src="#drawable/avatar" >
</ImageView>
<ImageView
android:id="#+id/avatarBadge"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignBottom="#id/avatarPic"
android:layout_centerHorizontal="true"
android:layout_centerVertical="false"
android:layout_marginBottom="1dp"
android:adjustViewBounds="true"
android:src="#drawable/red_dot" >
</ImageView>
</RelativeLayout>
and in the main layout it is accessible through the <include> tag
A quick solution is to use a RelativeLayout.
Place the avatar ImageView as first child in the RelativeLayout
Plece the dot ImageView to be drawn over the image view bottom left with e.g.
layout_alignParentLeft="true"
layout_alignParentBottom="true"

Support text in multiple screen size

I have researched on many relevant topics but the topics mainly revolve around images. I had understand this: http://developer.android.com/guide/practices/screens_support.html but I don't really get the idea of dynamic resizing for the fonts.
For images there's no problem, I used the ldpi, mdpi, hdpi and xhdpi. However for the fonts, I do not know how to resize them. Anyone?
My codes is as followed:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/headerimage"
android:orientation="horizontal">
<TextView
android:id="#+id/headerTitle"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#FFF"
android:textSize="28dp"
android:textStyle="bold"
android:text="Some Random Banner Title"
style="#style/headerTextStyle"
/>
<ImageButton
android:id="#+id/btnPlus"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_marginTop="6dp"
android:background="#0000"
android:scaleType="centerCrop"
android:src="#drawable/plus" />
<ImageButton
android:id="#+id/btnInfo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="6dp"
android:background="#0000"
android:scaleType="centerCrop"
android:src="#drawable/info" />
<ImageButton
android:id="#+id/btnGuide"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btnPlus"
android:background="#0000"
android:scaleType="centerCrop"
android:src="#drawable/guide" />
</RelativeLayout>
I would put the text sizes in a style resource, then use alternate style resource files for screen sizes..
so you will have res/values-large/style.xml, res/values-small/style.xml, etc. Each of those styles will contain the item for the text size with different dp values. Remember, hdpi, mdpi, ldpi etc have to do with pixel density, not screen size.
So for instance, if in res/values/style.xml you have:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="headerTextStyle" >
<item name="android:textSize">28dp</item>
...
</style>
</resources>
in another resource file, suppose for large screen (res/values-large/style.xml) :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="headerTextStyle" >
<item name="android:textSize">46dp</item>
...
</style>
</resources>
Then of course apply this style to the element, and the system will pick the correct one based on the device's screen size.. You can provide multiple styles in the same file, one for header text, one for regular text - whatever distinct types of text you need, similar to CSS.
Alternatively, you could duplicate your layout file in similarly qualified resource directories, like res/layout-large/layout.xml, res/layout-small/layout.xml and have different text sizes in each, but this will be duplicating more xml unnecessarily.

How to create really small buttons in android from code?

Android default Button class provides a really big button.
There is a lot of wasted space around. Now I want to create really small buttons, that are only slightly bigger than the text/caption. How to do this from code?
So far I found the following method, but it is still providing too big buttons and wasting too much space:
A. Create an XML (smallbutton.xml) in the res/layout:
<?xml version="1.0" encoding="utf-8"?>
style="?android:attr/buttonStyleSmall"
android:text="color"
android:padding="0dp"
android:layout_margin="0dp"
android:textSize="8dp"
android:maxHeight="2dp"
android:maxWidth="2dp"
/>
B. From your code inflate this and add to the view:
Button pickBackColor = (Button) this.getLayoutInflater().inflate(R.layout.smalbutton,null);
...
LinearLayout linLayout = new LinearLayout(this);
linLayout.addView(pickBackColor);
Now the problem is that the button is still too big, it uses too much space around (on the left, right, above, under) of the text.
Any hint how to decrease the size of the button further?
Buttons have a minimal height and width (typically of 48dp respectively 64dp by default). So add
android:minHeight="0dp"
android:minWidth="0dp"
to get really small buttons:
For your information there is also the default style provided by Android that defines smaller buttons:
style="?android:attr/buttonStyleSmall"
You can also if necessary modify this default style and define custom attributes in your file styles.xml. Here is an example with the modifications described in the question:
<style name="MyButtonStyleSmall" parent="android:Widget.Button.Small">
<!-- Customizations -->
<item name="android:minHeight">0dp</item>
<item name="android:minWidth">0dp</item>
</style>
Then you just need to call it in the XML:
<Button
android:id="#+id/small_button"
android:text="#string/example"
android:contentDescription="#string/example_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/MyButtonStyleSmall" />
in .xml file...
you need to pass small value to layout_height and layout_width in xml file...... try this.
android:layout_width="50dp" android:layout_height="50dp"
Change value as per your requirement...
try this in your code:
pickBackColor.setWidth(VALUE);
pickBackColor.setHeight(VALUE);
<com.google.android.material.button.MaterialButton
android:id="#+id/food_item_minus_button"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/custom_small_button_bg"
android:drawableLeft="#drawable/ic_minus"
android:paddingStart="6dp"
android:paddingLeft="6dp" />

How to remove button background color

I searched a lot but I didn't find how to remove the background color from the button which is appearing on the right and left side of button. Can anybody help?
My screen looks like
No matter what I try I am not able to remove the black portion.
Code:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn_base"
android:text="#string/base"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"/>
<Button
android:id="#+id/btn_care"
android:text="#string/care"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn_daily_prize"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"
android:text="#string/daily_prize" />
<Button
android:id="#+id/btn_winner"
android:layout_margin="2dp"
android:textColor="#000000"
android:background="#drawable/selector_button"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="30dp"
android:text="#string/winner" />
</LinearLayout>
</LinearLayout>
#Drawable/selector_button
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="#drawable/pressed"> </item>
<item android:state_focused="true"
android:drawable="#drawable/focused"/>
<item android:drawable="#drawable/change"></item>
</selector>
this is the image used with name change.9.png
according to your scenario i can surely say that either you are not using a 9-patch image (an image with extension like .9.png ) or the 1 pixel borders of 9-patch at left and top are not drawn in correct manner. thats why the edges and the side border shade get expanded with the button with long width. either you should show what 9-patch button background you have used or try some correct 9-patch and check results for that.
Why don't you use some other control element like TextView instead of buttons? I just saw that TextView has onClickListner and so you can use it as sort-of button, though I have not done it; button is meant to aid you defining your layout, but as this seems to only be a problem for you, just do not use it).
By the way I seriously recommend you to use android styles, as you copy-paste a lot of attributes. If you use Eclipse for development, open your layout xml, select the item you want to extract the style of, press ctrl + 1 and then select extract style. That way you should avoid copy-pasting all these style attributes.
Try removing the android:textColor attribute. These can be misleading and sometimes alter the colour of more than just the text. If the text is supposed to be black then you don't need it.

Categories

Resources