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

Monday, August 29, 2016

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);

    }
}



Saturday, August 27, 2016

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("");
            }
        });
    }
}


Wednesday, July 20, 2016

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>



Tuesday, March 22, 2016

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>


Saturday, December 26, 2015

Make TextView selectable, select and copy


Example to make TextView selectable, you can select and copy text from TextView and paste to other field, such as EditText.


Add android:textIsSelectable="true" in layout 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.androidselectabletext.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/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28sp"
        android:text="Default TextView, un-selectable"/>

    <TextView
        android:id="@+id/textview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="true"
        android:textSize="28sp"
        android:text="Selectable TextView, by XML"/>

    <TextView
        android:id="@+id/textview3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28sp"
        android:text="Selectable TextView, by Java code"/>

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28sp"/>
</LinearLayout>


or call setTextIsSelectable(true) method of the TextView object in Java code:
package com.blogspot.android_er.androidselectabletext;

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

public class MainActivity extends AppCompatActivity {

    TextView textView1, textView2, textView3;
    EditText editText;

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

        textView1 = (TextView)findViewById(R.id.textview1);
        textView2 = (TextView)findViewById(R.id.textview2);
        textView3 = (TextView)findViewById(R.id.textview3);
        textView3.setTextIsSelectable(true);
        editText = (EditText)findViewById(R.id.edittext);
    }
}