Multiple buttons in a popup, trying to stay organized - android

I'm implementing a popup in fragment Tab3Fragment and would like to keep the popup code in,
public void showPopup(View anchorView){
}
and minimized elsewhere in the Tab3Fragment as much as possible to keep things tidy.
Currently showPopup looks something like this,
public void showPopup(View anchorView) {
Button btnDismiss, btnFirstRecord, btnPreviousRecord, btnNextRecord, btnLastRecord;
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemServi (Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = (TextView)popupView.findViewById(R.id.tv);
tv.setText("Blah, blah, blah");
btnDismiss = (Button)popupView.findViewById(R.id.btnDismissxml);
btnFirstRecord = (Button)popupView.findViewById(R.id.btnFirstRecordxml);
btnPreviousRecord = (Button)popupView.findViewById(R.id.btnPreviousRecordxml);
btnNextRecord = (Button)popupView.findViewById(R.id.btnNextRecordxml);
btnLastRecord = (Button)popupView.findViewById(R.id.btnLastRecordxml);
popupWindow.setFocusable(true);
int location[] = new int[2];
anchorView.getLocationOnScreen(location);
popupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0);
}
}
QUESTION: Is there any way to implement an onClick method case-switch statement inside of showPopup that would handle this? Perhaps something like,
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnFirstRecordxml:
//firstRecord(v);
break;
case R.id.btnPreviousRecordxml:
//previousRecord(v);
break;
case R.id.btnNextRecordxml:
//nextRecord(v);
break;
case R.id.btnLastRecordxml:
//lastRecord(v);
break;
case R.id.btnDismissxml:
//closePopup(v);
break;
}}
Other solutions such as putting onClick in popup_layout.xml thusly,
<Button
android:id="#+id/btnFirstRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/pszFirstRecordButton"
android:onClick="Tab3Fragment.firstRecord"/>
to handle button clicks would be highly appreciated. Thanks in advance...
Update, 23 Nov 2014.
Here is a solution that does work for more than one button's code in showPopup(). I'll have several buttons in the popup....
private void showPopup(){
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
Button btnDismiss=(Button)popupView.findViewById(R.id.btnDismissxml);
Button btnSaveRecord=(Button)popupView.findViewById(R.id.btnSaveRecordxml);
final PopupWindow popupWindow=new PopupWindow(popupView,480,500,true);
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 40);
//first button
btnSaveRecord.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
saveRecord();
}
});
//second button
btnDismiss.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
}
Any way to insert a case-switch structure to allow other button code in showPopup()? This would avoid creating separate onClickListener-onClicks for each button as shown above.

Modify the xml like this, use android:onClick="ClickEvent" this formet in all the button
<Button
android:id="#+id/btnFirstRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/pszFirstRecordButton"
android:onClick="ClickEvent"/>
<Button
android:id="#+id/btnPreviousRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/pszPreviousRecordButton"
android:onClick="ClickEvent"/>
use the click funcationality like this,
public void clickEvent(View v)
{
switch (v.getId()) {
case R.id.btnFirstRecordxml:
//firstRecord(v);
break;
case R.id.btnPreviousRecordxml:
//previousRecord(v);
break;
case R.id.btnNextRecordxml:
//nextRecord(v);
break;
case R.id.btnLastRecordxml:
//lastRecord(v);
break;
case R.id.btnDismissxml:
//closePopup(v);
break;
}
}
Or try this,
btn.setOnClickListener(btnaction); set like this listener for all buttons,
OnClickListener btnaction = new OnClickListener() {
#Override
public void onClick(View v) {
//use the switch conditons...
}
};
btn1.setOnClickListener(btnaction);
btn2.setOnClickListener(btnaction);

