Please help me to find the solution to my problem-
I have implemented the recycler view where on clicking on the item an alert dialog opens which contains webview in which HTML data is parsed. For scrolling sometimes, I notice flickering of Alert Dialog. I don't know what exactly it is.
Thanks in Advance!
This is the glimpse to code I've written-
if(message.getCONTENT_TYPE().equals("HTML"))
{ //holder.setIsRecyclable(false);
webView= new WebView(context);
String Text="HTML Content, "+"Click to open!";
holder.messageView.setText(Text);
if(message.getMESSAGE_TYPE() == TYPE_SENT){
holder.messageView.setBackgroundResource(R.drawable.balloon_html_sent);
holder.item.setGravity(Gravity.END);
holder.messageView.setTextColor(holder.messageView.getResources().getColor(R.color.white));
holder.messageView.setTypeface(null, Typeface.BOLD);
}else if(message.getMESSAGE_TYPE() == TYPE_REC){
holder.messageView.setBackgroundResource(R.drawable.balloon_html_received);
holder.item.setGravity(Gravity.START);
holder.messageView.setTextColor(holder.messageView.getResources().getColor(R.color.white));
holder.messageView.setTypeface(null, Typeface.BOLD);
}else {
holder.messageView.setBackgroundResource(R.drawable.balloon_html_mid);
holder.item.setGravity(Gravity.CENTER_HORIZONTAL);
holder.messageView.setTextColor(holder.messageView.getResources().getColor(R.color.white));
holder.messageView.setTypeface(null, Typeface.BOLD_ITALIC);
}
holder.messageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(context,R.style.CustomAlertDialog);
LinearLayout wrapper = new LinearLayout(context);
WebView webView = new WebView(context);
EditText keyboardHack = new EditText(context);
keyboardHack.setVisibility(View.GONE);
webView.loadData(message.getCONTENT(), "text/html", "utf-8");
wrapper.setOrientation(LinearLayout.VERTICAL);
wrapper.addView(webView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
wrapper.addView(keyboardHack, LinearLayout
.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
if(wrapper.getParent() != null) {
((ViewGroup)wrapper.getParent()).removeView(wrapper);
}
builder.setView(wrapper);
builder.create().show();
}
});
}
Related
I have two Linear Layouts in single activity .
I want to display custom popups on each of them when clicked.
When I click on first layout, popup apperas,then on clicking on 2nd layout ,both popups are displayed. how can i have only single popup displayed at a time?
this is my code
workLinearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater=
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate
(R.layout.activity_your_places__work__popup, null);
updateTextView = (TextView)
popupView.findViewById(R.id.UpdateTextView);
updateTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Your_Places2Activity.this,
UpdateWorkAddressActivity.class);
startActivity(i);
}
});
deleteTextView = (TextView)
popupView.findViewById(R.id.DeleteTextView);
deleteTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
//code to delete address
}
});
popupWindowWork = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//dismiss other popup if is showing
if(popupWindowHome.isShowing())
{
popupWindowHome.dismiss();}
//display popup
popupWindowWork.showAsDropDown(workLinearLayout, 0, -70);
}
});
i have done same thing on other linear layout
popupWindowWork = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
if(popupWindowHome.isShowing())
{
popupWindowHome.dismiss();
}
in this line, you are dismissing the popupWindowHome dialog if popupWindowHome is showing, with popupWindowHome being the NEW dialog. Move the if statement before the constructor call.
if(popupWindowHome != null && popupWindowHome.isShowing())
{
popupWindowHome.dismiss();
}
popupWindowWork = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
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.
I already have the AlertDialog.Builder set to final.
I have a button inside of the dialog box that when clicked, makes a purchase for the user and then closes. I'm having issues with the "closes" portion. Here's the code for the button:
Button buyButton = new Button(context);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100, 0.0f);
buyButton.setLayoutParams(buttonParams);
buyButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view){
if (STOCK_TO_PURCHASE.size() != 0) {
Player thisPlayer = players[getPlayerIndexByPlayOrder(CURRENT_TURN)];
purchaseStock(thisPlayer);
buyStockDialog.dismiss();
} else {
buyStockDialog.dismiss();
}
}
});
The IDE is telling me I have to typecast buyStockDialog. If I typecast it though, I get a runtime error telling me I can't typecast buyStockDialog to DialogInterface.
What's the best way to handle this?
I already have a custom onBackPressed method and considered forcing the application to mimic the back button being pressed. What do you think the best solution here is?
Declaration line for AlertDialog.Builder as per request:
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
final AlertDialog.Builder buyStockDialog = new AlertDialog.Builder(ctw);
buyStockDialog.setTitle("Buy Stock: ");
As per Second request, the full Dialog's Code and Layout:
public void buyStock(View view){
Context context = getApplicationContext();
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
final AlertDialog.Builder buyStockDialog = new AlertDialog.Builder(ctw);
buyStockDialog.setTitle("Buy Stock: ");
//create ScrollView to hold everything
ScrollView scrollView = new ScrollView(context);
//generate content for dialog
LinearLayout dialogContainer = new LinearLayout(context);
dialogContainer.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 350, 1);
params.gravity = Gravity.CENTER;
dialogContainer.setLayoutParams(params);
dialogContainer.setPadding(15, 15, 0, 15);
dialogContainer.setBackgroundColor(Color.WHITE);
//each hotel stock options
for (int i = 0; i < hotels.size(); i++) {
Hotel testHotel = hotels.get(i);
testHotel.setPrice(200);
View stockPicker = getStockPicker(testHotel);
LinearLayout.LayoutParams pickerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 75, 1.0f);
pickerParams.gravity = Gravity.LEFT;
stockPicker.setLayoutParams(pickerParams);
dialogContainer.addView(stockPicker);
stockPicker.setBackgroundColor(0xffffff);
}
LinearLayout scrollWrapper = new LinearLayout(context);
scrollWrapper.addView(scrollView);
scrollWrapper.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 300, 1.0f));
scrollView.addView(dialogContainer);
scrollView.setLayoutParams(params);
LinearLayout dialogLayout = new LinearLayout(context);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
dialogLayout.setBackgroundColor(Color.WHITE);
Button buyButton = new Button(context);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100, 0.0f);
buyButton.setLayoutParams(buttonParams);
buyButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view){
if (STOCK_TO_PURCHASE.size() != 0) {
Player thisPlayer = players[getPlayerIndexByPlayOrder(CURRENT_TURN)];
purchaseStock(thisPlayer);
buyStockDialog.dismiss();
} else {
buyStockDialog.dismiss();
}
}
});
dialogLayout.addView(scrollWrapper);
dialogLayout.addView(buyButton);
buyStockDialog.setView(dialogLayout);
buyStockDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
int numInterations = STOCK_TO_PURCHASE.size();
for (int i = 0; i < numInterations; i++) {
STOCK_TO_PURCHASE.remove(0);
}
}
});
buyStockDialog.show();
}
There is a difference between AlertDialog, which has a dismiss() function. And AlertDialog.Builder which is the builder pattern for the alert dialog. When you are about to show the dialog you call
buyStockDialog.create()
The returned value there is the one you have to call dismiss on.
You have to do something like this where you declare your dialog:
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
AlertDialog.Builder buyStockDialog = new AlertDialog.Builder(ctw);
buyStockDialog.setTitle("Buy Stock: ");
final Dialog dialog = builder.create();
And inside your click listener where you want to dismiss the dialog you will call this:
dialog.dismiss();
I am trying to implement Admob ads on app. What I want to do is to ad a banner ad on a custom Dialog. I have tried everything but can't find the solution.
I have made a custom xml for the dialog. When adding admob on xml, it won't run. So I tried do it programmatically. But still can't make it work.
public void OnClickButton(View paramView)
{
int btn_id = paramView.getId();
if (btn_id == R.id.hint_field)
{
//set up dialog
if (hint != null && hint.length()>0) {
final Dialog dialog = new Dialog(Activity.this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
//ad loading
dialog.setContentView(R.layout.custom_dialog);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.dialog_l);
layout.addView(ad);
AdRequest r = new AdRequest();
ad.loadAd(r);
dialog.setTitle("Σχετικά με την λέξη :");
dialog.setCancelable(true);
//set up text
TextView text = (TextView) dialog.findViewById(R.id.hint_text);
text.setText(hint);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
// now that the dialog is set up, it's time to show it
dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
}
}
On a button click I display a custom Dialog. In the OnCreate method of my Activity I have made the adView
AdView ad = new AdView(this, AdSize.BANNER, "a15xxxxxxxxxxx");
I get a NullPointerException at : layout.addView(ad);
Any ideas ?
Thanks in advance!
Problem solved! I had to inflate the custom dialog interface first. The code below is the working code!
public void OnClickButton(View paramView)
{
int btn_id = paramView.getId();
if (btn_id == R.id.hint_field)
{
//set up dialog
if (hint != null && hint.length()>0) {
final Dialog dialog = new Dialog(Activity.this, R.style.Theme_New);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.custom_dialog);
LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main = inflater.inflate(R.layout.custom_dialog, null);
dialog.setContentView(main);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
LinearLayout linear = (LinearLayout)main.findViewById(R.id.ad_layout);
ad = new AdView(this, AdSize.IAB_MRECT, "a15xxxxxxxx");
AdRequest request = new AdRequest();
Set<String> keywords = new HashSet<String>();
keywords.add("game");
request.setKeywords(keywords);
linear.addView(ad);
ad.loadAd(request);
dialog.setTitle("Title :");
dialog.setCancelable(true);
//set up text
TextView text = (TextView) dialog.findViewById(R.id.hint_text);
text.setText(hint);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
// now that the dialog is set up, it's time to show it
lp.width = width;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
dialog.getWindow().setAttributes(lp);
dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
dialog.getWindow().getAttributes().height = LayoutParams.WRAP_CONTENT;
}
}
}
I m Using WebView in AlertDialog to authenticate user to twitter.
but When i click on field in webview ,android keyboard doesnt open.
here is my code that shows how i added webview in alert dialog.
i implicitly call android keyboard but it open keyboard behind alert dialog.
here is my code.
public static void webViewDialog(final String authorizationURL, final int type)
{
final boolean correctPin;
System.out.println("In webViewDialog");
container = new LinearLayout(context);
container.setOrientation(LinearLayout.VERTICAL);
container.setMinimumWidth(200);
container.setMinimumHeight(320);
webView = new WebView(context);
webView.setMinimumWidth(200);
webView.requestFocusFromTouch();
webView.setMinimumHeight(380);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new TwitterWebViewClient());
webView.loadUrl(authorizationURL);
webView.setBackgroundColor(0xFFbbd7e9);
container.addView(webView);
Builder webDialog = new AlertDialog.Builder(context);
webDialog.setView(container).setTitle("Twitter Login")
.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
if (type == 0)
{
twitterPinCodeDialog();
dialog.dismiss();
}
}
}).show();
showVirtualKeyboard();
}
public static void showVirtualKeyboard()
{
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
#Override
public void run()
{
InputMethodManager m = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if(m != null)
{
// m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
}
}
}, 100);
}
Here is my solution to this problem:
Put a dummy edit text, and set it's visibility to GONE, and add it to a containing LinearLayout, after adding the WebView to the layout.
Example:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LinearLayout wrapper = new LinearLayout(this);
WebView webView = new WebView(this);
EditText keyboardHack = new EditText(this);
keyboardHack.setVisibility(View.GONE);
webView.loadUrl(url);
wrapper.setOrientation(LinearLayout.VERTICAL);
wrapper.addView(webView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
wrapper.addView(keyboardHack, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
builder.setView(wrapper);
builder.create().show();
Once this is done, everything should work properly, and when you select an item in the WebView, the keyboard appears as expected.
I am using a PopupWindow with a WebView inside it and experienced the same problem, but if I set focusable to true in the parent the problem goes away:
popupWindow.setFocusable(true);
Hope this helps!
I had a similar issue and solved it in this way:
I override the onCreateView() method on the dialog fragment and define all view stuff configuration for my web view.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.webview, container, false);
// do some config
// some other stuffs
loginpage.loadUrl(url);
return view;
}
On the onCreateDialog() method i just add these.
#Override
public Dialog onCreateDialog(Bundle savedInstaceState) {
Dialog dialog = super.onCreateDialog(savedInstaceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
In my case i wanted to show a dialog with no title. So due the DialogBuilder take care of creating dialog's view i decided to override the onCreateView() and just call the super.onCreateDialog() and add my window configuration.
I also suffer from this problem, I solved this problem by customizing Dialog, here is my custom dialog code, hope so this is use full for you.
TwitterDialog fb=new TwitterDialog(this);
fb.abc();
//fb.dismiss();
class TwitterDialog extends Dialog {
Context context;
String url="https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1<mpl=default<mplcache=2";
public TwitterDialog(Context context) {
super(context);
this.context=context;
}
void abc(){
LinearLayout mContent = new LinearLayout(context);
mContent.setOrientation(LinearLayout.VERTICAL);
final float scale = context.getResources().getDisplayMetrics().density;
float[] dimensions =new float[]{400.0f,500.0f};
addContentView(mContent, new FrameLayout.LayoutParams(
(int) (dimensions[0] * scale + 0.5f),
(int) (dimensions[1] * scale + 0.5f)));
FrameLayout.LayoutParams FILL =
new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
WebView mWebView = new WebView(context);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(new WebClicent());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
mWebView.setLayoutParams(FILL);
mContent.addView(mWebView);
TwitterDialog.this.show();
}
class WebClicent extends WebViewClient{
#Override
public void onLoadResource(WebView view, String url) {
System.out.println("onLoadResource "+url);
super.onLoadResource(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("onPageFinished "+url);
//TwitterDialog.this.dismiss();
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
System.out.println("onPageStarted "+url);
super.onPageStarted(view, url, favicon);
}
}
}//TWitterDialog
The reason your keyboard may not be showing up, is probably because you are using a device that already has a hard keyboard. ANY DEVICE WITH A PHYSICAL KEYBOARD does not need to show a soft keyboard... That is why it is not showing. Because your device or your emulator has a physical hard keyboard.