1

Requirement

Application requirement is when the user installs and opens for the first time, he will be shown LoginActivity and once he logs in successfully, he will be asked to enter 4 digit passcode, to use in future, if application remains idle and phone gets locked.

Design

I've decided it to design both as in One Activity i.e. MainActivity and load contentView of LoginActivity on certain condition.

Code Part

MainActivity.java


@Override
protected void onCreate(Bundle savedInstanceState) {
    isUserValid=false; //just for testing on initial screen i.e. Login
    super.onCreate(savedInstanceState);
    if(isUserValid) {
        setContentView(R.layout.activity_main);
    }
    else{
        setContentView(R.layout.activity_login);
    }
}

@Override
protected void onResume(){
    super.onResume();
    if(isUserValid) {
        setContentView(R.layout.activity_main);
    }
    else{
        setContentView(R.layout.activity_login);

    }
}

@Override
protected void onStart(){
    super.onStart();
    if(isUserValid) {
        setContentView(R.layout.activity_main);
    }
    else{
        setContentView(R.layout.activity_login);
    }
}

LoginActivity.java


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    //Attaching click event to button
    Button btnValidate=(Button)findViewById(R.id.validate_code);
    btnValidate.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            //Functionality
        }
    });
}

I am aware that I could have attached click event directly to button in activity_login.xml but then it works only when we use setActivity() instead of setContentView and am supposed to use setContentView here as I do not want user to navigate between these 2 views on click of back button. I've also tried to attach click event for button in onCreate on MainActivity.java under else condition as below:

@Override
protected void onCreate(Bundle savedInstanceState) {
    isUserValid=false; //just for testing on initial screen i.e. Login
    super.onCreate(savedInstanceState);
    if(isUserValid) {
        setContentView(R.layout.activity_main);
    }
    else{
        setContentView(R.layout.activity_login);
        //Attaching click event to button
        Button btnValidate=(Button)findViewById(R.id.validate_code);
        btnValidate.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View view) {
                  //Functionality
             }
        });
    }
}

But that did not help. further, I went forward and tried to attach the event in both onStart and onResume states of both the activity, nevertheless, did not work. The click event is not at all triggered. I assure that button with that id is present in activity_login.xml. What is that am committing mistake here. I am not getting the functionality and am in just starting stage of learning Android. Hope to find some solution for this behavior.

2
  • 1
    alternatively you can state onClick funtion in XML only by using android:onClick="btnValidate" and define a funtion in MainActivity like public void btnValidate(View view){ Your Functionality goes here } Aug 11, 2016 at 9:53
  • @NishantPardamwar tried that.. Still no luck.. :( Aug 11, 2016 at 10:01

2 Answers 2

3

I highly recommend using multiple fragments however you can still add a content view programmatically as below :

View login = this.getLayoutInflater().inflate(R.layout.activity_login);
addContentView(login, new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

you can use the inflated login to find views and attach listeners.

To remove the login you can use the

((ViewGroup)getWindow().getDecorView()).removeView(login);

Again I don't recommend this approach. Using fragments or custom dialogs will be better.

Hope this will help.

0
1

Try using Fragments instead of different content views.

activity xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    if (isUserValid) {
        ft.replace(R.id.content, new LoggingFragment(), "loggingFragment");
    } else {
        ft.replace(R.id.content, new MainFragment(), "mainFragment");
    }
    ft.commit();
}

fragment xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">

<Button
       android:id="@+id/button"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

</LinearLayout>

fragment

@Override
protected View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment, null);

    Button btnValidate=(Button)v.findViewById(R.id.validate_code);
    btnValidate.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            //Functionality
        }
    });

    return v;
}
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.