public void showPopup(View anchorView) {
Button btnDismiss, btnFirstRecord, btnPreviousRecord, btnNextRecord, btnLastRecord;
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemServi (Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = (TextView)popupView.findViewById(R.id.tv);
tv.setText("Blah, blah, blah");
btnDismiss = (Button)popupView.findViewById(R.id.btnDismissxml);
btnFirstRecord = (Button)popupView.findViewById(R.id.btnFirstRecordxml);
btnPreviousRecord = (Button)popupView.findViewById(R.id.btnPreviousRecordxml);
btnNextRecord = (Button)popupView.findViewById(R.id.btnNextRecordxml);
btnLastRecord = (Button)popupView.findViewById(R.id.btnLastRecordxml);
btnFirstRecord.setOnClickListener(new Listener());
btnPreviousRecord.setOnClickListener(new Listener());
btnNextRecord .setOnClickListener(new Listener());
btnLastRecord .setOnClickListener(new Listener());
popupWindow.setFocusable(true);
int location[] = new int[2];
anchorView.getLocationOnScreen(location);
popupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0);
}
}
Class Listener implements OnClickListener
{
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnFirstRecordxml:
//firstRecord(v);
break;
case R.id.btnPreviousRecordxml:
//previousRecord(v);
break;
case R.id.btnNextRecordxml:
//nextRecord(v);
break;
case R.id.btnLastRecordxml:
//lastRecord(v);
break;
case R.id.btnDismissxml:
//closePopup(v);
break;
}
}
}

Related

Implement emojis in android keyboard with popupwindow

I've developed a keyboard and now i need to add emojis to it , from other questions i've realized the best way is with popupwindow,
Here's what i've done:
case -102:
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.emoji_view, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
popupWindow.showAsDropDown(getWindow().getOwnerActivity().getCurrentFocus(),50, -30);
Unfortunatly this doesn't work , showAsDropDown needs a view as its first var , and if the keyboard is in another app i don't have a view to give him...
Is there a way to fix that ?
or am i going about it all wrong and there is a better way...
all help will be appreciated!
Given that you are using the official SoftKeyBoard implementation as a template for your keyboard:
//Cut some pieces of the code for clarity
case -102:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.emoji_view, null);
PopupWindow popupWindow = new PopupWindow(popupView, MATCH_PARENT, MATCH_PARENT);
popupWindow.showAsDropDown(mInputView);
Use the view that you have inflated for your keyboard, in the offical SoftKeyBoard and the snippet above it is called mInputView.
hi i have done same things. I have made one custom keyboard in android.
EmoticonsPagerAdapter emojiAdapter;
/**
* Defining all components of emoticons keyboard
*/
private void enablePopUpView() {
final ViewPager pager = (ViewPager) popUpView
.findViewById(R.id.emoticons_pager);
pager.setOffscreenPageLimit(3);
final ArrayList<EmojiItem> paths = EmojiUtil.getInstance(acitiviy)
.getAllEmojis();
final ArrayList<EmojiItem>[] groups = new ArrayList[5];
for (EmojiItem emoji : paths) {
if (groups[emoji.emojiGroup] == null) {
groups[emoji.emojiGroup] = new ArrayList<EmojiItem>();
}
groups[emoji.emojiGroup].add(emoji);
}
final ArrayList<EmojiItem> history = new ArrayList<EmojiItem>();
ArrayList<Integer> historyIds = SettingsUtil.getHistoryItems(acitiviy);
for (Integer his : historyIds) {
for (EmojiItem emoji : paths) {
if (emoji.id == his) {
history.add(emoji);
break;
}
}
}
history.add(paths.get(0));
final KeyClickListener onEmojiClick = new KeyClickListener() {
#Override
public void keyClickedIndex(EmojiItem index) {
int cursorPosition = editMessage.getSelectionStart();
editMessage.getText().insert(cursorPosition, index.emojiText);
try {
editMessage.getText().setSpan(
new ImageSpan(index.emojiDrawable), cursorPosition,
cursorPosition + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (Exception e) {
}
if (history.get(0) != index)
history.add(0, index);
SettingsUtil.setHistoryItems(acitiviy, history);
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
};
((ImageButton) popUpView.findViewById(R.id.emoji2))
.setImageDrawable(groups[0].get(0).emojiDrawable);
((ImageButton) popUpView.findViewById(R.id.emoji3))
.setImageDrawable(groups[1].get(0).emojiDrawable);
((ImageButton) popUpView.findViewById(R.id.emoji4))
.setImageDrawable(groups[2].get(0).emojiDrawable);
((ImageButton) popUpView.findViewById(R.id.emoji5))
.setImageDrawable(groups[3].get(0).emojiDrawable);
((ImageButton) popUpView.findViewById(R.id.emoji6))
.setImageDrawable(groups[4].get(0).emojiDrawable);
popUpView.findViewById(R.id.emoji1).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = history;
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
popUpView.findViewById(R.id.emoji2).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = groups[0];
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
popUpView.findViewById(R.id.emoji3).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = groups[1];
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
popUpView.findViewById(R.id.emoji4).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = groups[2];
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
popUpView.findViewById(R.id.emoji5).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = groups[3];
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
popUpView.findViewById(R.id.emoji6).setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
emojiAdapter.emojis = groups[4];
emojiAdapter.notifyDataSetChanged();
pager.setAdapter(emojiAdapter);
}
});
emojiAdapter = new EmoticonsPagerAdapter(acitiviy, groups[0],
onEmojiClick);
pager.setAdapter(emojiAdapter);
// Creating a pop window for emoticons keyboard
popupWindow = new PopupWindow(popUpView, LayoutParams.MATCH_PARENT,
(int) keyboardHeight, false);
View backSpace = (View) popUpView.findViewById(R.id.imageBackspace);
backSpace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0,
0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
editMessage.dispatchKeyEvent(event);
}
});
popupWindow.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss() {
emoticonsCover.setVisibility(LinearLayout.GONE);
}
});
ViewPager pagerStickers = (ViewPager) popUpView
.findViewById(R.id.stickers_pager);
pagerStickers.setOffscreenPageLimit(3);
}
private void showKeyboardPopup(View root, boolean attaches) {
if (!popupWindow.isShowing()) {
popupWindow.setHeight((int) (keyboardHeight));
if (isKeyBoardVisible) {
imageEmoji.setImageResource(R.drawable.emoji_kbd);
emoticonsCover.setVisibility(LinearLayout.GONE);
} else {
imageEmoji.setImageResource(R.drawable.ic_down);
emoticonsCover.setVisibility(LinearLayout.VISIBLE);
}
try {
popupWindow.showAtLocation(root, Gravity.BOTTOM, 0, 0);
} catch (Exception e) {
}
} else {
imageEmoji.setImageResource(R.drawable.emoji_btn_normal);
popupWindow.dismiss();
return;
}
imageAttaches.setBackgroundColor(attaches ? 0xFF808080 : 0x00000000);
imageEmojis.setBackgroundColor(attaches ? 0x00000000 : 0xFF808080);
imageStickers.setBackgroundColor(0x00000000);
layoutEmojis.setVisibility(attaches ? View.GONE : View.VISIBLE);
layoutStickers.setVisibility(View.GONE);
}
Please checkout for more details click here.
Thanks hope this will help you.It is bit old but you can try it.
you can refer this github link for complete code
EmojiIcon

