Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe the code below will do what you want but I can't see a way to do this only in the xml layout. Basically I have added a textChangedListener to two different TextViews that have the two different layout options I believe you are looking for, both inside their own relative layout with the date displaying textview. When the subtitle is set the first of these is used to hold the text, if it requires more than a single line the second TextView is used and in either case the other has its visibility option set to GONE. In my example I use a seperate thread to change the subtitle, hopefully this doesn't confuse things too much.</p> <p>The layout xml is as follows:</p> <pre><code> &lt;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: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" &gt; &lt;TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:ellipsize="end" android:textAppearance="?android:attr/textAppearanceSearchResultTitle" android:background="#FF009999" android:textIsSelectable="false" android:text="@string/my_title" /&gt; &lt;RelativeLayout android:id="@+id/resizingTextContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/title" &gt; &lt;TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle" android:textIsSelectable="false" android:textColor="#FFFFFFFF" android:background="#FF000000" /&gt; &lt;TextView android:id="@+id/subtitle1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/date" android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle" android:textIsSelectable="false" android:textColor="#FFFFFF" android:background="#FF00FF00" /&gt; &lt;TextView android:id="@+id/subtitle2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/date" android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle" android:textIsSelectable="false" android:visibility="gone" android:textColor="#FFFFFF" android:background="#FF00FF00" /&gt; &lt;/RelativeLayout&gt; &lt;Button android:id="@+id/startTestBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/resizingTextContainer" android:layout_centerHorizontal="true" android:text="Click To Begin" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>And the main activity code with the textview switching logic:</p> <pre><code> package com.example.code.examples.changelayoutwithtextlength; import android.os.Bundle; import android.app.Activity; import android.text.Editable; import android.text.TextWatcher; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { TextWatcher textChangeDisplayCheck = new TextWatcher(){ @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { displayLatestSubtitle(s); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView DateTextView = (TextView)findViewById(R.id.date); DateTextView.setText("29th April 2013"); Button b = (Button)findViewById(R.id.startTestBtn); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { v.setEnabled(false); Thread thread = new testSubtitleThread(); thread.start(); } }); TextView tv = (TextView)findViewById(R.id.subtitle1); tv.addTextChangedListener(textChangeDisplayCheck); TextView tv2 = (TextView)findViewById(R.id.subtitle2); tv2.addTextChangedListener(textChangeDisplayCheck); } private void displayLatestSubtitle(CharSequence newSubtitle) { TextView tv = (TextView)findViewById(R.id.subtitle1); TextView tv2 = (TextView)findViewById(R.id.subtitle2); tv.removeTextChangedListener(textChangeDisplayCheck); tv2.removeTextChangedListener(textChangeDisplayCheck); tv.setVisibility(View.GONE); tv2.setVisibility(View.GONE); tv2.setText(""); tv.setText(newSubtitle); if(tv.getLineCount() &gt; 1) { tv.setText(""); tv2.setText(newSubtitle); tv2.setVisibility(View.VISIBLE); } else { tv.setVisibility(View.VISIBLE); } tv.addTextChangedListener(textChangeDisplayCheck); tv2.addTextChangedListener(textChangeDisplayCheck); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public class testSubtitleThread extends Thread { String[] subtitles = new String[] { "a short one", "a really long winded subtitle that will take over more than the allowed space", "tiny", "Really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, long.", ".....", "text just to long to fit on my device"}; private android.os.Handler handler = new android.os.Handler() { @Override public void handleMessage(android.os.Message msg) { if(msg.what &lt; subtitles.length) { TextView tv = (TextView)findViewById(R.id.subtitle1); tv.setText(subtitles[msg.what]); } else { Button b = (Button)findViewById(R.id.startTestBtn); b.setEnabled(true); } } }; @Override public void run() { for(int i = 0; i &lt;= subtitles.length; i++) { handler.sendEmptyMessage(i); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } </code></pre> <p>I hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload