0

I was planning to change from one page to another so the first screen has title bar and navigation bar on it and I want to change to another page with just textview on it with no title bar when I am out of fucos to my edittext.

Here is the Code:

    public class MainActivity extends Activity{
     private EditText edittext;
     private ListView list;
     private CustomListAdapter customAdapter;
     private DrawerLayout drawerLayout;
     private ActionBarDrawerToggle drawerToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);

       // this.tv = (TextView) this.findViewById(R.id.textView1);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        //new ConnnectionRequest(tv,"95","3025");
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        drawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout,         /* DrawerLayout object */
                R.drawable.ic_launcher,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle("Rodolfo");
                edittext.setText("");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("Navalon");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        addKeyListener(); 


        }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (drawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }

    public void addKeyListener() {
        List<String> arrayStrings = new ArrayList<String>();
        arrayStrings.add("rodolfo");
        arrayStrings.add("erodolfo2");

        edittext = (EditText) findViewById(R.id.editText);
        edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
        edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    //Remove title bar
                    requestWindowFeature(Window.FEATURE_NO_TITLE);

                    //Remove notification bar
                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);



                    setContentView(R.layout.second_screen);

                }
            }
        });

        list = (ListView) findViewById(R.id.list_view);

        customAdapter = new CustomListAdapter(this,R.layout.list_item,R.id.product_name,arrayStrings);
        list.setAdapter(customAdapter);

        edittext.addTextChangedListener(new TextWatcher(){

            @Override
            public void afterTextChanged(Editable arg0) {
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
            }

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                    int arg3) {
                MainActivity.this.customAdapter.getFilter().filter(cs);

            }

        });
    }  
}

At my addkeyListener method where my edditext is been initialized also in the setOnFucos change where my changing to page occur and got error. I dont know why its giving me an error.

10-03 20:47:47.916: E/AndroidRuntime(9023): FATAL EXCEPTION: main
10-03 20:47:47.916: E/AndroidRuntime(9023): **android.util.AndroidRuntimeException: requestFeature() must be called before adding content**
10-03 20:47:47.916: E/AndroidRuntime(9023):     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:268)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.app.Activity.requestWindowFeature(Activity.java:3310)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at com.example.octranspodestination.MainActivity$2.onFocusChange(MainActivity.java:122)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.onFocusChanged(View.java:4571)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.widget.TextView.onFocusChanged(TextView.java:7640)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.widget.EditText.onFocusChanged(EditText.java:172)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.clearFocus(View.java:4461)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.ViewGroup.clearFocus(ViewGroup.java:775)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.setFlags(View.java:8518)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.setVisibility(View.java:5631)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewPositionChanged(DrawerLayout.java:1364)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.support.v4.widget.ViewDragHelper.continueSettling(ViewDragHelper.java:730)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.support.v4.widget.DrawerLayout.computeScroll(DrawerLayout.java:764)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.getDisplayList(View.java:12594)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.getDisplayList(View.java:12650)
Prmths
  • 2,022
  • 4
  • 21
  • 38
game on
  • 347
  • 1
  • 7
  • 22

1 Answers1

0

The stack trace says

requestFeature() must be called before adding content

Looking at the documentation, this is true:

Enable extended screen features. This must be called before setContentView(). May be called as many times as desired as long as it is before setContentView().

In onCreate(), you already call setContentView() so when your editText changes focus and calls requestFeature again(), an Exception is thrown.

Since it is usually bad practice to swap content Views in one Activity. You probably should rethink about your app design - usually getting a user's input should not cause the status bar and title to disappear. You could launch a new Activity or make a Dialog instead, but this depends on your needs.

A--C
  • 36,351
  • 10
  • 106
  • 92
  • So I need to create a new activity and swap it through Intent class? Am I right – game on Oct 04 '13 at 01:25
  • @user2844805 If you require that when the user input whatever they need in the `EditText`, the title and status bar disappear, then something like that should work - use [`startActivityForResult()`](http://stackoverflow.com/questions/5868994/can-anybody-tell-what-is-use-of-start-activity-for-result-in-android). – A--C Oct 04 '13 at 01:37