Android popup - passing arguments to event handlers

I have a NavigationView with menu.
When any menuitem is long pressed, I inflate a popup which gives the user various options/buttons. Each button has an event handler.
I need those handlers to know which menuitem originally triggered the popup.
What I am missing is the knowledge of how to pass that identity along the flow of objects.
In the posted code, the menuitem is longclicked, I pass menutem as a view to the popup constructor.
Yet no idea how to then pass it to the popup Button's event handler.
I am considering as an alternative, simply setting a variable with the id when the longpress occurs, and then reading that variable back again in the final handler.... but that does feel like a cheat and would require managing to ensure synchronising with events.
#Override
public boolean onLongClick(View v) {
pop(v);
return true;
}
public void pop(View v){
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View vw) {
Toast.makeText(getApplicationContext(), "id of menuitem here", Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(v, 50, -30);
}
If I got your question right, you just want to pass the id of menuitem tapped on to pop method and later display that value in Toast.
In that case considering you can cast the id value into string, you can use setTag() and getTag():
#Override
public boolean onLongClick(View v)
{
v.setTag(place the id of menuitem tapped here);
pop(v);
return true;
}
public void pop(View v)
{
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View vw) {
Toast.makeText(getApplicationContext(), String.valueOf(v.getTag()), Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(v, 50, -30);
}

Set Text for pop up

I have a button with the onClick name of checkResult.
public void checkResult(View view){
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(this);
View popupView = layoutInflater.inflate(R.layout.layout_result_popup, myRoot);
final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//what I want to show in the popup
TextView scorePopup = (TextView)findViewById(R.id.score_popup);
scorePopup.setText("Your score: " + score);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(checkResultButton, 100, -1200);
popupWindow.setFocusable(true);
popupWindow.update();
}
When checkResult button is clicked, layout_result_popup is displayed.
I directly put a button at main activity and use onClick to show popup. So far, I managed to show the popup text in XML. But when I try to set some text to it, my app crashed.
Help please. :)
Try to replace those 2 lines with this:
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView countView = (TextView)findViewById(R.id.count);
countView .setText("Count: " + count+ "/" + totalCount);
}
});
It very likely even works with the first line outside of the runOnUiThread

