Awais Soomro Awais Soomro - 3 months ago 18
Android Question

Button topmost in android

I have layer, it is transparent. I can click on button only, I want to click through layer.

Here's my Manifest:

<activity android:name=".Test" android:theme="@style/Theme.Transparent"/>


Here's Transparent theme style:

<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>


Here's my Activity code:

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);

// v I can touch through app, but cant click on button
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
// ^ I can touch through app, but cant click on button

Button xclose = (Button) findViewById(R.id.buttonx);
xclose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
}
}


Here's my test.xml layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<Button android:id="@+id/buttonx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="X" />
</RelativeLayout>

Answer

Try this code and set click listener on the linear layout

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
          android:layout_width="match_parent" android:layout_height="match_parent" >

         <LinearLayout android:layout_width="match_parent"    
            android:layout_height="wrap_content"
            android:id="@+id/ll">

           <Button android:id="@+id/buttonx" android:layout_width="wrap_content" 
           android:layout_height="wrap_content" android:layout_alignParentRight="true" 
           android:layout_alignParentTop="true" android:text="X" />
           </LinearLayout>
          </RelativeLayout>