An Android Spinner is used to select one value from a set of values .In the default State a Spinner Shows the currently selected values.Touching the Spinner displays a drop down menu with all other available values ,from which the user can select a new one.
Step-2:Creating MainActivity
Creating Project:
Make sure your Android SDK,JDK and AVD for testing android Application.Create new project :
Step-1:Creating layout:
activity_home.xml:
<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.shahimtiyaj.alertdialoge.HomeActivity"
android:background="@drawable/im3" >
/RelativeLayout>
Step-2:Creating layout:
activity_spinner.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/im1">
<Spinner
android:id="@+id/viewSpin"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingRight="10dp"
android:textDirection="firstStrong"
android:prompt="@array/option"
/>
</LinearLayout>
Step-3:Creating Values:
array.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="option" >
<item >BDNews24</item>
<item >Banglamail</item>
<item >eprothom-alo</item>
<item >Somoyer konthoshor </item>
<item >Daily Naya Digonta</item>
<item >Jugantor</item>
</string-array>
</resources>
Step-2:Creating MainActivity
HomeActivity.java:
package com.shahimtiyaj.alertdialoge;
import android.R.anim;
import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class HomeActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
OpenDialog();
}
private void OpenDialog() {
AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle("Select Category");
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
iew v=inflater.inflate(R.layout.view, null);
dialog.setView(v);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog.show();
Spinner spinner=(Spinner)v.findViewById(R.id.viewSpin);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.option,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}
Step-3:Creating Manifest:
No other Special permission for our project
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wub.alertdialoge"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".HomeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
0 comments:
একটি মন্তব্য পোস্ট করুন