Android Tutorial Material Design 3 - Using a ToolBar as ActionBar

Inman

Well-Known Member
Licensed User
Longtime User
When you find time, please work on a transparent toolbar. Google uses it in their Play Store app (screenshot on the right side).

 

shashkiranr

Active Member
Licensed User
Longtime User
Can you attach the code of your addcreditcard Activity?

Hi Corwin,

PFB the activity code

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

#Extends: android.support.v7.app.ActionBarActivity

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Basescv As ScrollView
    Private SaveButton As Button
    Private CPanel As Panel
    Private EditText1 As EditText
    Private EditText2 As EditText
    Private EditText3 As EditText
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Basescv.Initialize(Activity.Height)
    Activity.AddView(Basescv,0,0,100%x,100%y)
    Basescv.Panel.LoadLayout("CardLayout")
    Basescv.Panel.Height = SaveButton.Top+SaveButton.Height
    Log(Basescv.Panel.Height)
    Log(Basescv.Height)
    If Basescv.Panel.Height < Activity.Height Then
        Basescv.Panel.Height = Activity.Height
    Else If Basescv.Panel.Height > Basescv.Height Then
        CPanel.Height = SaveButton.Top+SaveButton.Height
    End If
   
    EditText2.InputType = EditText2.INPUT_TYPE_NUMBERS
    EditText3.InputType = EditText3.INPUT_TYPE_NUMBERS
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub dummy
End Sub


Sub SaveButton_Click
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("Bank :").Append(EditText1.Text).Append("Cardnumber :").Append(EditText2.Text).Append("cvv :").Append(EditText3.Text)
    Log(sb.ToString)
   
   
   
End Sub
 

corwin42

Expert
Licensed User
Longtime User
Hi Corwin,

PFB the activity code
I think the problem is that you set #IncludeTitle: false
This may not work correctly with AppCompat. If you want to disable the title do it in the theme like in the example project.
 

corwin42

Expert
Licensed User
Longtime User
When you find time, please work on a transparent toolbar. Google uses it in their Play Store app (screenshot on the right side).
Have you tried to make the ToolBar background transparent?
 

koaunglay

Member
Licensed User
Longtime User

I like so much. But I have some error. please help what is my error?
B4X:
B4A version 4.30
Parsing code.  0.10
Compiling code.  0.30
Compiling layouts code.  0.05
Generating R file.  Error
..\resource\values\themes.xml:6: error: Error: No resource found that matches the given name: attr 'colorAccent'.
..\resource\values\themes.xml:4: error: Error: No resource found that matches the given name: attr 'colorPrimary'.
..\resource\values\themes.xml:5: error: Error: No resource found that matches the given name: attr 'colorPrimaryDark'.
 

Inman

Well-Known Member
Licensed User
Longtime User
Have you tried to make the ToolBar background transparent?

Sorry I didn't. My apologies if it works correctly. I am on vacation with my family and will reach home only by March 1st week.
 

gyruss68

Member
Licensed User
Longtime User
Hi Corvin,
i have downloaded your example and compile it.
When i try to execute and debug it , it's stop on emulation.

What's are the problem ?

Thenk's
Franco

The line on IDE is :
Activity.LoadLayout("main")

The declaration is :
#AdditionalRes: ..\resource
#AdditionalRes: C:\Users\Rataplan\Desktop\AppCompatToolbarMenuExample\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\Android\android-sdk\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#Extends: android.support.v7.app.ActionBarActivity



The error is :
** Activity (main) Create, isFirst = true **


java.lang.RuntimeException: java.lang.reflect.InvocationTargetException


at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:166)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:208)
at de.amberhome.appcompat.toolbarexample1.main._activity_create(main.java:353)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at de.amberhome.appcompat.toolbarexample1.main.afterFirstLayout(main.java:100)
at de.amberhome.appcompat.toolbarexample1.main.access$100(main.java:17)
at de.amberhome.appcompat.toolbarexample1.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)


