Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set button Visible in another acticty with Preferences setting

I've a problem to set button Visibility through another Activity

Code explanation:

First, menu.xml

<Button
    android:id="@+id/f1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/button1"
    android:visibility="visible" />

<ImageView
    android:id="@+id/f2lock"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:src="@drawable/levellocked"
    android:visibility="visible" />

<Button
    android:id="@+id/f2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/button2"
    android:visibility="gone" />

f2 button used for intent leveltwo.class but it still set to GONE, f2lock is ImageView for levellocked

Second, menu.java

public class menu extends Activity {

Button f1, f2;
ImageView f2lock;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.famouslevel);
    f1 =(Button)findViewById(R.id.f1);      

    f1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            // TODO Auto-generated method stub
            Intent level1 = new Intent ();
            level1.setClassName ("com.example.game", "com.example.game.levelone");
            startActivityForResult (level1, 0);              
        }             
    });     
}   

public void onActivityResult (int requestCode, int resultCode, Intent level1){
    super.onActivityResult (requestCode, resultCode, level1); 
    f2=(Button)findViewById(R.id.f2);      
    f2lock=(ImageView)findViewById(R.id.f2lock);

    switch (resultCode) {
        case 2:  f2.setVisibility(View.VISIBLE);
                 f2lock.setVisibility(View.GONE);            
    }      

    f2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            // TODO Auto-generated method stub
            Intent level2 = new Intent ();
            level2.setClassName ("com.example.game", "com.example.game.leveltwo");
            startActivityForResult (level2, 0);              
        }             
    });       
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.splashscreen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

following code to call levelone.java with a Result

so in levelone.java i put the code like this

 public void onClick(View v){
                  setResult (2);
                  finish();          
                  }
                }); 

the code function is to send Result (2) to menu.class when level.class is finish();

public void onActivityResult (int requestCode, int resultCode, Intent level1){
    super.onActivityResult (requestCode, resultCode, level1); 
    f2=(Button)findViewById(R.id.f2);      
    f2lock=(ImageView)findViewById(R.id.f2lock);

    switch (resultCode) {
        case 2:  f2.setVisibility(View.VISIBLE);
                 f2lock.setVisibility(View.GONE);            
    }      

the code above is to receive result (2) from levelone.class and do case 2 function

the question is how to use and set SharedPreferences in case 2? so the f2 and f2lock visibility will saved

because i have try the SharedPreferences code but nothing is happen, f2 button still GONE and f2lock imageview still VISIBLE

i mean is like this:

Like a game, when user have done level 1 so level 2 will unlocked

but in here i make button is VISIBLE when level 1 is done

like image 966
RichFounders Avatar asked Oct 30 '22 21:10

RichFounders


1 Answers

Hopefully I've understood your question correctly. Please, correct me if I have not!

The way I see things is that there are several different solution to your use case:

The first one, would be a proper SharedPreferences implementation, directly building on top of your existing code. In my eyes, that is not the best approach, as it is misusing the point of SharedPreferences.

Another way of doing this would be to implement callbacks into the different activities, but that would just get tedious, as you add more levels.

My solution to this would be to have a different class with static values, which would store the progress of the player. This can also be evolved to a file, which you write to the disk, if you want the progress to remain between sessions.

You would just need to check the player's progress, whenever you need, with a simple interface function like getPlayerProgress(), which would return, for example, an integer, explaining which is the maximum level achieved. This also assumes that you handle the interface with a seperate function, which would be called at the beginning of every level/game start ect. A name for that function would be updateLevel(), for example. Does this make sense to you?

Here is a sample implementation, of the two classes I mentioned:

/**
 * A static class, which handles all player progress, throughout the lifespan of the app.
 *
 */
static class PlayerProgress {
    // We set progress to static, so it would be alive, no matter what activity you're in.
    private static int progress = 1;

    /**
     * Update the player's progress.
     * @param levelNumber: latest level number.
     */
    public static void updateProgress(int levelNumber) {
        progress = levelNumber;
    }

    /**
     * Get the player's progress.
     * @return the latest level number.
     */
    public static int getPlayerProgress() {
        return progress;
    }
}

/**
 * The gui handler would need to be called, every time you need to update the screen to the
 * appropriate level and it's assets. (Buttons, activities ect.)
 * 
 * I would implement a MainActivity, which would handle the different screens. 
 *
 */
class guiHandler {
    public void updateLevel() {
        int progress = PlayerProgress.getPlayerProgress();
        /*
         * Add your 
         */
        switch(progress) {
        case 1:
            /*
             * Add the code, which would display level one. 
             * This would include all button visibilities and maybe load level resources ect.
             */
            break;
        case 2:
            /*
             * Same as above.
             */
            break;

        // You can expand this to as many levels you'd like. 
        }
    }
}

Again, if I have misunderstood, please correct me. If you'd like sample code, by all means, just ask.

I hope you have a lovely day.

like image 164
Sipty Avatar answered Nov 15 '22 06:11

Sipty