|
2 | 2 |
|
3 | 3 | import android.support.v7.app.AppCompatActivity; |
4 | 4 | import android.os.Bundle; |
| 5 | +import android.util.Log; |
5 | 6 | import android.view.View; |
| 7 | +import android.widget.Button; |
6 | 8 |
|
7 | 9 | import at.blogc.android.views.ExpandableTextView; |
8 | 10 | import at.blogc.android.views.R; |
|
24 | 26 | */ |
25 | 27 | public class MainActivity extends AppCompatActivity |
26 | 28 | { |
| 29 | + private static final String TAG = "ExpandableTextView"; |
| 30 | + |
27 | 31 | @Override |
28 | 32 | protected void onCreate(Bundle savedInstanceState) |
29 | 33 | { |
30 | 34 | super.onCreate(savedInstanceState); |
31 | 35 | this.setContentView(R.layout.activity_main); |
32 | 36 |
|
33 | 37 | final ExpandableTextView expandableTextView = (ExpandableTextView) this.findViewById(R.id.expandableTextView); |
34 | | - expandableTextView.setOnClickListener(new View.OnClickListener() |
| 38 | + final Button buttonToggle = (Button) this.findViewById(R.id.button_toggle); |
| 39 | + |
| 40 | + // set animation duration via code, but preferable in your layout files by using the animation_duration attribute |
| 41 | + expandableTextView.setAnimationDuration(1000L); |
| 42 | + |
| 43 | + // toggle the ExpandableTextView |
| 44 | + buttonToggle.setOnClickListener(new View.OnClickListener() |
35 | 45 | { |
36 | 46 | @Override |
37 | 47 | public void onClick(final View v) |
38 | 48 | { |
39 | 49 | expandableTextView.toggle(); |
| 50 | + buttonToggle.setText(expandableTextView.isExpanded() ? R.string.collapse : R.string.expand); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + // but, you can also do the checks yourself |
| 55 | + buttonToggle.setOnClickListener(new View.OnClickListener() |
| 56 | + { |
| 57 | + @Override |
| 58 | + public void onClick(final View v) |
| 59 | + { |
| 60 | + if (expandableTextView.isExpanded()) |
| 61 | + { |
| 62 | + expandableTextView.collapse(); |
| 63 | + buttonToggle.setText(R.string.expand); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + expandableTextView.expand(); |
| 68 | + buttonToggle.setText(R.string.collapse); |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + // listen for expand / collapse events |
| 74 | + expandableTextView.setOnExpandListener(new ExpandableTextView.OnExpandListener() |
| 75 | + { |
| 76 | + @Override |
| 77 | + public void onExpand(final ExpandableTextView view) |
| 78 | + { |
| 79 | + Log.d(TAG, "ExpandableTextView expanded"); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onCollapse(final ExpandableTextView view) |
| 84 | + { |
| 85 | + Log.d(TAG, "ExpandableTextView collapsed"); |
40 | 86 | } |
41 | 87 | }); |
42 | 88 | } |
|
0 commit comments