Android_800_thumb800

Android’s common controls code examples

Text Controls

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.trankyphat.app.uicontrol_textview.MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvtext1" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:ems="10"
        android:hint="@string/edittexthint" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="34dp"
        android:text="@string/tvtext2" />

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="29dp"
        android:ems="10"
        android:hint="@string/AutoCompleteHintText1" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/autoCompleteTextView1"
        android:layout_below="@+id/autoCompleteTextView1"
        android:text="@string/tvtext3" />

    <MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_centerVertical="true"
        android:ems="10"
        android:hint="@string/MultiAutoCompleteHintText1" />
</RelativeLayout>

MainActivity.java

package com.trankyphat.app.uicontrol_textview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

    private static final String[] COUNTRIES = new String[] {
            "Belgium", "France", "Italy", "Germany", "Spain"
    };


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

        //One AutoComplete TextView
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);
        AutoCompleteTextView mytextView = (AutoCompleteTextView)
                findViewById(R.id.autoCompleteTextView1);
        mytextView.setAdapter(adapter);

        //Multiple Auto Complete textview
        MultiAutoCompleteTextView mactv = (MultiAutoCompleteTextView) this
                .findViewById(R.id.multiAutoCompleteTextView1);
        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());
    }

}

Button Controls

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.trankyphat.app.uicontrol_button.MainActivity">

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginTop="19dp"
        android:src="@mipmap/ic_launcher" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton1"
        android:layout_alignRight="@+id/imageButton1"
        android:layout_below="@+id/imageButton1"
        android:layout_marginTop="20dp"
        android:text="Stop" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton1"
        android:layout_alignParentTop="true"
        android:onClick="myClickHandler"
        android:text="Basic Button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

MainActivity.java

package com.trankyphat.app.uicontrol_button;

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

public class MainActivity extends AppCompatActivity {

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

    public void myClickHandler(View target) {
        switch (target.getId()) {
            case R.id.button1:
                TextView myViewText = (TextView) this.findViewById(R.id.textView);
                myViewText.setText("Clicked");
                break;
            default:
                break;
        }
    }
}

CheckBox Control

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.trankyphat.app.uicontrol_checkbox.MainActivity">

    <CheckBox android:id="@+id/chickenCB"
        android:text="Chicken"
        android:checked="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <CheckBox android:id="@+id/fishCB"
        android:text="Fish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <CheckBox android:id="@+id/steakCB"
        android:text="Steak"
        android:checked="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java

package com.trankyphat.app.uicontrol_checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class MainActivity extends AppCompatActivity {

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


        CheckBox fishCB = (CheckBox) findViewById(R.id.fishCB);
        if (fishCB.isChecked())
            fishCB.toggle(); // flips the checkbox to unchecked if it was checked

        fishCB.setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                        Log.v("CheckBoxActivity", "The fish checkbox is now "
                                + (isChecked ? "checked" : "not checked"));
                    }
        });
    }
}

Switch Control

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Switch
        android:id="@+id/switchdemo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This switch is: off" />
</LinearLayout>

MainActivity.java

package com.trankyphat.app.uicontrol_checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

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

        sw=(Switch)findViewById(R.id.switchdemo);
        sw.setOnCheckedChangeListener(this);
    }

    public void onCheckedChanged(CompoundButton buttonView,
                                 boolean isChecked) {
        Log.i("MyMessage", Boolean.toString(isChecked));


        if (isChecked) {
            Log.i("MyMessage", "Set switch to on");
            sw.setText("This switch is: on");

        }
        else {
            Log.i("MyMessage", "Set switch to off");
            sw.setText("This switch is: off");
        }
    }
}

RadioButton Control

android_radiobox

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RadioButton android:id="@+id/anotherRadBtn"
        android:text="Outside"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <RadioGroup android:id="@+id/radGrp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton android:id="@+id/chRBtn"
            android:text="Chicken"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RadioButton android:id="@+id/fishRBtn"
            android:text="Fish"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RadioButton android:id="@+id/stkRBtn"
            android:text="Steak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView android:text="My Favorite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RadioGroup>
</LinearLayout>

MainActivity.java

