6

Is there anyway to set height of a "android.support.v7.widget.Toolbar" half of a predefined attr like "?android:attr/actionBarSize" in layout xml file? in fact I want the height of my toolbar be something like :

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height=("?android:attr/actionBarSize")/2
    >
MatPag
  • 41,742
  • 14
  • 105
  • 114
Mostafa
  • 95
  • 1
  • 10
  • No. You can't. You need to set programmatically or apply manually in xml file – Piyush Aug 04 '17 at 07:10
  • normally toolbar height is 50dp to 55dp so you can measure from your own need it can be 25dp or 26dp will do for all device. – 9spl Aug 04 '17 at 07:13
  • height of toolbar in all devices are 50 to 55dp? – Mostafa Aug 04 '17 at 07:15
  • 2
    @9sol, that's not correct. It accepts 56dp, 48dp (landscape), 64dp (on sw600dp). – azizbekian Aug 04 '17 at 07:17
  • 1
    Instead of hardcoding any value as defautl one might change with newer android APIs (or be different on other devices), I would set it programmatically - get the value of android:attr/actionBarSize, multiply and set. – Deividas Strioga Aug 04 '17 at 07:23
  • @DeividasStrioga yes, this is the best option for sure – MatPag Aug 04 '17 at 07:29
  • i've added an answer with some details on how to do this in Java code, @Mostafa let me know if this works for you. – MatPag Aug 04 '17 at 07:50
  • @MatPag, initial question had **not** stated, that it wanted a programmatic solution. Entirely opposite - it wanted an xml solution. Performing such a change you are making other answers, such as mine, become invalid, which is not accepted. Please revert back the original question. – azizbekian Dec 09 '17 at 13:22
  • @azizbekian Done. Wasn't my intention to change the user's question. Only adding a good keyword to help people finding a partial XML and programmatically solution we posted. – MatPag Dec 09 '17 at 13:38
  • 1
    @MatPag, as OP has mentioned in [his comment](https://stackoverflow.com/questions/45500313/set-height-of-toolbar-half-of-attr-actionbarsize-in-xml?noredirect=1#comment77966934_45500909) he knew about programmatic solution, so he was interested in XML solution specifically. Imho, providing an answer, that does **not** exactly answer the question (programmatic way, like you did), does not yet mean, that the question should be changed. Thanks for cooperation. – azizbekian Dec 09 '17 at 13:43

2 Answers2

2

It is not possible to perform arithmetic actions in xml.

If you want to give the value through xml, then you have to perform following:

  1. in values/dimes.xml define a variable halfActionBar and make it be 28dp (original is 56dp).
  2. in values-land/dimes.xml define a variable halfActionBar and make it be 24dp (original is 48dp).
  3. in values-sw600dp-v13/dimes.xml define a variable halfActionBar and make it be 32dp (original is 64dp).

In your styles.xml theme.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:actionBarSize">@dimen/halfActionBar</item>
    ...
</style>

Then in your layout:

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"/>

Note, that this is a bad solution, because it depends on platform implementation. I advice to get actionBarSize from java/kotlin code (not from xml).

azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • I don't like this solution, it is very error prone to changes in the Android platform. I think the only way to do this in an effective manner is in Java code. I will try something different when i go home. – MatPag Aug 04 '17 at 07:27
  • @MatPag, totally agree. – azizbekian Aug 04 '17 at 07:31
1

You can't do it in XML but if you want to go with the programmatically way you can try with something like this, i took the idea from here

//themedContext is an Activity or a Context which has a Theme attached,
//you can't use Application context for this
final TypedArray array = themedContext.getTheme().obtainStyledAttributes(
                new int[] { android.R.attr.actionBarSize });
int actionBarSize = (int) array.getDimensionPixelSize(0, -1);
array.recycle();

Then to apply this to the Toolbar you could do:

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);

RelativeLayout.LayoutParams layoutParams = 
                 (RelativeLayout.LayoutParams) myToolbar.getLayoutParams();
//half the height of toolbar prior to set its value
layoutParams.height = (int)(actionBarSize / 2);
myToolbar.setLayoutParams(layoutParams);

setSupportActionBar(myToolbar);

PS: don't forget that this attribute value changes when the device is rotated, so you should set this custom value every time the device changes from portrait to landscape and viceversa.

MatPag
  • 41,742
  • 14
  • 105
  • 114
  • 2
    I knew I can do it programmatically . the question was about defining that value in xml files , as it seems it not Possible – Mostafa Aug 04 '17 at 09:31
  • No, is not possible in a reliable manner with XML, you didn't provided any hint in your question that you didn't intend to try with the Java code, this is why i posted my answer – MatPag Aug 04 '17 at 09:32