How to avoid AlertDialog positive button to become invisible? - android

My app uses android API 21 (android 5) with the Theme.Material.Light.DarkActionBar theme.
In an AlertDialog I add positive, negative and neutral buttons. On small screens this can cause the positive to be hidden if the (translated!) button texts are too long to fit on the (portrait) screen. Previous versions of android did not have this problem (text was wrapped to make the buttons fit). The app will work OK if the neutral button is hidden, but I need the positive button to be visible at all times and regardless of translations. Is there any way to enforce this, other than adding "normal" buttons in the layout?
Alt: is ther any way to check if buttons will fit? So I can at runtime decide not to use the neutral button?

I found one solution that more or less works. In the onResume() method I modify the neutral button like so:
public void onResume() {
super.onResume();
final Button btn = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEUTRAL);
if (btn != null) {
if (Build.VERSION.SDK_INT >= 21) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
int maxWidth = (metrics.widthPixels / 3);
btn.setSingleLine(false);
btn.setMaxWidth(maxWidth);
btn.setMaxLines(2);
btn.setEllipsize(TextUtils.TruncateAt.END);
// something in the above code reverts the allCaps
btn.setAllCaps(true);
}
}
Setting maxWidth works, but I have not found a way to accurately calculate the actual max width that would cause the positive button to disappear. The code above unconditionally limits to a third of the display width, which seems to work OK in my case.

The issue can be solved at all places by creating custom view ( Dialog with custom view )which will have buttons as the bottom. set onclick listeners to all and handle separately.

Related

How can I expand an EditText when it is not focused?

I'm doing a message view like whats app and I want my EditText in the bottom to have the "send" button/icon if it's focused and if it's not, then hide/delete that button and expand the edit text. I don't know how to do the the expand and delete thing in run time. If anyone has some clue I would be really thankful.
Use OnFocusListener to trigger your desired events whenever focus of EditText changes.
Use setVisibility to hide and show your Button, and change width of EditText via getLayoutParams().width
m_editText.setOnFocusChangeListener((v, hasFocus) -> {
if (hasFocus) {
m_button.setVisibility(View.GONE);
m_editText.getLayoutParams().width=400;
} else {
m_button.setVisibility(View.VISIBLE);
m_editText.getLayoutParams().width=200;
}
});
You can also turn your size to dp with:
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, YOUR_SIZE_HERE, getApplication().getResources().getDisplayMetrics());

How to display a Button inline within a multiline TextView

I have to embed a button "inline" within a multiline textview, like so:
I'm not sure how to do this in Android, as there are no layout elements or attributes (that I'm aware of) that allow aligning a Button where text ends in a multiline TextView. And I really don't want to use a WebView for this and do it with html / css, as this screen is already heavy enough. Any ideas how to accomplish it?
ClickableSpan is the answer to your problem!
Android strings accept unicode and html characters, which includes arrows
You can create a textview(tvDescription) with the maxLines=5 and ellipsize=end
I'm assuming the maximum lines you wan to show is 5
android:ellipsize="end"
android:maxLines="5"
Add a view(btnIndicator currently button. You can change as per your requirements) which always stays on the bottom right of the textView. This view contains the text (View More and View Less). Suggest you to use constraint layout which will make your task easy.
Assuming you have got the reference to Textview as tvDescription.
And the view at the bottom right as btnIndicator. Add onClickListener to this view.
btnIndicator.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int maxLines = tvDescription.getLineCount();
if (maxLines == 5) {
maxLines = Integer.MAX_VALUE;
btnIndicator.setText("View Less")
}
else {
maxLines = 5;
btnIndicator.setText("View More")
}
/*
* Here you can append the text VIEW MORE(when max lines is 5)
* or ignore if it is already expanded
* */
tvDescription.setMaxLines(maxLines);
}
});

Is there a way to extend AlertDialog and then totally take control of its appearance?

