Skip to content

Instantly share code, notes, and snippets.

@xaethos
Created June 24, 2015 04:12
Show Gist options
  • Save xaethos/ed488eebc29a01687a92 to your computer and use it in GitHub Desktop.
Save xaethos/ed488eebc29a01687a92 to your computer and use it in GitHub Desktop.
Expandable text view
<LinearLayout 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:orientation="vertical"
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="net.xaethos.sandbox.expand.ExpandTextActivity">
<FrameLayout android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec convallis id dolor non pulvinar. Vestibulum nec eleifend magna, vitae dictum risus. Fusce tempus metus et ipsum aliquam tincidunt. Nulla vulputate dui nec nisi tincidunt, at auctor elit aliquam. Ut viverra ac dolor ultricies commodo. Nam quis turpis sit amet neque cursus sodales vitae sit amet erat. Aenean ornare dignissim turpis sit amet scelerisque. Duis sit amet condimentum mauris. Cras elementum tincidunt dapibus. Donec semper est odio, sit amet volutpat nunc efficitur sed. Phasellus eu semper felis, eu accumsan turpis. Praesent venenatis vestibulum dolor vel ornare. Nulla tincidunt tempus pretium. Curabitur sodales viverra dolor, ut congue tortor luctus at." />
</FrameLayout>
<Button android:id="@android:id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toggle"
android:text="Toggle" />
</LinearLayout>
package net.xaethos.sandbox.expand;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import net.xaethos.sandbox.R;
public class ExpandTextActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expand_text);
}
@Override
protected void onResume() {
super.onResume();
View text = findViewById(android.R.id.text1);
int height = text.getMeasuredHeight();
Log.d("XAE", "Height is " + height);
if (height < 700) findViewById(android.R.id.button1).setVisibility(View.GONE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_expand_text, menu);
return true;
}
private boolean expanded = true;
public void toggle(View button) {
View content = findViewById(R.id.content);
ViewGroup.LayoutParams params = content.getLayoutParams();
params.height = expanded ? 700 : ViewGroup.LayoutParams.WRAP_CONTENT;
expanded = !expanded;
content.setLayoutParams(params);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment