简体   繁体   中英

Pop up menu on Application start

I had an application which have option to choose language at start. Currently when the application start i had to click on choose language and then pop up appears containing different languages. But I don't want to click on choose language option for displaying pop up. I want my app to automatically appears a pop up to choose language on app start. I followed this tutorial. Here is the snapshot of the thing I would like to achieve.

在此处输入图片说明

Here is a code of Mainactivity.java

public class AndroidLocalize extends Activity {
    Spinner spinnerctrl;
    Button btn;
    Locale myLocale;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spinnerctrl = (Spinner) findViewById(R.id.spinner1);
        spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                if (pos == 1) {
                    Toast.makeText(parent.getContext(), "You have selected Tamil", Toast.LENGTH_SHORT).show();
                    setLocale("ta");
                } else if (pos == 2) {
                    Toast.makeText(parent.getContext(), "You have selected Hindi", Toast.LENGTH_SHORT).show();
                    setLocale("hi");
                } else if (pos == 3) {
                    Toast.makeText(parent.getContext(), "You have selected English", Toast.LENGTH_SHORT).show();
                    setLocale("en");
                }
                else if (pos == 4) {
                    Toast.makeText(parent.getContext(), "You have selected Arabic", Toast.LENGTH_SHORT).show();
                    setLocale("ar");
                }
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
    }

    public void setLocale(String lang) {
        myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        Intent refresh = new Intent(this, AndroidLocalize.class);
        startActivity(refresh);
    }
}

My main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/greet"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/greet"
        android:textSize="25sp" android:gravity="center" android:paddingTop="25sp" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/sachin" android:paddingTop="25sp" />

    <TextView
        android:textColor="#ffffff"
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/langselection"
        android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center" android:paddingTop="25sp"/>

    <Spinner
        android:popupBackground="#ff004372"
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/languages"
        android:gravity="center" 
        android:paddingTop="25sp" />
</LinearLayout>

Try this in onCreate() of your Activity :

spinnerctrl = (Spinner) findViewById(R.id.spinner1);
findViewById(android.R.id.content).post(new Runnable() {
            @Override
            public void run() {
                 spinnerctrl.performClick();  
            }
});
//Rest of the code... 

It will automatically open the spinner popup during onCreate of Activity

Update

To cancel the runnable use:

Declare:

 Handler handler;
 Runnable runnable;

inside onCreate()

handler = new Handler();
runnable = new Runnable() {
         public void run() {
             spinnerctrl.performClick();  
       }
   };
handler.postDelayed(this, 500);

Now cancel it in onItemSelected using:

 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if (pos == 1) {
                Toast.makeText(parent.getContext(), "You have selected Tamil", Toast.LENGTH_SHORT).show();
                setLocale("ta");
                handler.removeCallbacks(runnable); 
            } 
            //rest of the condition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM