Pavan Kumar Yadav
Pavan Kumar Yadav

Reputation: 5

How to automatically create buttons in another activity using a button from different activity?

I have a button in MainActivity.

I want to create new buttons in Second Activity.

Each time if a user presses the button in MainActivity, then same number of button should be automatically created in the Second Activity.

Upvotes: 0

Views: 264

Answers (1)

Nikhil PV
Nikhil PV

Reputation: 1012

Here is the sample code for getting no. button clicks in another activity. Here i have took if user has clicked 3, 6 or 9 times then call second activity and create that many buttons.

MainActivity.java

public class MainActivity extends Activity {
 Button btn;
 int i =0;
 SharedPreferences.Editor preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.button1);
    preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            i++;
            preferences.putInt("value", i).apply();

            if(i==3 || i==6 || i==9){
                Intent intent = new Intent(MainActivity.this, Second.class);
                startActivity(intent);
            }
        }
    });

 }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sample.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text22"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Button" />

 </RelativeLayout>

SecondActivity.java

public class SecondActivity extends Activity {

int value;
SharedPreferences preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    preferences = PreferenceManager.getDefaultSharedPreferences(SecondActivity.this);
    value = preferences.getInt("value", 0);
    System.out.println("SecondActivity.onCreate() of i ----- " + value);

    for (int i = 1; i <= value; i++) {
        Button myButton = new Button(this);
        myButton.setText("Add Me");
        LinearLayout ll = (LinearLayout) findViewById(R.id.layout);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER;
        myButton.setLayoutParams(lp);
        ll.addView(myButton, lp);
    }
  }
 }

second.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.example.sample.Second" >

</LinearLayout>

Below are the screenshots.

MainActivity image

SecondActivity image

Upvotes: 1

Related Questions