how to implement pop-up code for multiple buttons on the screen in the android?

As I am beginner, I am able to implement the pop-up code for single button that is pasted below..
Requirement: I need to implement multiple pop-up's to show different text(information) on each pop-up.
public void init() {
popupButton = (Button) findViewById(R.id.textview1);
popupText = new TextView(this);
insidePopupButton = new Button(this);
layoutOfPopup = new LinearLayout(this);
LinearLayout lt=new LinearLayout(this);
view=new ScrollView(this);
insidePopupButton.setText("OK");
popupText.setText("This is Popup Window.press OK to dismiss it.");
popupText.setBackgroundColor(Color.WHITE);
popupText.setPadding(0, 0, 0, 20);
layoutOfPopup.setOrientation(1);
lt.addView(popupText);
layoutOfPopup.setBackgroundColor(Color.BLACK);
layoutOfPopup.addView(view);
layoutOfPopup.addView(insidePopupButton,350,50);
view.addView(lt);
}
public void popupInit() {
popupButton.setOnClickListener(this);
insidePopupButton.setOnClickListener(this);
popupMessage = new PopupWindow(layoutOfPopup, LayoutParams.FILL_PARENT,
LayoutParams.MATCH_PARENT);
popupMessage.setContentView(layoutOfPopup);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.textview1) {
popupMessage.showAsDropDown(popupButton, 0, 0);
}
else {
popupMessage.dismiss();
}
}
}
and my requirement is showing through the image.
Use switch case for handle click on other views.
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.textview1:
case R.id.textview2:
case R.id.textview3:
case R.id.textview4: popupMessage.showAsDropDown(popupButton, 0, 0);
break;
default:popupMessage.dismiss();
}
}
Use a dialog fragment and design a layout that will contain all of the required buttons that you need.

How to create a popup window (PopupWindow) in Android

