Android Proficiency Tutorial V

Posted by blakey on Sat, 11 May 2019 19:24:21 +0200

Preface

Hello, everyone. Bring you an overview of Android Proficiency Tutorial V. I hope you like it.

Preface

If you want to learn Android development, you need to understand Java programming, which is the foundation and the key point. If you don't learn Java grammar, you should learn Android first, and then Android. Don't ask if you can learn Android first. You've all told me that you can learn Java first, right?

Basic Understanding of Android Development

Android development mainly understands these four important components:

  • Activity is the user interface, that is to say, activity can constitute the user interface.
  • ContentProvider is for the data stored in the device, through the creation of ContentProvider to achieve data sharing.
  • Service is a task that runs in the background and does not require users to interact with it directly.
  • Intent is a mechanism for describing behavior (such as choosing photos, making phone calls, etc.). In Android, almost everything is done through Intent, which gives us a lot of opportunities to replace or reuse components.

    Describe the Android project structure

    Android Manifest. xml: An XML file that describes the built application.
    assets: Folders are used to store static files that need to be packaged into applications.
    bin: Folders are used to store compiled applications.
    gen: Folder to store the generated source code.
    libs: A folder is a jar file that stores third-party packages.
    src: The folder is the Java source code of the program.
    res: The folder stores the resources of the application.
    In the res folder:
    res/drawable/: Stores images
    res/layout/: Stores xml-based files.
    res/menu/: Stores xml-based menu files.
    res/raw/: General files are stored.
    res/valuse/: Stores strings.
    res/xml /: is a common xml file.
    In the bin folder:
    bin/classes/: The compiled Java class files are stored.
    In the AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.gdmec.android.androidstudiodemo">
    <!--originally be android:theme="@style/AppTheme"-->
    <!--Remove ActionBar title bar-->
    <!--Add the application icon, app_icon-->
    <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--Add implementation classes-->
        <activity android:name=".######"></activity>
    </application>
</manifest>

Learn about build.gradle(app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "cn.edu.gdmec.android.androidstudiodemo"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

Understanding the basic layout of components

TextView: Learn about android:typeface,android:textStyle,android:textColor.
EditText: Edit box android:autoText,android:capitalize,android:digitas,android:singleLine.
Inside margin: android: padding Left, android: padding Right, android: padding Top, android: padding Bottom
Relative Layout Layout Layout Layout: android: layout_alignParentTop, android: layout_alignParentBottom, android: layout_alignParentLeft, android: layout_alignParentRight, android: layout_centerHorizontal, android: layout_centerVertical, android: center: Horizontal, android: layout_centerInParent.
android:layout_above,android:layout_below,android:layout_toLeftOf,android:layout_toRightOf,android:layout_alignTop, android:layout_alignBottom,android:layout_alignLeft,android:layout_alignRight,android:layout_alignBaseline.
TableLayout layout:
android:stretchColumns,android:shrinkColumns,android:collapseColumns.

//TableLayout
<TableLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:stretchColumns="1">
 <TableRow>
  <TextView
   android:text="Full name:"/>
  <EditText
   android:id="@+id/xm"
   android:layout_span="3"/>
 </TableRow>
 <View
   android:layout_height="2px"
   android:background="#000000"/>
 <TableRow>
  <Button
   android:id="@+id/xx"
   android:layout_column="2"
   android:text="eliminate"/>
  <Button
   android:id="@+id/submit"
   android:text="Send out"/>
 </TableRow>
</TableLayout>

Adapter

Sting[] items={"h","j","k","a","s","d","b"};
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
<?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">
<TextView
 android:id="@+id/selection"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"/>
<ListView
 android:id="@android:id/list"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:drawSelectiorOnTop="false"/>
</LinearLayout>
public class ListViewDemo extends ListActivity{
 TextView selection;
 String[] items={"a","b","c","d","e","f","g"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   setListAdapter(new ArrayAdapter<String>(this,android.R.layout_simple_list_item_1,items));
   selection= findViewById(R.id.selection);
}
 public void onListItemClick(ListView parent,View v,int position,long id){
  selection.setText(items[position]);
 }
}

grid

GridView: android:numColumns,android:verticalSpacing,android:horizontalSpacing,android:columnWidth,android:stretchMode.

//Adapter
<?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">
<TextView
 android:id="@+id/selection"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"/>
<AutoCompleTextView
 android:id="@+id/auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:completionThreshold="3"/>
</LinearLayout>
public class AutoCompleteDemo extends Activity implements TextWatcher{
 TextView selection;
 AutoCompleteTextView auto;
 String[] items={"aaav","bbbv","cccv","dddv","eeev","fffv","gggv"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   selection=findViewById(R.id.selection);
   auto=findViewById(R.id.auto);
   auto.addTextChangedListener(this);
   auto.setAdapter(new ArrayAdapter<String>(this,android.R.layout_simple_item_1,items));
}
 public void onTextChanged(CharSequence s, int start, int before, int count){
  selection.setText(auto.getText());
}
}

Gallery

Gallery: android:spacing,android:spinnerSelector,android:drawSelectorOnTop

//Adapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://echema.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
<ImageView
 android:id="@+id/icon"
 android:layout_width="20px"
 android:layout_height="wrap_content"
 android:paddingLeft="2px"
 android:paddingRight="2px"
 android:paddingTop="2px"
 android:src="@drawable/image"/>
<TextView
 android:id="@+id/label"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="20sp"/>
</LinearLayout>
public class Demo extends ListActivity{
 TextView selection;
 String[] items = { "aaaa","asdg","bsdes","slfl","wete","wetwd","sdfefs"};
@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   setListAdapter(new ArrayAdapter<String>(this, R.layout.simple,R.id.label,items));
   selection=findViewById(R.id.selection);
}
public void onListItemClick(ListView parent,View v,int position,long id){
 selection.setText(items[position]);
 }
}
//Dynamic list
public class Demo extends ListActivity{
 TextView selection;
 String[] items = { "aaaa","asdg","bsdes","slfl","wete","wetwd","sdfefs"};
@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   setListAdapter(new IconAdapter());
   selection=findViewById(R.id.selection);
}
public void onListItemClick(ListView parent,View v,int position,long id){
 selection.setText(items[position]);
 }
