Set size for TextView : TextView « UI « Android






Set size for TextView

    



/***
  Copyright (c) 2008-2009 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

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>

   
    
    
    
  








Related examples in the same category

1.Add TextView to LinearLayout
2.Using ScrollView to hold a TextView
3.Set text for TextView in xml file
4.Set text for TextView
5.Add TextView and set text
6.Using AutoCompleteTextView
7.Set size for AutoCompleteTextView
8.Fill data to AutoCompleteTextView with ArrayAdapter
9.Create TextView within code
10.Set text Size, color, padding and background for TextView
11.extends TextView to create customized widget
12.RelativeLayout TextView and EditView
13.Using MultiAutoCompleteTextView
14.This is a TextView that is Editable and by default scrollable like EditText without a cursor.
15.AutoCompleteTextView 2
16.AutoCompleteTextView 3
17.Two AutoCompleteTextView
18.MultiAutoCompleteTextView Demo
19.Demonstrates using a LinearLayout background to group related TextViews, EditTexts, and Buttons.
20.Create TextView
21.Append and set text to TextView
22.toast with TextView
23.Format number
24.Auto complete Text view
25.Programmatically load text from an asset and place it into the text view.
26.extends ScrollingMovementMethod