at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:54)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:158)
... 17 more
Caused by: java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.getDrawable
at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:133)
at android.support.v7.internal.widget.TintTypedArray.getDrawable(TintTypedArray.java:62)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:249)
 

corwin42

Expert
Licensed User
Longtime User
Seems that you have not copied the latest android-support-v4.jar to your custom libs folder.
 

gyruss68

Member
Licensed User
Longtime User
Seems that you have not copied the latest android-support-v4.jar to your custom libs folder.
Thank's Corvin,
I solved.
The problem was that the IDE B4a, even though I had set the path to the custom lib, sought the lib folder libraries of program files.
I copied the two libraries and it worked.

Franco
 

Inman

Well-Known Member
Licensed User
Longtime User
I am adding views to toolbar using actionbar.addview() instead of Activity.AddMenuItem3() as I need to toggle the visibility of certain contents based on the content of that activity. While views get added correctly, I can't find a way to space them correctly, even with gravity. If I use left or center for gravity, the icon is very near to actionbar title text. If I use right for gravity, the icon is at the very right edge of the actionbar. I would like to leave some space from the right edge, sort of like how much space you get on the left edge when you set the ABHelper.ShowUpIndicator.

Is it possible?
 

RandomCoder

Well-Known Member
Licensed User
Longtime User

corwin42

Expert
Licensed User
Longtime User

Can you show an example what the problem looks like?


Yes, just add a TabBar directly under the Toolbar.
 

Inman

Well-Known Member
Licensed User
Longtime User
Can you show an example what the problem looks like?



The above is the result of this code
B4X:
    pb.Initialize("")
    pb.Indeterminate=True
    actionbar.AddView(pb,24dip,24dip,Gravity.left)

imgabshare.Initialize("imgabshare")
    imgabshare.Gravity=Gravity.FILL
    imgabshare.Bitmap=LoadBitmap(File.DirAssets,"ic_share_white_24dp.png")
    actionbar.AddView(imgabshare,24dip,24dip,Gravity.RIGHT)

As you can see, the share icon is on the extreme right and progressbar are very close to title. I would like to add some padding for them, from their neighbor/edge.

Compare that to how it looks on Google Drive app

 

Inman

Well-Known Member
Licensed User
Longtime User
Here is another issue I am having with Toolbar

After adding the toolbar, I add a panel to the activity. This panel is nearly full-size width and height is therefore supposed to be on top of toolbar as well (it is used as a popup). On Android 5.0 device, the top portion of the panel is behind the toolbar and is therefore cut-off. But on Android 4.x versions it works as intended.

I tried with and without toolbar.SetAsActionBar but that doesn't seem to have any effect. Any idea?
 

corwin42

Expert
Licensed User
Longtime User
As you can see, the share icon is on the extreme right and progressbar are very close to title. I would like to add some padding for them, from their neighbor/edge.

Adding Views to a Toolbar should mainly be used for custom views as a replacement for the title. Action items should be added as menu items.


I can only guess what happens here. Do you set an elevation value for the ToolBar?
If yes, thats the problem here. In Android 5.0 Google introduced the elevation value for views. With this value you can control the depth order of views. So views with higher elevation will always display in front of views with lower elevation. Try to set a higher elevation for the panel than for the ToolBar and it should display in front of it.
 

Inman

Well-Known Member
Licensed User
Longtime User
Adding Views to a Toolbar should mainly be used for custom views as a replacement for the title. Action items should be added as menu items.

But if I add action items as menu items, will I be able to selectively show/hide it when needed? I have your ViewPager in the Activity and when the user swipes to different pages, I need to show and hide certain items on toolbar based on the CurrentPage.


You are absolutely correct. I had elevation for toolbar as mentioned in your tutorial for shadow effect. Now I set a higher elevation for the panel and it worked. Thank you so much for this wonderful library.
 

migrec

Member
Licensed User
Longtime User
what is the "click event" for the "up" indicator?
like:
B4X:
ABHelper.ShowUpIndicator = True
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…