Reputation: 117
I'm new to Android App development and java and I have the following problem: I have an "about" option inside menu in actionbar. I want popup to appear when clicked on "about" option, showing app version and developer details. As I have no experience with android app development and java(I'm still learning though), could you please help me!
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="133dp"
android:layout_marginTop="126dp"
android:text="@string/appVersion" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginTop="156dp"
android:text="@string/developer" />
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// create an action bar button
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
getMenuInflater().inflate(R.menu.mymenu, menu);
return super.onCreateOptionsMenu(menu);
}
// handle button activities
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.about) {
// about app page
onButtonShowPopupWindowClick();
}
return super.onOptionsItemSelected(item);
}
public void onButtonShowPopupWindowClick(View view) {
//inflate layout of popup window
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_window, null);
// creating the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
}
Upvotes: 1
Views: 1440
Reputation: 1
just need context (this) or (.context), custom layout with options and find the view(button) the popup will be actived -> translate to java
button.setOnClickListener{
val popup = LayoutInflater.from(this).inflate(R.layout.layout_custom_popupmenu, null)
val popupMenu = PopupWindow(this)
popupMenu.contentView = popup
popupMenu.width = LinearLayout.LayoutParams.WRAP_CONTENT
popupMenu.isFocusable = true
popupMenu.isOutsideTouchable = true
var op1 = popup.findViewById<TextView>(R.id.popOption1)
var op2 = popup.findViewById<TextView>(R.id.popOption2)
var op3 = popup.findViewById<TextView>(R.id.popOption3);
var op4 = popup.findViewById<TextView>(R.id.popOption4);
op1.setText("Edit"); op2.setText("Erase")
op1.setOnClickListener { popupMenu.dismiss()}
op2.setOnClickListener { popupMenu.dismiss();}
popupMenu.setBackgroundDrawable(null)
popupMenu.showAsDropDown(button, 0, 0)
}
Upvotes: 0
Reputation: 1895
You can use an AlertDialog
with a custom view. With a custom view you can design your AlertDialog
by XML.
public void onButtonShowPopupWindowClick(Context context) {
LayoutInflater layoutinflater = getLayoutInflater;
View view1 = layoutinflater.inflate(R.layout.yourlayout, null);
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setCancelable(true);
builder1.setView(view1);
AlertDialog alert = builder1.create();
alert.show();
}
You have to create a XML file named yourlayout and in this layout you design your dialog.
The Parameter context
of the void has to be YourActivity.this
.
Upvotes: 0
Reputation: 502
I think this : https://developer.android.com/guide/topics/ui/dialogs is a good guide for you to start. It also exposes good practices with pop-up in android
Hope it will be helpfull
PS: In the bottom you will find an example with an custom pop-up like you need
Upvotes: 1
Reputation: 631
You should try with an AlertDialog:
new AlertDialog.Builder(context)
.setView(R.layout.popup_window)
.create().show();
Upvotes: 0