Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Can not touch Android Button

Discussion in 'Android' started by radarpq, Jul 26, 2012.

  1. radarpq

    radarpq

    Joined:
    Mar 18, 2011
    Posts:
    4
    Dear Community,

    I've try to create an Android Button written in Java and call to show and tap it from Unity.

    Having read a lot of post I could successfully create button in Unity, But can not touch it. Which means any touch on that button make no sense.

    I've tried every way I could do but still failed.
    Could anyone make a help, Please?



    Code (Domob.java)
    (CreateButton is the method which I call from Unity)
    ---------------------------------

    Code (csharp):
    1. public class Domob extends UnityPlayerActivity{
    2.  
    3.     private static RelativeLayout _layout;
    4.  
    5.     public static void CreateButton(){
    6.         UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
    7.             @Override
    8.             public void run() {
    9.                 //Add a test Button
    10.                 Button btn = new Button(UnityPlayer.currentActivity);
    11.                 btn.setText("Hide Banner");
    12.                 btn.setClickable(true);
    13.                 btn.setOnClickListener(new Button.OnClickListener(){
    14.                     @Override
    15.                     public void onClick(View arg0) {
    16.                         Log.i("Test", "Button Clicked.");
    17.                     }
    18.                 });
    19.                
    20.                 _layout = new RelativeLayout(UnityPlayer.currentActivity);
    21.                 _layout.addView(btn);
    22.                
    23.                 UnityPlayer.currentActivity.addContentView(_layout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    24.             }
    25.         });
    26.     }
    27. }
    Test.cs (Unity call Test)
    -------------------------------------------
    Code (csharp):
    1. void Start(){
    2.        AndroidJavaClass jc = new AndroidJavaClass("com.test.Domob");
    3.        jc.CallStatic("CreateButton");
    4. }

    Android Manifest
    -------------------------------------------
    Code (csharp):
    1. <manifest
    2.     xmlns:android="http://schemas.android.com/apk/res/android"
    3.     package="com.test"
    4.     android:installLocation="preferExternal"
    5.     android:versionCode="1"
    6.     android:versionName="1.0">
    7.     <supports-screens
    8.         android:smallScreens="true"
    9.         android:normalScreens="true"
    10.         android:largeScreens="true"
    11.         android:xlargeScreens="true"
    12.         android:anyDensity="true"/>
    13.  
    14.     <application
    15.         android:icon="@drawable/app_icon"
    16.         android:label="@string/app_name"
    17.         android:debuggable="true">
    18.         <activity android:name="com.unity3d.player.UnityPlayerProxyActivity"
    19.                   android:label="@string/app_name"
    20.                   android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    21.             <intent-filter>
    22.                 <action android:name="android.intent.action.MAIN" />
    23.                 <category android:name="android.intent.category.LAUNCHER" />
    24.             </intent-filter>
    25.         </activity>
    26.         <activity android:name="com.unity3d.player.UnityPlayerActivity"
    27.                   android:label="@string/app_name"
    28.                   android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    29.         </activity>
    30.         <activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
    31.                   android:label="@string/app_name"
    32.                   android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    33.             <meta-data android:name="android.app.lib_name" android:value="unity" />
    34.             <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
    35.         </activity>
    36.         <activity android:name="com.unity3d.player.VideoPlayer"
    37.                   android:label="@string/app_name"
    38.                   android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    39.         </activity>
    40.        
    41.         [B][COLOR="#2e8b57"]<activity android:name="com.test.Domob" ></activity>[/COLOR][/B]
    42.  
    43.     </application>
    44.    
    45.     <!-- PERMISSIONS -->
    46.         <uses-permission android:name="android.permission.INTERNET" />
    47.         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    48.         <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    49.         <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    50.         <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />   
    51. </manifest>
    --------------------------------------------




    So in the screen capture below, we can see I successfully called "CreateButton" method,
    but just can not touch it.
    No event occurs.

    Could some one tell me why?
     
  2. lyh1

    lyh1

    Joined:
    Jun 30, 2010
    Posts:
    92
    I think you must dispatch the touch event from Activity to the view of button manually.
     
  3. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Your RelativeLayout is filling your screen and is consuming the touch events, because it's in front of the Unity's GLSurface. Use LayoutParams.WRAP_CONTENT instead, so the RelativeLayout is as big as it's content (the button).

    It's in general a bad Idea, to put Android Layouts on top of the GLSurface. Maybe you should try FrameLayout which places several Layers on top of each other
     
  4. Andy-Block

    Andy-Block

    Joined:
    Aug 10, 2012
    Posts:
    10
    We had a similar problem, and discovered that it was resolved by setting the value given in the manifest for 'unityplayer.ForwardNativeEventsToDalvik' to 'true'. I'm struggling to find much information on the implications of changing this value though (does anyone know?). Hope that helps you or someone else. Regards,

    Andy