Android Tutorial - UI TextView
Add TextView to LinearLayout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/directions" /> </LinearLayout>
Using ScrollView to hold a TextView
package app.Test; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.TextView; public class appTest extends Activity { private TextView urlText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); urlText = (TextView) findViewById(R.id.text); urlText.setOnKeyListener(new OnKeyListener() { public boolean onKey(View view, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_ENTER) { return true; } return false; } }); } } //main.xml <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </ScrollView>
Set text for TextView in xml file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Please enter your home address." /> <EditText android:id="@+id/address" android:layout_width="fill_parent" android:layout_height="wrap_content" android:autoText="true" /> <Button android:id="@+id/launchmap" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Map" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Unlocking Android" /> </LinearLayout>
Set text for TextView
package app.test; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Test extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("5! = " + factorial(5)); setContentView(tv); } public static long factorial(long n) { if (n <= 0) return 1; else return n * factorial(n - 1); } }
Add TextView and set text
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="this is a test." /> package app.test; import android.app.Activity; import android.os.Bundle; public class Test extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); } }
Set size for TextView
package com.commonsware.android.eu4you; import android.app.ListActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; public class EU4You extends ListActivity { static private ArrayList<Country> EU=new ArrayList<Country>(); static { EU.add(new Country(R.string.austria, R.drawable.austria, R.string.austria_url)); EU.add(new Country(R.string.belgium, R.drawable.belgium, R.string.belgium_url)); EU.add(new Country(R.string.bulgaria, R.drawable.bulgaria, R.string.bulgaria_url)); EU.add(new Country(R.string.cyprus, R.drawable.cyprus, R.string.cyprus_url)); EU.add(new Country(R.string.czech_republic, R.drawable.czech_republic, R.string.czech_republic_url)); EU.add(new Country(R.string.denmark, R.drawable.denmark, R.string.denmark_url)); EU.add(new Country(R.string.estonia, R.drawable.estonia, R.string.estonia_url)); EU.add(new Country(R.string.finland, R.drawable.finland, R.string.finland_url)); EU.add(new Country(R.string.france, R.drawable.france, R.string.france_url)); EU.add(new Country(R.string.germany, R.drawable.germany, R.string.germany_url)); EU.add(new Country(R.string.greece, R.drawable.greece, R.string.greece_url)); EU.add(new Country(R.string.hungary, R.drawable.hungary, R.string.hungary_url)); EU.add(new Country(R.string.ireland, R.drawable.ireland, R.string.ireland_url)); EU.add(new Country(R.string.italy, R.drawable.italy, R.string.italy_url)); EU.add(new Country(R.string.latvia, R.drawable.latvia, R.string.latvia_url)); EU.add(new Country(R.string.lithuania, R.drawable.lithuania, R.string.lithuania_url)); EU.add(new Country(R.string.luxembourg, R.drawable.luxembourg, R.string.luxembourg_url)); EU.add(new Country(R.string.malta, R.drawable.malta, R.string.malta_url)); EU.add(new Country(R.string.netherlands, R.drawable.netherlands, R.string.netherlands_url)); EU.add(new Country(R.string.poland, R.drawable.poland, R.string.poland_url)); EU.add(new Country(R.string.portugal, R.drawable.portugal, R.string.portugal_url)); EU.add(new Country(R.string.romania, R.drawable.romania, R.string.romania_url)); EU.add(new Country(R.string.slovakia, R.drawable.slovakia, R.string.slovakia_url)); EU.add(new Country(R.string.slovenia, R.drawable.slovenia, R.string.slovenia_url)); EU.add(new Country(R.string.spain, R.drawable.spain, R.string.spain_url)); EU.add(new Country(R.string.sweden, R.drawable.sweden, R.string.sweden_url)); EU.add(new Country(R.string.united_kingdom, R.drawable.united_kingdom, R.string.united_kingdom_url)); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setListAdapter(new CountryAdapter()); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(EU.get(position).url)))); } static class Country { int name; int flag; int url; Country(int name, int flag, int url) { this.name=name; this.flag=flag; this.url=url; } } class CountryAdapter extends ArrayAdapter<Country> { CountryAdapter() { super(EU4You.this, R.layout.row, R.id.name, EU); } @Override public View getView(int position, View convertView, ViewGroup parent) { CountryWrapper wrapper=null; if (convertView==null) { convertView=getLayoutInflater().inflate(R.layout.row, null); wrapper=new CountryWrapper(convertView); convertView.setTag(wrapper); } else { wrapper=(CountryWrapper)convertView.getTag(); } wrapper.populateFrom(getItem(position)); return(convertView); } } class CountryWrapper { private TextView name=null; private ImageView flag=null; private View row=null; CountryWrapper(View row) { this.row=row; } TextView getName() { if (name==null) { name=(TextView)row.findViewById(R.id.name); } return(name); } ImageView getFlag() { if (flag==null) { flag=(ImageView)row.findViewById(R.id.flag); } return(flag); } void populateFrom(Country nation) { getName().setText(nation.name); getFlag().setImageResource(nation.flag); } } } //res\layout\main.xml <?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> //res\layout\row.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="2dip" android:minHeight="?android:attr/listPreferredItemHeight" > <ImageView android:id="@+id/flag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|left" android:paddingRight="4px" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|right" android:textSize="5mm" /> </LinearLayout> //res\values\strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">EU4You</string> <string name="austria">Austria</string> <string name="belgium">Belgium</string> <string name="bulgaria">Bulgaria</string> <string name="cyprus">Cyprus</string> <string name="czech_republic">Czech Republic</string> <string name="denmark">Denmark</string> <string name="estonia">Estonia</string> <string name="finland">Finland</string> <string name="france">France</string> <string name="germany">Germany</string> <string name="greece">Greece</string> <string name="hungary">Hungary</string> <string name="ireland">Ireland</string> <string name="italy">Italy</string> <string name="latvia">Latvia</string> <string name="lithuania">Lithuania</string> <string name="luxembourg">Luxembourg</string> <string name="malta">Malta</string> <string name="netherlands">Netherlands</string> <string name="poland">Poland</string> <string name="portugal">Portugal</string> <string name="romania">Romania</string> <string name="slovakia">Slovakia</string> <string name="slovenia">Slovenia</string> <string name="spain">Spain</string> <string name="sweden">Sweden</string> <string name="united_kingdom">United Kingdom</string> <string name="austria_url">http://en.m.wikipedia.org/wiki/Austria</string> <string name="belgium_url">http://en.m.wikipedia.org/wiki/Belgium</string> <string name="bulgaria_url">http://en.m.wikipedia.org/wiki/Bulgaria</string> <string name="cyprus_url">http://en.m.wikipedia.org/wiki/Cyprus</string> <string name="czech_republic_url">http://en.m.wikipedia.org/wiki/Czech_republic</string> <string name="denmark_url">http://en.m.wikipedia.org/wiki/Denmark</string> <string name="estonia_url">http://en.m.wikipedia.org/wiki/Estonia</string> <string name="finland_url">http://en.m.wikipedia.org/wiki/Finland</string> <string name="france_url">http://en.m.wikipedia.org/wiki/France</string> <string name="germany_url">http://en.m.wikipedia.org/wiki/Germany</string> <string name="greece_url">http://en.m.wikipedia.org/wiki/Greece</string> <string name="hungary_url">http://en.m.wikipedia.org/wiki/Hungary</string> <string name="ireland_url">http://en.m.wikipedia.org/wiki/Ireland</string> <string name="italy_url">http://en.m.wikipedia.org/wiki/Italy</string> <string name="latvia_url">http://en.m.wikipedia.org/wiki/Latvia</string> <string name="lithuania_url">http://en.m.wikipedia.org/wiki/Lithuania</string> <string name="luxembourg_url">http://en.m.wikipedia.org/wiki/Luxembourg</string> <string name="malta_url">http://en.m.wikipedia.org/wiki/Malta</string> <string name="netherlands_url">http://en.m.wikipedia.org/wiki/Netherlands</string> <string name="poland_url">http://en.m.wikipedia.org/wiki/Poland</string> <string name="portugal_url">http://en.m.wikipedia.org/wiki/Portugal</string> <string name="romania_url">http://en.m.wikipedia.org/wiki/Romania</string> <string name="slovakia_url">http://en.m.wikipedia.org/wiki/Slovakia</string> <string name="slovenia_url">http://en.m.wikipedia.org/wiki/Slovenia</string> <string name="spain_url">http://en.m.wikipedia.org/wiki/Spain</string> <string name="sweden_url">http://en.m.wikipedia.org/wiki/Sweden</string> <string name="united_kingdom_url">http://en.m.wikipedia.org/wiki/United_kingdom</string> </resources>
Using AutoCompleteTextView
package app.test; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class Test extends Activity { String[] presidents = { "Dwight D. Eisenhower", "John F. Kennedy", "Lyndon B. Johnson", "Richard Nixon", "Gerald Ford", "Jimmy Carter", "Ronald Reagan", "George H. W. Bush", "Bill Clinton", "George W. Bush", "Barack Obama" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, presidents); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries); textView.setThreshold(3); textView.setAdapter(adapter); } } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Name of President" /> <AutoCompleteTextView android:id="@+id/txtCountries" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
Set size for AutoCompleteTextView
package app.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.graphics.Color; public class Test extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); ArrayAdapter<String> monthArray = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Months); final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.testAutoComplete); textView.setAdapter(monthArray); final Button changeButton = (Button) findViewById(R.id.autoCompleteButton); changeButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { changeOption(textView); } }); final Button changeButton2 = (Button) findViewById(R.id.textColorButton); changeButton2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { changeOption2(textView); } }); } static final String[] Months = new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; public void changeOption(AutoCompleteTextView text) { if (text.getHeight() == 100) { text.setHeight(30); } else { text.setHeight(100); } } public void changeOption2(AutoCompleteTextView text) { text.setTextColor(Color.RED); } } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <AutoCompleteTextView android:id="@+id/testAutoComplete" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/autoCompleteButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Change Layout"/> <Button android:id="@+id/textColorButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Change Text Color"/> </LinearLayout>
Fill data to AutoCompleteTextView with ArrayAdapter
package app.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.graphics.Color; public class Test extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); ArrayAdapter<String> monthArray = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Months); final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.testAutoComplete); textView.setAdapter(monthArray); final Button changeButton = (Button) findViewById(R.id.autoCompleteButton); changeButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { changeOption(textView); } }); final Button changeButton2 = (Button) findViewById(R.id.textColorButton); changeButton2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { changeOption2(textView); } }); } static final String[] Months = new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; public void changeOption(AutoCompleteTextView text) { if (text.getHeight() == 100) { text.setHeight(30); } else { text.setHeight(100); } } public void changeOption2(AutoCompleteTextView text) { text.setTextColor(Color.RED); } } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <AutoCompleteTextView android:id="@+id/testAutoComplete" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/autoCompleteButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Change Layout"/> <Button android:id="@+id/textColorButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Change Text Color"/> </LinearLayout>
Create TextView within code
package app.test; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Test extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = new TextView(this); tv.setText("Android"); setContentView(tv); } }
Set text Size, color, padding and background for TextView
package app.test; import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.TextView; public class Test extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("ViewTextActivity"); setContentView(R.layout.main); find_and_modify_text_view(); } private void find_and_modify_text_view() { TextView text_view = (TextView) findViewById(R.id.text_view); CharSequence text_view_old = text_view.getText(); text_view.setText(text_view_old); } } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/text_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="#ffffff" android:padding="10dip" android:background="#cc0000" android:text="TextView" /> </LinearLayout>
extends TextView to create customized widget
package app.test; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; class TodoListItemView extends TextView { private Paint marginPaint; private Paint linePaint; private int paperColor; private float margin; public TodoListItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG); marginPaint.setColor(Color.RED); linePaint = new Paint(Paint.ANTI_ALIAS_FLAG); linePaint.setColor(Color.BLUE); paperColor = Color.YELLOW; margin = Color.CYAN; } @Override public void onDraw(Canvas canvas) { canvas.drawColor(paperColor); canvas.drawLine(0, 0, getMeasuredHeight(), 0, linePaint); canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint); canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint); canvas.save(); canvas.translate(margin, 0); super.onDraw(canvas); canvas.restore(); } } public class Test extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); ListView myListView = (ListView) findViewById(R.id.myListView); final EditText myEditText = (EditText) findViewById(R.id.myEditText); final ArrayList<String> todoItems = new ArrayList<String>(); int resID = R.layout.row; final ArrayAdapter<String> aa = new ArrayAdapter<String>(this, resID, todoItems); myListView.setAdapter(aa); myEditText.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { todoItems.add(0, myEditText.getText().toString()); myEditText.setText(""); aa.notifyDataSetChanged(); return true; } return false; } }); } } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:id="@+id/myEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="New To Do Item" /> <ListView android:id="@+id/myListView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> //row.xml <?xml version="1.0" encoding="utf-8"?> <app.test.TodoListItemView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:scrollbars="vertical" android:fadingEdge="vertical" />
RelativeLayout TextView and EditView
package app.test; import android.app.Activity; import android.os.Bundle; public class Test extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } //main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/userNameLbl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Username:" android:layout_alignParentTop="true" /> <EditText android:id="@+id/userNameText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/userNameLbl" /> <TextView android:id="@+id/pwdLbl" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/userNameLbl" android:text="Password:" /> <EditText android:id="@+id/pwdText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/pwdLbl" /> <TextView android:id="@+id/pwdCriteria" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/pwdText" android:text="Password Criteria... " /> <TextView android:id="@+id/disclaimerLbl" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Use at your own risk... " /> </RelativeLayout>
Using MultiAutoCompleteTextView
package app.test; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.EditText; import android.widget.MultiAutoCompleteTextView; import android.widget.TextView; public class Test extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv =(TextView)findViewById(R.id.tv); EditText et = (EditText)findViewById(R.id.et); AutoCompleteTextView actv = (AutoCompleteTextView)findViewById(R.id.actv); ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, new String[] {"English", "Hebrew", "Hindi", "Spanish", "German", "Greek" }); actv.setAdapter(aa); MultiAutoCompleteTextView mactv = (MultiAutoCompleteTextView)findViewById(R.id.mactv); ArrayAdapter<String> aa2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, new String[] {"English", "Hebrew", "Hindi", "Spanish", "German", "Greek" }); mactv.setAdapter(aa2); mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } }
This is a TextView that is Editable and by default scrollable like EditText without a cursor.
import android.widget.TextView;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.text.method.MovementMethod;
import android.text.Editable;
import android.util.AttributeSet;
/**
* This is a TextView that is Editable and by default scrollable,
* like EditText without a cursor.
*
* <p>
* <b>XML attributes</b>
* <p>
* See
* {@link android.R.styleable#TextView TextView Attributes},
* {@link android.R.styleable#View View Attributes}
*/
class LogTextBox extends TextView {
public LogTextBox(Context context) {
this(context, null);
}
public LogTextBox(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
public LogTextBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected MovementMethod getDefaultMovementMethod() {
return ScrollingMovementMethod.getInstance();
}
@Override
public Editable getText() {
return (Editable) super.getText();
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, BufferType.EDITABLE);
}
}
package com.example.android.apis.text;
import com.example.android.apis.R;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.widget.TextView;
public class Link extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.link);
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
TextView t3 = (TextView) findViewById(R.id.text3);
t3.setText(
Html.fromHtml(
"<b>text3:</b> Text with a " +
"<a href=\"http://www.google.com\">link</a> " +
"created in the Java source code using HTML."));
t3.setMovementMethod(LinkMovementMethod.getInstance());
SpannableString ss = new SpannableString(
"text4: Click here to dial the phone.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView t4 = (TextView) findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());
}
}
package com.example.android.apis.text;
import com.example.android.apis.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Using a LogTextBox to display a scrollable text area
* to which text is appended.
*
*/
public class LogTextBox1 extends Activity {
private LogTextBox mText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_text_box_1);
mText = (LogTextBox) findViewById(R.id.text);
Button addButton = (Button) findViewById(R.id.add);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mText.append("This is a test\n");
} });
}
}
itations under the License.
*/
package com.example.android.apis.text;
import com.example.android.apis.R;
import android.app.Activity;
import android.os.Bundle;
public class Marquee extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marquee);
}
}
//layout/marqee.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="@string/marquee_default"
android:singleLine="true"
android:ellipsize="marquee"/>
<Button
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="@string/marquee_once"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="1"/>
<Button
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="@string/marquee_forever"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"/>
</LinearLayout>
//layout/link.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="all"
android:text="@string/link_text_auto"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/link_text_manual"
/>
<!-- text3 builds the text in the Java code using HTML. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- text4 builds the text in the Java code without using HTML. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text4"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
//layout/log_text_box_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/log_text_box_1_add_text"/>
<com.example.android.apis.text.LogTextBox
android:id="@+id/text"
android:background="@drawable/box"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:scrollbars="vertical"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/log_text_box_1_do_nothing_text"/>
</LinearLayout>AutoCompleteTextView 2
package app.test; import android.app.Activity; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.os.Bundle; public class Test extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit); textView.setAdapter(adapter); } static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland", "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas", "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe" }; } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_1_instructions" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_1_country" /> <AutoCompleteTextView android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_1_focus" /> </LinearLayout>
AutoCompleteTextView 3
package app.test; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class Test extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit); textView.setAdapter(adapter); } static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland", "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas", "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe" }; } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_2_focus" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_2_country" /> <AutoCompleteTextView android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
Two AutoCompleteTextView
package app.test; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class Test extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit); textView.setAdapter(adapter); textView = (AutoCompleteTextView) findViewById(R.id.edit2); textView.setAdapter(adapter); } static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland", "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas", "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe" }; } //main.xml <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_5" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_6" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_7" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_8" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_5" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_6" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_7" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button_8" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_country" /> <AutoCompleteTextView android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_button" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_3_country" /> <AutoCompleteTextView android:id="@+id/edit2" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </ScrollView>
MultiAutoCompleteTextView Demo
package app.test; import android.app.Activity; import android.widget.ArrayAdapter; import android.widget.MultiAutoCompleteTextView; import android.os.Bundle; public class Test extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES); MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.edit); textView.setAdapter(adapter); textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland", "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas", "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe" }; } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_7_instructions" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_7_country" /> <MultiAutoCompleteTextView android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="autocomplete_7_focus" /> </LinearLayout>
Demonstrates using a LinearLayout background to group related TextViews, EditTexts, and Buttons.
package com.example.android.apis.view; // Need the following import to get access to the app resources, since this // class is in a sub-package. import com.example.android.apis.R; import android.app.Activity; import android.os.Bundle; /** * Demonstrates the use of LinearLayout backgrounds to group labels, * EditTexts, and buttons, */ public class LinearLayout10 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.linear_layout_10); } } //layout/linear_layout_10.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:addStatesFromChildren="true" android:gravity="center_vertical" android:paddingRight="0dip" android:background="@android:drawable/edit_text"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/linear_layout_10_from" android:textColor="?android:attr/textColorSecondary" android:textAppearance="?android:attr/textAppearanceLargeInverse" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" android:background="@null" /> <ImageButton style="@android:style/Widget.Button.Inset" android:src="@android:drawable/star_big_on" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="2dip" android:layout_marginRight="2dip" android:layout_marginBottom="2dip" android:padding="10dip" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:addStatesFromChildren="true" android:gravity="center_vertical" android:paddingRight="0dip" android:background="@android:drawable/edit_text"> <!-- TextView label goes at the left. --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/linear_layout_10_to" android:textColor="?android:attr/textColorSecondary" android:textAppearance="?android:attr/textAppearanceLargeInverse" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" android:background="@null" /> <ImageButton style="@android:style/Widget.Button.Inset" android:src="@android:drawable/star_big_on" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="2dip" android:layout_marginRight="2dip" android:layout_marginBottom="2dip" android:padding="10dip" /> </LinearLayout> </LinearLayout>
//package com.akjava.lib.android.ui; import java.io.BufferedInputStream; import java.io.InputStream; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; class WidgetUtil { public static TextView createText(Context context, String text) { TextView bt=new TextView(context); bt.setText(text); return bt; } public static TextView createText(Context context, int resid) { TextView bt=new TextView(context); bt.setText(resid); return bt; } }
Append and set text to TextView
import android.view.MenuItem; import android.widget.TextView; class TextViewUtil { public static void appendText(TextView tv, String text){ tv.setText(tv.getText() + text); } public static void appendMenuItemText(TextView tv, MenuItem menuItem){ String title = menuItem.getTitle().toString(); tv.setText(tv.getText()+ "_n" + title); } public static void emptyText(TextView tv){ tv.setText(""); } public static void setText(TextView tv, String text){ tv.setText(text); } }
toast with TextView
import android.content.Context; import android.graphics.Color; import android.graphics.Typeface; import android.view.Gravity; import android.widget.TextView; import android.widget.Toast; class ToastForAPP { public static void toastNow(String message,Context context) { Toast toast = Toast.makeText(context,message, Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2); TextView textView = new TextView(context); textView.setBackgroundColor(Color.DKGRAY); textView.setTextColor(Color.WHITE); textView.setTextSize(30); Typeface typeface = Typeface.create("serif", Typeface.BOLD); textView.setTypeface(typeface); textView.setPadding(10, 10, 10, 10); textView.setText(message); toast.setView(textView); toast.show(); } }
Format number
package app.test; import java.text.NumberFormat; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Test extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); final EditText myEditField = (EditText) findViewById(R.id.mealprice); final TextView answerfield = (TextView) findViewById(R.id.answer); final Button button = (Button) findViewById(R.id.calculate); button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { try { String mealprice = myEditField.getText().toString(); String answer = ""; if (mealprice.indexOf("$") == -1) { mealprice = "$" + mealprice; } NumberFormat nf = java.text.NumberFormat.getCurrencyInstance(); if (nf == null) { Log.i("", "NumberFormat is null"); } float fmp = nf.parse(mealprice).floatValue(); fmp *= 2; Log.i("", "Total:" + fmp); answer = "Full Price:" + nf.format(fmp); answerfield.setText(answer); } catch (java.text.ParseException pe) { Log.i("", "Parse exception caught"); answerfield.setText("Failed to parse amount?"); } catch (Exception e) { Log.e("", "Failed to Calculate Tip:" + e.getMessage()); e.printStackTrace(); answerfield.setText(e.getMessage()); } } }); } } // main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Calculator" /> <EditText android:id="@+id/mealprice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:autoText="true" /> <Button android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calculate Tip" /> <TextView android:id="@+id/answer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> </LinearLayout>
Auto complete Text view
package app.test; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class Test extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setTitle("AutoCompleteTextViewActivity"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.auto_complete); textView.setAdapter(adapter); } static final String[] COUNTRIES = new String[] { "China", "Russia", "Germany", "Ukraine", "Belarus", "USA", "China1", "China12", "Germany", "Russia2", "Belarus", "USA" }; } //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <AutoCompleteTextView android:id="@+id/auto_complete" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
Programmatically load text from an asset and place it into the text view.
package com.example.android.apis.content; // Need the following import to get access to the app resources, since this // class is in a sub-package. import com.example.android.apis.R; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import java.io.IOException; import java.io.InputStream; /** * Demonstration of styled text resources. */ public class ReadAsset extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // See assets/res/any/layout/styled_text.xml for this // view layout definition. setContentView(R.layout.read_asset); // Programmatically load text from an asset and place it into the // text view. Note that the text we are loading is ASCII, so we // need to convert it to UTF-16. try { InputStream is = getAssets().open("read_asset.txt"); // We guarantee that the available method returns the total // size of the asset... of course, this does mean that a single // asset can't be more than 2 gigs. int size = is.available(); // Read the entire asset into a local byte buffer. byte[] buffer = new byte[size]; is.read(buffer); is.close(); // Convert the buffer into a string. String text = new String(buffer); // Finally stick the string into the text view. TextView tv = (TextView)findViewById(R.id.text); tv.setText(text); } catch (IOException e) { // Should never happen! throw new RuntimeException(e); } } } //layout/read_asset.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textStyle="normal"/> </LinearLayout>
import android.text.Layout; import android.text.NoCopySpan; import android.text.Selection; import android.text.Spannable; import android.text.method.LinkMovementMethod; import android.text.method.MovementMethod; import android.text.method.ScrollingMovementMethod; import android.text.style.ClickableSpan; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; public class MyLinkMovementMethod extends ScrollingMovementMethod { private static final int CLICK = 1; private static final int UP = 2; private static final int DOWN = 3; @Override public boolean onKeyDown(TextView widget, Spannable buffer, int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: case KeyEvent.KEYCODE_ENTER: if (event.getRepeatCount() == 0) { if (action(CLICK, widget, buffer)) { return true; } } } return super.onKeyDown(widget, buffer, keyCode, event); } @Override protected boolean up(TextView widget, Spannable buffer) { if (action(UP, widget, buffer)) { return true; } return super.up(widget, buffer); } @Override protected boolean down(TextView widget, Spannable buffer) { if (action(DOWN, widget, buffer)) { return true; } return super.down(widget, buffer); } @Override protected boolean left(TextView widget, Spannable buffer) { if (action(UP, widget, buffer)) { return true; } return super.left(widget, buffer); } @Override protected boolean right(TextView widget, Spannable buffer) { if (action(DOWN, widget, buffer)) { return true; } return super.right(widget, buffer); } private boolean action(int what, TextView widget, Spannable buffer) { boolean handled = false; Layout layout = widget.getLayout(); int padding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom(); int areatop = widget.getScrollY(); int areabot = areatop + widget.getHeight() - padding; int linetop = layout.getLineForVertical(areatop); int linebot = layout.getLineForVertical(areabot); int first = layout.getLineStart(linetop); int last = layout.getLineEnd(linebot); ClickableSpan[] candidates = buffer.getSpans(first, last, ClickableSpan.class); int a = Selection.getSelectionStart(buffer); int b = Selection.getSelectionEnd(buffer); int selStart = Math.min(a, b); int selEnd = Math.max(a, b); if (selStart < 0) { if (buffer.getSpanStart(FROM_BELOW) >= 0) { selStart = selEnd = buffer.length(); } } if (selStart > last) selStart = selEnd = Integer.MAX_VALUE; if (selEnd < first) selStart = selEnd = -1; switch (what) { case CLICK: if (selStart == selEnd) { return false; } ClickableSpan[] link = buffer.getSpans(selStart, selEnd, ClickableSpan.class); if (link.length != 1) return false; link[0].onClick(widget); break; case UP: int beststart, bestend; beststart = -1; bestend = -1; for (int i = 0; i < candidates.length; i++) { int end = buffer.getSpanEnd(candidates[i]); if (end < selEnd || selStart == selEnd) { if (end > bestend) { beststart = buffer.getSpanStart(candidates[i]); bestend = end; } } } if (beststart >= 0) { Selection.setSelection(buffer, bestend, beststart); return true; } break; case DOWN: beststart = Integer.MAX_VALUE; bestend = Integer.MAX_VALUE; for (int i = 0; i < candidates.length; i++) { int start = buffer.getSpanStart(candidates[i]); if (start > selStart || selStart == selEnd) { if (start < beststart) { beststart = start; bestend = buffer.getSpanEnd(candidates[i]); } } } if (bestend < Integer.MAX_VALUE) { Selection.setSelection(buffer, beststart, bestend); return true; } break; } return false; } public boolean onKeyUp(TextView widget, Spannable buffer, int keyCode, KeyEvent event) { return false; } @Override public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= widget.getTotalPaddingLeft(); y -= widget.getTotalPaddingTop(); x += widget.getScrollX(); y += widget.getScrollY(); Layout layout = widget.getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class); if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { link[0].onClick(widget); } else if (action == MotionEvent.ACTION_DOWN) { Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0])); } return true; } else { Selection.removeSelection(buffer); } } return super.onTouchEvent(widget, buffer, event); } public void initialize(TextView widget, Spannable text) { Selection.removeSelection(text); text.removeSpan(FROM_BELOW); } public void onTakeFocus(TextView view, Spannable text, int dir) { Selection.removeSelection(text); if ((dir & View.FOCUS_BACKWARD) != 0) { text.setSpan(FROM_BELOW, 0, 0, Spannable.SPAN_POINT_POINT); } else { text.removeSpan(FROM_BELOW); } } public static MovementMethod getInstance() { if (sInstance == null) sInstance = new LinkMovementMethod(); return sInstance; } private static LinkMovementMethod sInstance; private static Object FROM_BELOW = new NoCopySpan.Concrete(); }