How to define gravity of a Dialog Fragment? - android-dialogfragment

I want my dialog to stick to the start but it always comes in the center.

i will give you example==>
Following is the code -->Onclick image -->Dialog Appears==>
Dialog dialog; //your dialog declaration
//
//
//
oncreate(){
imageView = (ImageView) findViewById(R.id.customProfileGridImg);
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.START |Gravity.TOP; //change direction as per your requirement
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.show();
}
});
}

Related

Unity Banner is not showing

My Banner Ad does show in test mode but without testmode not.
I used a code template from the unity website.
I also asked the unity ads support but they couldn't find the error.
they send me a video of my application,and in this video the banner ad worked.
Here is my code:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
public class newadsystem : MonoBehaviour
{
// For the purpose of this example, these buttons are for functionality testing:
[SerializeField] Button _loadBannerButton;
[SerializeField] Button _showBannerButton;
[SerializeField] Button _hideBannerButton;
[SerializeField] BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER;
[SerializeField] public string _androidAdUnitId = "Banner_Android";
[SerializeField] string _iOsAdUnitId = "Banner_iOS";
public string _adUnitId = "Banner_Android";
void Start()
{
Advertisement.Initialize("4395157",false,true);
// Disable the button until an ad is ready to show:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
// Set the banner position:
Advertisement.Banner.SetPosition(_bannerPosition);
// Configure the Load Banner button to call the LoadBanner() method when clicked:
_loadBannerButton.onClick.AddListener(LoadBanner);
_loadBannerButton.interactable = true;
}
// Implement a method to call when the Load Banner button is clicked:
public void LoadBanner()
{
// Set up options to notify the SDK of load events:
BannerLoadOptions options = new BannerLoadOptions
{
loadCallback = OnBannerLoaded,
errorCallback = OnBannerError
};
// Load the Ad Unit with banner content:
Advertisement.Initialize("4395157", false, true);
Advertisement.Banner.Load(_adUnitId, options);
Advertisement.Initialize("4395157", false, true);
}
// Implement code to execute when the loadCallback event triggers:
void OnBannerLoaded()
{
Debug.Log("Banner loaded");
// Configure the Show Banner button to call the ShowBannerAd() method when clicked:
_showBannerButton.onClick.AddListener(ShowBannerAd);
// Configure the Hide Banner button to call the HideBannerAd() method when clicked:
_hideBannerButton.onClick.AddListener(HideBannerAd);
// Enable both buttons:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
}
// Implement code to execute when the load errorCallback event triggers:
void OnBannerError(string message)
{
Debug.Log($"Banner Error: {message}");
// Optionally execute additional code, such as attempting to load another ad.
}
// Implement a method to call when the Show Banner button is clicked:
void ShowBannerAd()
{
// Set up options to notify the SDK of show events:
BannerOptions options = new BannerOptions
{
clickCallback = OnBannerClicked,
hideCallback = OnBannerHidden,
showCallback = OnBannerShown
};
// Show the loaded Banner Ad Unit:
Advertisement.Initialize("4395157", false, true);
Advertisement.Banner.Show(_adUnitId, options);
}
// Implement a method to call when the Hide Banner button is clicked:
void HideBannerAd()
{
// Hide the banner:
Advertisement.Banner.Hide();
}
void OnBannerClicked()
{
Debug.Log("Banner Clicked!");
}
void OnBannerShown()
{
Debug.Log("Banner Shown!");
}
void OnBannerHidden()
{
Debug.Log("Banner Hidden");
}
void OnDestroy()
{
// Clean up the listeners:
_loadBannerButton.onClick.RemoveAllListeners();
_showBannerButton.onClick.RemoveAllListeners();
_hideBannerButton.onClick.RemoveAllListeners();
}
}
I looked into the android device log and there was: "Unity : Banner Error: UnityAds is not initialized."
but i call several times the initialize function.
PS: i am not very good at english.
Have a look at Start (), your code:
Advertisement.Initialize("4395157",false,true);
// Disable the button until an ad is ready to show:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
And then at OnBannerLoaded ()
// Enable both buttons:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
But those button are already interactable = true.
Maybe you need to change Start () like this:
Advertisement.Initialize("4395157",false,true);
// Disable the button until an ad is ready to show:
_showBannerButton.interactable = false;
_hideBannerButton.interactable = false;
Update 1
Try to change your code the following way:
void Start()
{
_showBannerButton.interactable = false;
_hideBannerButton.interactable = false;
Advertisement.Initialize("4395157",false,true);
_loadBannerButton.onClick.AddListener(LoadBanner);
_showBannerButton.onClick.AddListener(ShowBannerAd);
_hideBannerButton.onClick.AddListener(HideBannerAd);
_loadBannerButton.interactable = true;
}
public void LoadBanner()
{
BannerLoadOptions options = new BannerLoadOptions
{
loadCallback = OnBannerLoaded,
errorCallback = OnBannerError
};
if (Advertisement.isInitialized) {
Advertisement.Banner.Load(_adUnitId, options);
} else Debug.LogWarning ("Adverisement not initialized! Try again later");
}
void OnBannerLoaded()
{
Debug.Log("Banner loaded");
BannerOptions options = new BannerOptions
{
clickCallback = OnBannerClicked,
hideCallback = OnBannerHidden,
showCallback = OnBannerShown
};
Advertisement.Banner.SetPosition(_bannerPosition);
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
}
void ShowBannerAd()
{
Advertisement.Banner.Show(_adUnitId, options);
}

