Set padding with dpi value : LinearLayout « UI « Android






Set padding with dpi value

   


/***
  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.webkit.WebView;
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>();
  private WebView browser=null;
  
  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);
    
    browser=(WebView)findViewById(R.id.browser);
    
    setListAdapter(new CountryAdapter());
  }
  
  @Override
  protected void onListItemClick(ListView l, View v,
                                 int position, long id) {
    String url=getString(EU.get(position).url);
    
    if (browser==null) {
      startActivity(new Intent(Intent.ACTION_VIEW,
                                Uri.parse(url)));
    }
    else {
      browser.loadUrl(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\layout-large-land\main.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="fill_parent"
>    
  <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_weight="1"
  />
  <WebView
    android:id="@+id/browser"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_weight="1"
  />
</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>



Auto Complete Demo
/***
  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.auto;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

public class AutoCompleteDemo extends Activity
  implements TextWatcher {
  TextView selection;
  AutoCompleteTextView edit;
  String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
          "consectetuer", "adipiscing", "elit", "morbi", "vel",
          "ligula", "vitae", "arcu", "aliquet", "mollis",
          "etiam", "vel", "erat", "placerat", "ante",
          "porttitor", "sodales", "pellentesque", "augue", "purus"};

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    selection=(TextView)findViewById(R.id.selection);
    edit=(AutoCompleteTextView)findViewById(R.id.edit);
    edit.addTextChangedListener(this);
    
    edit.setAdapter(new ArrayAdapter<String>(this,
                          android.R.layout.simple_dropdown_item_1line,
                          items));
  }
  
  public void onTextChanged(CharSequence s, int start, int before,
                              int count) {
    selection.setText(edit.getText());
  }
  
  public void beforeTextChanged(CharSequence s, int start,
                                  int count, int after) {
    // needed for interface, but not used
  }
  
  public void afterTextChanged(Editable s) {
    // needed for interface, but not used
  }
}


//res\layout\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/selection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
  <AutoCompleteTextView android:id="@+id/edit"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:completionThreshold="3"/>
</LinearLayout>

   
    
    
  








Related examples in the same category

1.Adding two controls to LinearLayout
2.Using LinearLayout for Activity
3.Using LinearLayout to layout two RadioGroups
4.Using LinearLayout.LayoutParams
5.LinearLayout for ListAdapter
6.Using LinearLayout
7.LinearLayout which uses a combination of wrap_content on itself and match_parent on its children to get every item to be the same width.
8.LinearLayout inside ScrollView
9.Transparent Panel extends LinearLayout
10.Set min height
11.Set padding right
12.Baseline alignment includes a android.widget.LinearLayout within another android.widget.LinearLayout.