परिचय
यह उदाहरण दर्शाता है कि Android में एक कार्यशील वर्टिकल (लंबवत) सीकबार कैसे प्राप्त करें। डिफ़ॉल्ट रूप से Android का SeekBar क्षैतिज होता है, लेकिन rotation विशेषता का उपयोग करके इसे आसानी से लंबवत बनाया जा सकता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project चुनें और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट XML में कोड जोड़ें
res/layout/activity_main.xml फ़ाइल में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:rotation="270"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text=""
android:textSize="16sp" />
</RelativeLayout>
चरण 3: MainActivity.java में लॉजिक जोड़ें
src/MainActivity.java फ़ाइल में निम्नलिखित कोड जोड़ें:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
SeekBar seekBar;
TextView textView;
int min = 0, max = 100, current = 50;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
seekBar = findViewById(R.id.seekBar);
seekBar.setProgress(max - min);
seekBar.setProgress(current - min);
textView.setText("" + current);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
current = progress + min;
textView.setText("" + current);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
}
}
चरण 4: AndroidManifest.xml कॉन्फ़िगर करें
सुनिश्चित करें कि आपके androidManifest.xml में मुख्य गतिविधि सही ढंग से घोषित है:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में टूलबार से Run आइकन पर क्लिक करें, अपना डिवाइस चुनें, और ऐप लॉन्च होने पर आपको लंबवत सीकबार दिखाई देगा।
