Reputation: 77
I have created an pop up window that I can use to show important messages in my app. The code for this are written in PopActivity.
Here is my code:
public class PopActivity extends Activity {
private WorkOutClass the_workout_class = new WorkOutClass();
private TextView repTextField, setsTextField;
private Button den_knappen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop);
repTextField = (TextView) findViewById(R.id.repetitionID);
setsTextField = (TextView) findViewById(R.id.setsID);
den_knappen = (Button) findViewById(R.id.buttonID);
repTextField.setText("Reps : " + String.valueOf(the_workout_class.getReps()));
setsTextField.setText("Sets: " +String.valueOf(the_workout_class.getSets()));
DisplayMetrics thePopUpWindow = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(thePopUpWindow);
int width = thePopUpWindow.widthPixels;
int height = thePopUpWindow.heightPixels;
getWindow().setLayout((int)(width*.8), (int)(height*.7));
WindowManager.LayoutParams params = getWindow().getAttributes();
params.gravity = Gravity.CENTER;
params.x = 0;
params.y = 20;
getWindow().setAttributes(params);
den_knappen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
the_workout_class.increaseReps();
repTextField.setText("Reps : " + String.valueOf(the_workout_class.getReps()));
setsTextField.setText("Sets: " +String.valueOf(the_workout_class.getSets()));
}
});
}}
My question is though: If i want to use the same type of pop-up window somewhere else in my app which provide a different message, is there a way I can just copy this to use it? Or do I need to create new file such as PopActivity2, PopActivity3 etc etc.?
Thanks for answer.
Upvotes: 0
Views: 47
Reputation: 561
Sounds to me that what you really need is a Dialog
. You can create your own layout and texts for a dialog, and then use it where ever you need it. These links can help you with that:
https://developer.android.com/guide/topics/ui/dialogs
How do I display an alert dialog on Android?
How to set dialog to show in full screen?
Upvotes: 1
Reputation: 162
You can pass your data via Intent when you start your Activity like
// in previous activity
Intent intent = new Intent(this, PopActivity.class);
intent.putExtra("first", "your message")
startActivity(intent);
// in PopActivity
String message = getIntent().getStringExtra("first")
and use this as the message your need. You can pass more data if you want. Best would be to define a constant for the name ("first") with a better name and use this on both Activities ;-)
Upvotes: 0