Today I will tell you how to make a user interface element called the seekbar with the Android Studio I.D.E.
On Android Studio select a new Empty Activity project, named it uiseekbar001.
You need to add the SeekBar UI and TextView UI into activity_main.xml file.
The textView, seekBar are variable for id into user interface elements and call the java file.
See the result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="org.free_tutorials.catafest.uiseekbar001.MainActivity"> <GridLayout android:layout_width="368dp" android:layout_height="495dp" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" tools:layout_editor_absoluteX="85dp" tools:layout_editor_absoluteY="313dp" /> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="-78dp" /> </LinearLayout> </GridLayout> </android.support.constraint.ConstraintLayout> |
You must interact with the user interface through the java file called MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package org.free_tutorials.catafest.uiseekbar001; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity { // the vars for SeekBar , TextView , progress and i SeekBar seekbar; TextView textView; int progress = 24; private int i; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // set the seekbar seekbar = (SeekBar) findViewById(R.id.seekBar); seekbar.setMax(100); seekbar.setProgress(progress); // set the textView textView = (TextView) findViewById(R.id.textView); textView.setText(" "+progress); textView.setTextSize(progress); // make new OnSeekBarChangeListener with metods and chenge size with i and progress vars seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean fromUser) { progress = i; textView.setText(" "+progress); textView.setTextSize(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } } |
The output will be a number text with value 24 sized by SeekBar up to value 100.