-2

I want to press the [Create New Follower] button and it increment the textview in followers.xml by one, each time the button is pressed.

Here's my MainActivity.class

package com.couchmango.godslife;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import java.util.*;
import android.preference.*;

public class MainActivity extends Activity       implements OnClickListener
{
    //Declare Constant Variables
    private Button createFollowerButton;
    private int followerLimit;
    private boolean reachedFollowerLimit;
    public static int FollowerCount;
    public static TextView numberOfFollowers;


    //Called when activity opens
    @Override
    protected void onCreate(Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Create Button
        createFollowerButton = (Button)findViewById(R.id.createFollowerButton);
        createFollowerButton.setOnClickListener(this);

        numberOfFollowers = (TextView) findViewById(R.id.numberOfFollowers);

        };//End onCreate

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState)
        {
        super.onRestoreInstanceState(savedInstanceState);
        // Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.

        }


    @Override
    public void onClick(View v)
        {
        switch(v.getId()){

                //Milestones Button Pressed
                case R.id.milestones:Intent intent = new Intent(MainActivity.this, milestones.class);
                MainActivity.this.startActivity(intent);
                break;

                //God Stats Button Pressed
                case R.id.godStats:intent = new Intent(MainActivity.this, godStats.class);
                MainActivity.this.startActivity(intent);
                break;

                //Influence Button Pressed
                case R.id.influence:intent = new Intent(MainActivity.this, influence.class);
                MainActivity.this.startActivity(intent);
                break;

                //Followers Button Pressed
                case R.id.followers:intent = new Intent(MainActivity.this, followers.class);

                MainActivity.this.startActivity(intent);
                break;

                //Create Follower Button Pressed
                case R.id.createFollowerButton: 

                    numberOfFollowers = (TextView) numberOfFollowers;
                    FollowerCount++;

                    if(reachedFollowerLimit == false){AddFollower();}

                    if(followerLimit == 10){

                    reachedFollowerLimit = true;

                    Context context = getApplicationContext();
                    CharSequence toastText = "Follower limit reached";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, toastText, duration);
                    toast.show();

                    }//end if()
                break;
            }//END SWITCH

        }//End OnCLICK





    //Adds Follower
    //everytime button is clicked
    public final void AddFollower()
        {


        followerLimit++;
        FollowerCount++;

        }//End names()


    private class Follower
        {
            int influence;

            public Follower(int influence)
                {
                influence = 1;

                }

        }



    public void startWorshipping()
        {


        }
}//End MainActivity`

The issue I'm having is that I want the button to increment a textview by one each time the button on the main activity is pressed.

  • Possible duplicate of [How do I pass data between Activities in Android application?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Titus May 18 '17 at 05:34
  • if `followers.xml` binded to any other activity or fragment ? – Manohar Reddy May 18 '17 at 05:34

2 Answers2

0

Set the text view's width and height as wrap content in xml. Then for every button click increase the textview's text size.

Bosco Sabin
  • 124
  • 7
0

You can store the value in the shared Preferences on the button click in the MainActivtiy and in the followers screen you can get the value from the shared preference and show it in the textview.

UPDATE

in your MainActivity

declare globally

public static final String PREFS = "prefs";
public static final String COUNT = "count";
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
int count = 0;

Then in your onCreate initialize both

sharedPreferences = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();

Then in your button click

count++;
editor.putInt(COUNT,count);
editor.commit();

then in the activity where you want to show the value

SharedPreferences sharedPreferences;

in onCreate

sharedPreferences = getSharedPreferences(MainActivity.PREFS, Context.MODE_PRIVATE);
sharedPreferences.getInt(MainActivity.COUNT,0);

this get int value will return the number of clicks

Anmol317
  • 1,251
  • 1
  • 11
  • 29