Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive a long press/click on a linear layout in android?

I'm painfully new to android and I've run into a wall.

I'm trying get a linear layout to function more like a button, with different actions for press and long press - The reason being so I can have 2 differently formatted text labels on each "button". Something along the lines of:

-------------------------
|          2nd          |  <- Label for long press (regular/smaller type)
|           =           |  <- Label for regular press (bold/larger type)
-------------------------

The posts I've found explain how to receive a regular click on a linear layout (I use the onClick attribute in the layout XML). But I've had no luck with long press. I've tried to define a new onLongClick attribute for xml as described in Aleksander Gralak's answer here: Long press definition at XML layout, like android:onClick does . But had no such luck - it looks like it was intended for a text view, I tried changing it to linear layout but failed miserably.

Here is the object in question: Main.xml

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:clickable="true"
    android:focusable="true"
    android:background="@drawable/darkgrey_button"
    android:onClick="equals" android:longClickable="true" android:id="equalsbutton"
    android:focusableInTouchMode="false">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2nd"
        android:id="@+id/textView"
        android:duplicateParentState="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" = "
        android:id="@+id/textView1"
        android:duplicateParentState="true"/>
</LinearLayout>

And Main.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void equals(View view) {
    Context context = getApplicationContext();
    CharSequence text = "Hello toast!";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}
like image 679
user2201544 Avatar asked Mar 23 '13 09:03

user2201544


1 Answers

Add an id to your layout.

<LinearLayout
    android:id="@+id/my_button_layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    … >

In your Main.java

Get a reference to your LinearLayout and set a OnLongClickListener.

LinearLayout buttonLayout = (LinearLayout) findViewById(R.id.my_button_layout);
…
buttonLayout.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
          Toast.makeText(Main.this, "Long click!", Toast.LENGTH_SHORT).show();
          return true;
    }

});
like image 171
Ragunath Jawahar Avatar answered Sep 28 '22 03:09

Ragunath Jawahar