Single and Double Click on Button in Android?

I want to create Single and Double click on Button in Android...
Thanks for help in Advance.
I have already tried using button.setOnClickListener() for single click on button but i couldn't find double click on button
Try this code : (btn is the button you want to check for single and double click)
int i = 0;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
i++;
Handler handler = new Handler();
Runnable r = new Runnable() {
#Override
public void run() {
i = 0;
}
};
if (i == 1) {
//Single click
handler.postDelayed(r, 250);
} else if (i == 2) {
//Double click
i = 0;
ShowDailog();
}
}
});

Fire Button onAction on key released and touch released JavaFX

The docs say the button should fire for clicks, keypress, and touch. It doesn't seem to work.
Some code to play with.
public class FireButtonTest extends Application {
#Override
public void start(Stage primaryStage) {
TextArea ta = new TextArea();
Button btn1 = new Button();
btn1.setText("btn1");
btn1.setOnAction((ActionEvent event) -> {
ta.setText(ta.getText()+"btn1 fired \n");
});
Button btn2 = new Button();
btn2.setText("btn2");
btn2.setOnAction((ActionEvent event) -> {
ta.setText(ta.getText()+"btn2 fired \n");
});
btn2.setOnKeyReleased((KeyEvent ke) -> {
if (ke.getCode() == KeyCode.ENTER) {
ta.setText(ta.getText()+"btn2 key event -> ");
btn2.fire();
}
});
//?? can't test this
btn2.setOnTouchReleased((TouchEvent te) -> {
if (te.getTouchCount() == 1){
ta.setText(ta.getText()+"btn2 touch event -> ");
btn2.fire();
}
});
VBox root = new VBox(5);
root.getChildren().addAll(btn1, btn2, ta);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Button Test");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Why can't I just press enter when the button is focused?
Doh! I guess you have to use the space bar. Here's the source.
<!-- language: java -->
protected static final List<KeyBinding> BUTTON_BINDINGS = new ArrayList<KeyBinding>();
static {
BUTTON_BINDINGS.add(new KeyBinding(SPACE, KEY_PRESSED, PRESS_ACTION));
BUTTON_BINDINGS.add(new KeyBinding(SPACE, KEY_RELEASED, RELEASE_ACTION));
}

Softkeyboard is shown only on calling method from button click

I am programatically showing the soft keyboard when a layout is loaded on a button click.
I am showing the softkeyboard only when the textfield has focus. It works fine. But when I call the same method at another place in the code(not a button click) then the soft keyboard does not show up. Below is my code. Please point out where I went wrong.
public void showNewView() {
setContentView(R.layout.activity_main);
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
isRegisterScreen = true;
final EditText text1 = (EditText) findViewById(R.id.label1);
final EditText text2 = (EditText) findViewById(R.id.label2);
final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
text1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
inputManager.showSoftInput(labelText, InputMethodManager.SHOW_IMPLICIT);
}else{
inputManager.hideSoftInputFromWindow(text1.getWindowToken(), 0);
}
}
});
text2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
inputManager.showSoftInput(text2, InputMethodManager.SHOW_IMPLICIT);
}else{
inputManager.hideSoftInputFromWindow(phoneText.getWindowToken(), 0);
}
}
});
text1.requestFocus();
}

Adding a button to MediaController

I want to add a button to MediaController. So I extended MediaController class, created a button and added it into the frame layout. But the newly added button is not reflecting while running.
Please find the code below
public class VideoController extends MediaController {
private Button searchButton;
public VideoController(Context context) {
super(context);
searchButton = new Button(context);
searchButton.setText("Search");
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
System.out.println("Adding a button");
addView(searchButton, params);
//updateViewLayout(this, params);
}
#Override
public void hide() {
}
}
what I am doing wrong here. Any suggestions will be helpful.
Thanks in advance.
You have to override setAnchorView in your VideoController class:
#Override
public void setAnchorView(View view) {
super.setAnchorView(view);
Button searchButton = new Button(context);
searchButton.setText("Search");
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
addView(searchButton, params);
}
Actually that happens because media controller's view is constructed later (in makeControllerView method). So you need to override it and add button there.
Unfortunately, it is hidden at the moment. And overriding setAnchorView seems the best solution.