I'm looking at the documentation here:
http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents
And all their examples seem to overlook the developer's potential desire to totally alter the appearance and/or positioning of the OK | Cancel buttons.
I feel like the solution with the DialogFragment comes close, but still there's no obvious way for me to define what View in my activity should BE the OK button, so that I can easily attach the callback to it.
What am I missing? Or is it really not possible to manipulate the appearance and position of the buttons in anything that extends AlertDialog or DialogFragment?
You can use Dialog and add your own custom layout to it and control the appearance of every view in it. Here is an example how you can do this :
final Dialog alert = new Dialog(FingerPaintActivity.this, android.R.style.Theme_Light_Panel);
alert.requestWindowFeature(Window.FEATURE_NO_TITLE); // no title
alert.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation; // this is used for custom animation when dialog is showing and hiding
alert.setContentView(getLayoutInflater().inflate(R.layout.stamps, null)); // here is your custom layout
alert.getWindow().setLayout(width-50, (height-100)); // set height / width
and you can use set listeners to your views (for example a button) like this :
Button myOkBtn = (Button) alert.findViewById(R.id.myOkBtn);
myOkBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

Dynamically change the width of a button in Android

My question is very simple, I'm trying to dynamically change the width of this button :
<Button>
android:layout_height="35dip"
android:background="#drawable/buttonlesson"
android:text="Level 3 [TAP HERE]"
android:onClick="MenuLI1L3"
android:layout_width="300dp"
android:id="#+id/II3"
</Button>
Here is the code I use :
Button myButton = (Button) findViewById(R.id.II3);
myButton.setWidth(10);
myButton.setText("kl");
Text indeed change but not the width. Sound like there is a bug.
There is another button on its right which suppose to fill the gap when this button is reduced to 10 pixel, so I can't change the LinearLayout above too.
Any explanation & solution ? It should work no? Thanks
I assume wrap_content doesn't work for your in your specific case, right?
If you need absolute width, then you need to assign that via new LayoutParameters, i.e.
myButton.setLayoutParams(new LinearLayout.LayoutParams(
30 * someDensityFactor, LinearLayout.LayoutParams.WRAP_CONTENT
))
where the someDensityFactor is your screen density (float). You also might need to invalidate your layout then as well in order to get the button repainted.
As a suggestion try calling invalidate() on the parent view (which will cause drawing invocations to all the children - including your button). This might not work because what you also need is for the button to re-run its onMeasure() logic (which runs prior to drawing).
Play with either invalidating or any other method which will cause the parent to invoke the onMeasure of the children.

pop-up type view (z-index?) - android

I want to add a new view to my scene that will contain content that will change programatically throughout the course of my application. When it does change, it needs to pop up on the screen for 3 seconds (or a click) then disappear.
This view will change in size according to its content WRAP_CONTENT, but ideally I'd like it centered horizontally and vertically on the screen.
I'm stuck on three parts:
1) what type of view should I use for this...I was thinking Relative, but all of my playing with it has yielded no good results for what I'm trying to do
2) with respect to #1 (trying relative view), I could not get it centered properly (tried using param.leftMargin and param.topMargin with varying values but could not get it to work on different devices with different resolutions
3) also with respect to #1, I couldn't make this float over everything else on my screen (need something like a z-index or the like).
any ideas, code examples would be wonderful.
TIA
Use a custom dialog, i.e. a LinearLayout with android:theme="#android:style/Theme.Dialog" and for the class, it would be something like
public class YourCustomDialog extends Dialog implements DialogInterface
where you can implement you custom logic of what to display. Such dialog is floating and modal on top of all other views then and you can also optionally set the background to blurry, etc.
This is a typical constructor of my custom dialog - the layout would be defined in an xml layout file, which in my case is my_custom_dialog.xml:
public MyCustomDialog(Context context) {
super(context, android.R.style.Theme);
Window window = getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.empty));
setContentView(R.layout.my_custom_dialog);
// actually not necessary as it's already the default value:
window.setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
...
}

Categories

Resources