Reputation: 326
I have a layout which is inherited from LinearLayout, I have this Custom View on MainActivity.
My problem is the OnLongTouch won't work when I put this Custom View on an Activity and it seems my custom view is not clickable!
Layout code:
public class AliLayout extends LinearLayout {
public AliLayout(Context context, AttributeSet attrs) {
super(context, attrs);
if(! isInEditMode()){
final AliLayout ly2 = this;//(AliLayout)findViewById(R.layout.ali_layout);
ly2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
ly2.setTitle("Hello world!");
return false;
}
});
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
LayoutInflater.from(this.getContext()).inflate(R.layout.ali_layout,
this);
}
public void setTitle(String txt) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(txt);
}
}
inflated xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="68dp"
android:layout_marginLeft="40dp"
android:background="#E0D641"
android:clickable="true"
android:longClickable="true"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
MainActivity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:longClickable="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.component.AliLayout
android:id="@+id/ali1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="22dp" >
</com.example.component.AliLayout>
</RelativeLayout>
Upvotes: 1
Views: 5838
Reputation: 19
layout.setclickble="true"
in XML file and
layout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: 0
Reputation: 326
After trying and failing for hundred times I found the solution to my problem!
Instead of:
ly2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
ly2.setTitle("Hello world!");
return false;
}
});
I should have written:
this.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
setTitle("Hello World");
return true;
}
});
And I shouldn't have used android:clickable
and android:longClickable
for inflated XML(ali_layout.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="68dp"
android:layout_marginLeft="40dp"
android:background="#E0D641"
android:orientation="vertical" >
Finally, the problem solved ...
Upvotes: 1
Reputation: 5600
I am answering the question of title, you can use two options for xml and code:
Put in the xml:
android:clickable="true"
Or in .java
yourLinearLayout.setClickable(true);
Upvotes: 0
Reputation: 1032
The problem is that your layout is not filling the screen at all since you are using wrap_content with no content in it:
switch your layout to
<com.example.component.AliLayout
android:id="@+id/ali1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_marginTop="22dp" >
</com.example.component.AliLayout>
Upvotes: 0