class IconAdapter extends ArrayAdapter {
 IconAdapter(){
  super(Demo.this,R.layout.simple,items);
 }
 public View getView(int position, View convertView, ViewGrop parent){
 LayoutInflater inflater=getLayoutInflater();
 View simple = inflater.inflate(R.layout.simple,parent,false);
 TextView label=simple.findViewById(R.id.simple);
 label.setText(items[position]);
 ImageView icon = simple.findViewById(R.id.icon);
 icon.setImageResource(R.drawable.icon);
}

Date and time

DatePicker and DatePickerDialog - > DatePickerDialog - > OnDateChangedListener and OnDateSetListener
TimePicker and TimePickerDialog - > TimePickerDialog - > OnTimeChangedListener and OnTimeSetListener
Main sample code:

Calendar dateTime = Calendar.getInstance();
//date
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener(){
 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){
  dateTime.set(Calendar.YEAR,year);
  dateTime.set(Calendar.MONTH,monthOfYear);
  dateTime.set(Calendar.DAY_OF_MONTH,dayOfMonth);
  updateLabel();
  }
};
//time
TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener(){
 public void onTimeSet(TimePicker view, int hourOfDay, int minute){
  dateTime.set(Calendar.HOUR_OF_DAY,hourOfDay);
  dateTime.set(Calender.MINUTE,minute);
  updateLabel();
  }
};
//Date click button
Button btn=findViewById(R.id.date);
btn.setOnClickListener(new View.OnClickListener(){
 public void onClick(View v){
  new DatePickerDialog(Demo.this,d,dateTime.get(Calendar.YEAR),dateTime.get(Calendar.MONTH),dateTime.get(Calendar.DAY_OF_MONTH)).show();
 }
});
//Click the button of time
Button btn=findViewById(R.id.time);
btn.setOnClickListener(new View.OnClickListener(){
 public void onClick(View v){
  new TimePickerDialog(Demo.this,t,dateTime.get(Calendar.HOUR_OF_DAY),dateTime.get(Calendar.MINUTE),true).show();
 }
});
//Display Calendar
dateTimetv.getTime();// dtl.format();

Create clock

DigitalClock or Analog Clock

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <AnalogClock 
 android:id="@+id/analogclock"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_alginParentTop="true"/>
 <DigitalClock
 android:id="@+id/digitalclock"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_below="@id/analogclock"/>
</RelativeLayout>

Progress bar (android.widget.ProgressBar)

The progress bar can be either horizontal or rotary. You can use increment ProgressBy () to increase progress, or setProgress() to increase progress.

There are many styles of progress bars, which Android provides:

  • Widget.ProgressBar.Horizontal
  • Widget.ProgressBar.Small
  • Widget.ProgressBar.Large
  • Widget.ProgressBar.Inverse
    And so on.

android.widget.SeekBar
This is an extension of ProgressBar, which is a slippery alternative form of progress.

Users can manipulate the location of the slider through SeekBar.OnSeekBarChangeListener.

Adapter - android.widget.Adapter

Its subclasses are: arrayadapter,baseadpter,cursoradapter,listadapter,simpleadapter,spinneradapter

WebView

android.webkit.WebView
The WebView here is a view showing the web page. When we want to load the web page in WebView, we need to add permissions in android manifest.xml.

<uses-permission android: name = "android.permission.INTERNET" />  

How to use the code, the following shows:

 Uri uri = Uri.parse(url);
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);

display

WebView webview = new WebView(this);
 setContentView(webview);

Loading:

webview.loadUrl(url);

Display box

public void onClick(View view){
 if(view==button1){
  new AlertDialog.Builder(this).setTitle("Message").setMessage("ok").setNeutralButton("Close", new DialogInterface.OnClickListener(){
   public void onClick(DialogInterface dialog, int in){
   }
  }).show();
}

summary

  • This article talks about Android Proficiency Tutorial V, if you still have a better understanding, welcome to communicate.
  • Location: Share Android & Java Knowledge Points and be interested to continue to pay attention to them

Topics: Android xml Java encoding