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/...."/>
Related
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
I create a layout for dailpad using LinearLayout and I divided the number by using layout_weight.
<LinearLayout
android:id="#+id/layout_digitOne"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum=".9">
<TextView
android:id="#+id/digit_one"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".6"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#e03a2f2d"
android:textSize="45sp"
android:text="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2"
android:layout_gravity="top"
android:gravity="top|center_horizontal"
android:textSize="15dp"
android:text=""/>
</LinearLayout>
Here for the Digit one in the TextView I set the size for Text by 45sp.but when got the small screen because of text size specify in the layout, the layout is distroying.
Is it possible according to TextView layout_height can set the textSize
Can anyone give me an idea
Thanks in advance :)
You need make a different layouts or use that library,
https://github.com/grantland/android-autofittextview
You should use styles, then you can have separate folders "values" (default) "values-hdpi" (high density) "values-mdpi" (medium density) and so on and put your style file with correct textSize values in each folder as needed.
For example,
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-large/dimens.xml
res/values-xlarge/dimens.xml
Now specify the values you desire to keep in different XML file.
Example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
By doing this it will fetch the correct XML according to your screen size and set the TextSize mentioned in that XML.
Use dimens.xml to control the size of TextView according to device size. You can do this by setting different size of Text in different versions of dimens.xml and then reference the size in layout xml instead of directly hardcoding it.
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2"
android:layout_gravity="top"
android:gravity="top|center_horizontal"
android:textSize="#dimen/<nameOfTextSizeInDimens>"
android:text=""/>
When using the new FloatingActionButton, the size is determined by app:fabSize="normal". How can I set what in dp is the size referenced by "normal"?
I tried to create values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="app">
<attr name="fabSize">
<enum name="mini" value="50dp" />
<enum name="normal" value="100dp" />
</attr>
</declare-styleable>
</resources>
But I get the error
"normal" in attribute "fabSize" is not a valid integer
There are two different sizes of FAB available: normal or mini
Normal (56dp) — This size should be used in most situations.
Mini (40dp) — Should only be used when there is a need for visual continuity with other components displayed on the screen.
You can override the normal and mini sizes by adding the following to values/dimens.xml:
<!-- Overriding sizes of the FAB -->
<dimen name="design_fab_size_normal">90dp</dimen>
<dimen name="design_fab_size_mini">30dp</dimen>
The tricky thing would be if you need more than 2 fab sizes, in that case i guess you need to create a custom view extending the fab.
Just user app:fabCustomSize in xml
app:fabCustomSize="100dp"
Bingo.
I know it's not recommended, but for those that absolutely need to change the default sizes, I was able to do it by wrapping the FloatingActionButton in a LinearLayout.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:orientation="horizontal" >
<android.support.design.widget.FloatingActionButton
android:layout_width="#dimen/custom_fab_size"
android:layout_height="#dimen/custom_fab_size"
app:fabSize="normal"
android:clickable="true"
android:src="#drawable/ic_mic_white_24dp"
android:scaleType="center"/>
</LinearLayout>
• Standard Sizes
There are three options for standard FAB sizes (according to developer.android) which you can set them using app:fabSize.
normal: The normal sized button.
mini: The mini sized button.
auto: Size which will change based on the window size
.
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_add_black_24dp"
app:tint="#404D54"
app:backgroundTint="#ffd500"
app:fabSize="normal" />
.
• Custom Sizes
To set custom FAB size you can set app:fabCustomSize. Note that android:layout_width and android:layout_height should be "wrap_content".
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_add_black_24dp"
app:tint="#404D54"
app:backgroundTint="#ffd500"
app:fabCustomSize="36dp" />
app:maxImageSize
<android.support.design.widget.FloatingActionButton
android:id="#+id/floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_marginBottom="60dp"
android:src="#drawable/icon_return_back"
app:backgroundTint="#00ffffff"
app:borderWidth="0dp"
app:elevation="0dp"
app:maxImageSize="46dp"
app:pressedTranslationZ="0dp"
app:rippleColor="#00ffffff" />
<dimen name="design_fab_size_normal">90dp</dimen>
<dimen name="design_fab_size_mini">30dp</dimen>
Set this in dimen file.
To change Fab size and to see large image init i have made below changes in android api 28:- thew are as
<android.support.design.widget.FloatingActionButton
android:id="#+id/profile_imageview"
android:layout_width="#dimen/design_fab_size_mini"
android:layout_height="#dimen/design_fab_size_mini"
android:layout_marginTop="10dp"
android:src="#drawable/pin_check"
app:borderWidth="0dp"
app:elevation="0dp"
app:maxImageSize="90dp" />
where in dimens.xml
<dimen name="design_fab_size_mini" tools:override="true">90dp</dimen>
<dimen name="design_fab_content_size" tools:override="true">58dp</dimen>
Just add app:fabCustomSize="70dp" in your xml file under FAB component
Here is an example of a FAB button :
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_AddRoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="#android:drawable/ic_input_add"
app:fabCustomSize="70dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.93"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.93" />
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.
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" />