Reputation: 315
I am willing to create a Button named READ MORE on click the Layout of TextView
should turn to WRAP CONTENT
and READ MORE button text should be changed to SHOW LESS (this is new button which I created but set visibility GONE
in xml while READ MORE has visibility VISIBLE
)
So here is the code of mine which work awesome.. on very first click of READ MORE and also very first click of SHOW LESS but after then if again I click READ MORE just only the button gets replaced with SHOW LESS and the layout remains same.
TextView
which is inside LinearLayout
which is inside CardView
and its a fragmentXML CODE
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/view_project_description"
android:layout_width="match_parent"
android:layout_height="80dp"
android:padding="15dp"
android:text="No Description"
android:textColor="@color/black_overlay"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="12dp"
android:background="#44eaeaea" />
<Button
android:id="@+id/view_project_readmore_button"
android:layout_width="match_parent"
android:layout_height="27dp"
android:background="#55f4f4f4"
android:layout_marginLeft="75dp"
android:layout_marginRight="75dp"
android:text="Read More >>>"
android:textColor="#3143b3"
android:textSize="13sp" />
<Button
android:id="@+id/view_project_showless_button"
android:layout_width="match_parent"
android:layout_height="27dp"
android:background="#55f4f4f4"
android:layout_marginLeft="75dp"
android:layout_marginRight="75dp"
android:text="Show Less >>>"
android:textColor="#3143b3"
android:textSize="13sp"
android:visibility="gone"/>
</LinearLayout>
JAVA BUTTON CODE
final Button view_project_readmore =(Button)getView().findViewById(R.id.view_project_readmore_button);
final Button view_project_showless = (Button)getView().findViewById(R.id.view_project_showless_button);
view_project_showless.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
project_desc.setEnabled(false);
project_desc.setVisibility(View.GONE);
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (80 * scale + 0.5f);
project_desc.setHeight(500);
project_desc.setText("This is less Description Now");
project_desc.setEnabled(true);
project_desc.setVisibility(View.VISIBLE);
view_project_readmore.setVisibility(View.VISIBLE);
view_project_showless.setVisibility(View.GONE);
}
});
view_project_readmore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
project_desc.setEnabled(false);
//project_desc.setVisibility(View.GONE);
Typeface typeface=Typeface.createFromAsset(getActivity().getAssets(), "fonts/Raleway-Medium.ttf");
project_desc.setTypeface(typeface);
project_desc.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec non sapien tellus. Suspendisse non sapien nulla. Maecenas ornare velit sit amet consequat hendrerit. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec egestas dignissim enim. Nunc non egestas lacus. Aliquam rhoncus nec urna eu semper. In varius justo augue, eu tincidunt tellus ultricies nec. Pellentesque vel metus sapien.\n" +
"\n" +
"Morbi venenatis ultrices felis et sodales. Suspendisse aliquet justo nec gravida viverra. Nulla condimentum mi ac purus feugiat semper. Ut libero nunc, molestie ac dignissim quis, laoreet vel sapien. Morbi porttitor pulvinar mi, non consectetur lectus posuere ac. Morbi blandit nisl eu diam pretium, maximus mattis orci lobortis. Donec ut tincidunt erat. Donec pharetra, sapien eget mollis vestibulum, felis nisl finibus sapien, tempus laoreet turpis ipsum fermentum eros. Praesent eu nulla facilisis, aliquam massa a, hendrerit risus. Proin at mi odio. Quisque semper, nunc sit amet molestie mattis, quam orci commodo ipsum, ut congue risus dolor eget tellus. Nullam ac mauris in eros condimentum lobortis eget eget ipsum. Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
ViewGroup.LayoutParams params = project_desc.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
project_desc.setLayoutParams(params);
project_desc.setEnabled(true);
//project_desc.setVisibility(View.VISIBLE);
view_project_readmore.setVisibility(View.GONE);
view_project_showless.setVisibility(View.VISIBLE);
}
});
Upvotes: 1
Views: 75
Reputation: 89608
Setting the height through the layout params in the click listener of the show less button does the trick.
So replace this:
project_desc.setHeight(500);
With this:
ViewGroup.LayoutParams params = project_desc.getLayoutParams();
params.height = 500;
project_desc.setLayoutParams(params);
Or if you want it to be shorter, this works as well:
project_desc.getLayoutParams().height = 500;
Upvotes: 2