Showing posts with label TextView. Show all posts
Showing posts with label TextView. Show all posts

Auto scrolling (horizontal running) TextView


It's follow-up post about my old post of "Implement auto-running TextView", to make a TextView horizontal scrolling automatically.

This video show how it tested on Android emulator running Android 7.0 Nougat API 24, in Multi-Window Mode also.


<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidautotextview.MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"/>

<TextView
android:id="@+id/scrollingtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:background="#D0D0D0"
android:text="http://android-er.blogspot.com/"

android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
/>

</LinearLayout>


And set it selected in Java code.
package com.blogspot.android_er.androidautotextview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView scrollingText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollingText = (TextView)findViewById(R.id.scrollingtext);
scrollingText.setSelected(true);

}
}



TextView, auto scroll down to display bottom of text


This example show how to make a TextView auto scroll down to display bottom of text. In the demonstration, the upper TextView is normal, user cannot see the bottom of text if it is full. The lower one, the TextView will auto scroll down, such that user can see the new added text.


<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidautotextview.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"/>

<EditText
android:id="@+id/textin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"/>

<Button
android:id="@+id/btnappend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Append Text"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20dp"
android:background="#D0D0D0"/>
<TextView
android:id="@+id/textout2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20dp"
android:gravity="bottom"
android:background="#E0E0E0"/>
</LinearLayout>

</LinearLayout>


package com.blogspot.android_er.androidautotextview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText editTextIn;
TextView textViewOut1, textViewOut2;
Button btnAppend;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editTextIn = (EditText)findViewById(R.id.textin);
btnAppend = (Button)findViewById(R.id.btnappend);

textViewOut1 = (TextView)findViewById(R.id.textout1);
textViewOut1.setText("It's normal TextView.\n");

textViewOut2 = (TextView)findViewById(R.id.textout2);
textViewOut2.setMovementMethod(new ScrollingMovementMethod());
textViewOut2.setText("This TextView always auto scroll down to display bottom of text.\n");


btnAppend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String textToAppend = editTextIn.getText().toString() + "\n";
textViewOut1.append(textToAppend);
textViewOut2.append(textToAppend);
editTextIn.setText("");
}
});
}
}


Create styling string from resources

This example show how to create partial styling string from resources:


Edit values/strings.xml to add string resources with styling:
<resources>
<string name="app_name">AndroidStylingStringResources</string>
<string name="normal_name">It is Normal String</string>
<string name="italic_name">Partial <i>Italic String</i> from resources</string>
<string name="bold_name">Partial <b>Bold String</b> from resources</string>
<string name="underline_name">Partial <u>Underline String</u> from resources</string>
</resources>


Edit layout/activity_main.xml to use the string resources:
<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidstylingstringresources.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:text="@string/normal_name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:textStyle="italic"
android:text="Whole TextView in italic style"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:text="@string/italic_name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:text="@string/bold_name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:text="@string/underline_name"/>

</LinearLayout>



Set Text size base on TypedValue

setTextSize (int unit, float size) set the default text size to a given unit and value. See TypedValue for the possible dimension units.

Example to set text size of TextView base on TypedValue:


MainActivity.java
package com.blogspot.android_er.androidfonts;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView text1, text2, text3, text4, text5, text6;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (TextView)findViewById(R.id.text1);
text2 = (TextView)findViewById(R.id.text2);
text3 = (TextView)findViewById(R.id.text3);
text4 = (TextView)findViewById(R.id.text4);
text5 = (TextView)findViewById(R.id.text5);
text6 = (TextView)findViewById(R.id.text6);

text1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
text1.setText("20 DIP (Device Independent Pixels)");

text2.setTextSize(TypedValue.COMPLEX_UNIT_IN, 0.5f);
text2.setText("0.5 inch");

text3.setTextSize(TypedValue.COMPLEX_UNIT_MM, 10);
text3.setText("10 millimeter");

text4.setTextSize(TypedValue.COMPLEX_UNIT_PT, 30);
text4.setText("30 points");

text5.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
text5.setText("30 raw pixels");

text6.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
text6.setText("30 scaled pixels");
}
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidfonts.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>