To create a simple working PopupWindow, we need to do the following:
popup_example.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Test Pop-Up" />
</LinearLayout>
Java code
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false),100,100, true);
pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
My requirement is that I need a
<TEXTVIEW android:layout_height="wrap_content" android:layout_width="fill_parent" />
and a
<BUTTON android:id="#+id/end_data_send_button" android:text="Cancel"/>
in my popup_example.xml. How can I handle these two components in my Java code?
How to make a simple Android popup window
This is a fuller example. It is a supplemental answer that deals with creating a popup window in general and not necessarily the specific details of the OP's problem. (The OP asks for a cancel button, but this is not necessary because the user can click anywhere on the screen to cancel it.) It will look like the following image.
Make a layout for the popup window
Add a layout file to res/layout that defines what the popup window will look like.
popup_window.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#62def8">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="30dp"
android:textSize="22sp"
android:text="This is a popup window."/>
</RelativeLayout>
Inflate and show the popup window
Here is the code for the main activity of our example. Whenever the button is clicked, the popup window is inflated and shown over the activity. Touching anywhere on the screen dismisses the popup window.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_window, null);
// create 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;
}
});
}
}
That's it. You're finished.
Going on
Check out how gravity values effect PopupWindow.
You can also add a shadow.
Further study
These were also helpful in learning how to make a popup window:
PopupWindow documentation
How To Create Pop Up Window In Android (YouTube video)
Android Popup Window Example
Here, I am giving you a demo example. See this and customize it according to your need.
public class ShowPopUp extends Activity {
PopupWindow popUp;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
LinearLayout layout = new LinearLayout(this);
LinearLayout mainLayout = new LinearLayout(this);
TextView tv = new TextView(this);
Button but = new Button(this);
but.setText("Click Me");
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
}
}
Hope this will solve your issue.
are you done with the layout inflating? maybe you can try this!!
View myPoppyView = pw.getContentView();
Button myBelovedButton = (Button)myPoppyView.findViewById(R.id.my_beloved_button);
//do something with my beloved button? :p
I construct my own class, and then call it from my activity, overriding small methods like showAtLocation. I've found its easier when I have 4 to 5 popups in my activity to do this.
public class ToggleValues implements OnClickListener{
private View pView;
private LayoutInflater inflater;
private PopupWindow pop;
private Button one, two, three, four, five, six, seven, eight, nine, blank;
private ImageButton eraser;
private int selected = 1;
private Animation appear;
public ToggleValues(int id, Context c, int screenHeight){
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pop = new PopupWindow(inflater.inflate(id, null, false), 265, (int)(screenHeight * 0.45), true);
pop.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.alpha_0));
pView = pop.getContentView();
appear = AnimationUtils.loadAnimation(c, R.anim.appear);
one = (Button) pView.findViewById(R.id.one);
one.setOnClickListener(this);
two = (Button) pView.findViewById(R.id.two);
two.setOnClickListener(this);
three = (Button) pView.findViewById(R.id.three);
three.setOnClickListener(this);
four = (Button) pView.findViewById(R.id.four);
four.setOnClickListener(this);
five = (Button) pView.findViewById(R.id.five);
five.setOnClickListener(this);
six = (Button) pView.findViewById(R.id.six);
six.setOnClickListener(this);
seven = (Button) pView.findViewById(R.id.seven);
seven.setOnClickListener(this);
eight = (Button) pView.findViewById(R.id.eight);
eight.setOnClickListener(this);
nine = (Button) pView.findViewById(R.id.nine);
nine.setOnClickListener(this);
blank = (Button) pView.findViewById(R.id.blank_Selection);
blank.setOnClickListener(this);
eraser = (ImageButton) pView.findViewById(R.id.eraser);
eraser.setOnClickListener(this);
}
public void showAtLocation(View v) {
pop.showAtLocation(v, Gravity.BOTTOM | Gravity.LEFT, 40, 40);
pView.startAnimation(appear);
}
public void dismiss(){
pop.dismiss();
}
public boolean isShowing() {
if(pop.isShowing()){
return true;
}else{
return false;
}
}
public int getSelected(){
return selected;
}
public void onClick(View arg0) {
if(arg0 == one){
Sudo.setToggleNum(1);
}else if(arg0 == two){
Sudo.setToggleNum(2);
}else if(arg0 == three){
Sudo.setToggleNum(3);
}else if(arg0 == four){
Sudo.setToggleNum(4);
}else if(arg0 == five){
Sudo.setToggleNum(5);
}else if(arg0 == six){
Sudo.setToggleNum(6);
}else if(arg0 == seven){
Sudo.setToggleNum(7);
}else if(arg0 == eight){
Sudo.setToggleNum(8);
}else if(arg0 == nine){
Sudo.setToggleNum(9);
}else if(arg0 == blank){
Sudo.setToggleNum(0);
}else if(arg0 == eraser){
Sudo.setToggleNum(-1);
}
this.dismiss();
}
}
Button endDataSendButton = (Button)findViewById(R.id.end_data_send_button);
Similarly you can get the text view by adding a id to it.
LayoutInflater inflater = (LayoutInflater) SettingActivity.this.getSystemService(SettingActivity.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.gd_quick_action_slide_fontsize, null),LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, true);
pw.showAtLocation(SettingActivity.this.findViewById(R.id.setting_fontsize), Gravity.CENTER, 0, 0);
View v= pw.getContentView();
TextView tv=v.findViewById(R.id.....);
This an example from my code how to address a widget(button) in popupwindow
View v=LayoutInflater.from(getContext()).inflate(R.layout.popupwindow, null, false);
final PopupWindow pw = new PopupWindow(v,500,500, true);
final Button button = rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pw.showAtLocation(rootView.findViewById(R.id.constraintLayout), Gravity.CENTER, 0, 0);
}
});
final Button popup_btn=v.findViewById(R.id.popupbutton);
popup_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popup_btn.setBackgroundColor(Color.RED);
}
});
Hope this help you
Edit your style.xml with:
<style name="AppTheme" parent="Base.V21.Theme.AppCompat.Light.Dialog">
Base.V21.Theme.AppCompat.Light.Dialog provides a android poup-up theme

Categories

Resources