package com.trankyphat.app.uicontrol_checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity{

    private static final String TAG = "MyMessage";

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

        RadioGroup radGrp = (RadioGroup)findViewById(R.id.radGrp);
        int checkedRadioButtonId = radGrp.getCheckedRadioButtonId();
        radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup arg0, int id) {
                switch(id) {
                    case -1:
                        Log.v(TAG, "Choices cleared!");
                        break;
                    case R.id.chRBtn:
                        Log.v(TAG, "Chose Chicken");
                        break;
                    case R.id.fishRBtn:
                        Log.v(TAG, "Chose Fish");
                        break;
                    case R.id.stkRBtn:
                        Log.v(TAG, "Chose Steak");
                        break;
                    default:
                        Log.v(TAG, "Huh?");
                        break;
                }
            }});
    }

    private void addNewRadioButtonToRadioGroup(){
        RadioGroup radGrp = (RadioGroup)findViewById(R.id.radGrp);
        RadioButton newRadioBtn = new RadioButton(this);
        newRadioBtn.setText("Pork");
        radGrp.addView(newRadioBtn);
    }
}

ImageView Control

midu on android

activity_main.xml

<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_height="wrap_content" android:layout_width="wrap_content">

    <ImageView android:id="@+id/image1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

    <ImageView android:id="@+id/image2"
        android:layout_width="35dip" android:layout_height="50dip"
        android:src="#555555" />

    <ImageView android:id="@+id/image24"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:src="@drawable/midu" />

    <ImageView android:id="@+id/image4"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:maxWidth="35dip" android:maxHeight="50dip"
        />

    </LinearLayout>
</ScrollView >

MainActivity.java

package com.trankyphat.app.uicontrol_checkbox;

import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity{

    private static final String TAG = "MyMessage";

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

        ImageView imgView = (ImageView)findViewById(R.id.image4);
        imgView.setImageResource(R.mipmap.ic_launcher);
        imgView.setImageBitmap(BitmapFactory.decodeResource(
                this.getResources(), R.drawable.stevejobs));

        /*we’re showing the
        source of the image coming from the SD card. You’ll need to put some sort of image file
        out on the SD card with the proper name for this to work for you. Similar to BitmapFactory,
        the Drawable class has a few different ways to construct Drawables, including from an XML
        stream.*/
        imgView.setImageDrawable(Drawable.createFromPath("/mnt/sdcard/dave2.jpg"));
        /*
        takes the URI of an image file and uses that as the image source. For
        this last call, don’t think that you can use any image URI as the source. This method is really
        only intended to be used for local images on the device, not for images that you might find
        through HTTP. To use Internet-based images as the source for your ImageView, you’d most
        likely use BitmapFactory and an InputStream.
        */
        imgView.setImageURI(Uri.parse("file://mnt/sdcard/dave2.jpg"));
    }

}

DatePicker and TimePicker Controls

 

android_control_datetime2

activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:id="@+id/dateDefault"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <DatePicker android:id="@+id/datePicker"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
        <TextView android:id="@+id/timeDefault"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <TimePicker android:id="@+id/timePicker"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
        <TextClock
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:format12Hour="hh:mm:ss aa" android:format24Hour="kk:mm:ss" />
        <AnalogClock
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </LinearLayout>
</ScrollView>

android_control_datetime1

MainActivity.java

package com.trankyphat.app.uicontrol_checkbox;

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

public class MainActivity extends AppCompatActivity{

    private static final String TAG = "MyMessage";

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

        TextView dateDefault = (TextView)findViewById(R.id.dateDefault);
        TextView timeDefault = (TextView)findViewById(R.id.timeDefault);

        DatePicker dp = (DatePicker)this.findViewById(R.id.datePicker);
        // The month, and just the month, is zero-based. Add 1 for display.
        dateDefault.setText("Date defaulted to " + (dp.getMonth() + 1) + "/" +
                dp.getDayOfMonth() + "/" + dp.getYear());
        // And here, subtract 1 from December (12) to set it to December
        dp.init(2008, 11, 10, null);
        TimePicker tp = (TimePicker)this.findViewById(R.id.timePicker);
        java.util.Formatter timeF = new java.util.Formatter();
        timeF.format("Time defaulted to %d:%02d", tp.getHour(),
                tp.getMinute());
        timeDefault.setText(timeF.toString());
        tp.setIs24HourView(true);
        tp.setHour(new Integer(10));
        tp.setMinute(new Integer(10));
    }

}

MapView Control

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.google.android.maps.MapView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="myAPIKey"
        />
</LinearLayout>

 

Android 6’s Common Controls

Category: AndroidPrograming
0
2239 views

Join the discussion

Your email address will not be published. Required fields are marked *