Jetpack compose LazyColumn or Scrollable Column and IME padding for TextField doesn't work - android-softkeyboard

I am trying to set a list if text fields, and when user set the focus on one text field at the bottom I expect that the user can see the appearing IME soft keyboard and the text field being padded according to the configuration set in my manifest file android:windowSoftInputMode="adjustPan", but it doesn't work the first time, it works only when some of the listed textfield have already a focus.
Behavior in video.
My code in onCreate method.
// Turn off the decor fitting system windows, which allows us to handle insets,
// including IME animations
WindowCompat.setDecorFitsSystemWindows(window, false)
setContent {
// Provide WindowInsets to our content. We don't want to consume them, so that
// they keep being pass down the view hierarchy (since we're using fragments).
ProvideWindowInsets(consumeWindowInsets = false) {
MyApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background, modifier = Modifier.systemBarsPadding()) {
Column(modifier = Modifier.fillMaxHeight()) {
val list: List<#Composable () -> Unit> = (1..10).map {
{
Text(text = "$it")
Divider()
TextField(value = "", onValueChange = {}, modifier = Modifier.navigationBarsWithImePadding(),)
}
}
LazyColumn(modifier = Modifier.fillMaxSize().weight(1F)) {
itemsIndexed(list) { index, inputText ->
inputText()
}
}
}
}
}
}
}

If your problem is going on, check Insets for Jetpack Compose.
Only add this dependency:
implementation 'com.google.accompanist:accompanist-insets:{insetsVersion}'
and use Modifier.imePadding() or the custom solution:
#Composable
fun ImeAvoidingBox() {
val insets = LocalWindowInsets.current
val imeBottom = with(LocalDensity.current) { insets.ime.bottom.toDp() }
Box(Modifier.padding(bottom = imeBottom))
}

Related

Using Zxing Library with Jetpack compose

I am trying to implement qr scanner using zxing library. For this, i have added a button on screen, and on click of it, i am launching scanner as below
Button(
onClick = {
val intentIntegrator = IntentIntegrator(context)
intentIntegrator.setPrompt(QrScanLabel)
intentIntegrator.setOrientationLocked(true)
intentIntegrator.initiateScan()
},
modifier = Modifier
.fillMaxWidth()
) {
Text(
text = QrScanLabel
)
}
but, it launches an intent, which expects onActivityResult method to get back the results. And Jetpack compose uses rememberLauncherForActivityResult like below
val intentLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartIntentSenderForResult()
) {
if (it.resultCode != RESULT_OK) {
return#rememberLauncherForActivityResult
}
...
}
but how do we integrate both things together here?
I make a provisional solution with same library:
Gradle dependencies:
implementation('com.journeyapps:zxing-android-embedded:4.1.0') { transitive = false }
implementation 'com.google.zxing:core:3.4.0'
My new Screen with jetpack compose and camera capture, that works for my app:
#Composable
fun AdminClubMembershipScanScreen(navController: NavHostController) {
val context = LocalContext.current
var scanFlag by remember {
mutableStateOf(false)
}
val compoundBarcodeView = remember {
CompoundBarcodeView(context).apply {
val capture = CaptureManager(context as Activity, this)
capture.initializeFromIntent(context.intent, null)
this.setStatusText("")
capture.decode()
this.decodeContinuous { result ->
if(scanFlag){
return#decodeContinuous
}
scanFlag = true
result.text?.let { barCodeOrQr->
//Do something and when you finish this something
//put scanFlag = false to scan another item
scanFlag = false
}
//If you don't put this scanFlag = false, it will never work again.
//you can put a delay over 2 seconds and then scanFlag = false to prevent multiple scanning
}
}
}
AndroidView(
modifier = Modifier,
factory = { compoundBarcodeView },
)
}
Since zxing-android-embedded:4.3.0 there is a ScanContract, which can be used directly from Compose:
val scanLauncher = rememberLauncherForActivityResult(
contract = ScanContract(),
onResult = { result -> Log.i(TAG, "scanned code: ${result.contents}") }
)
Button(onClick = { scanLauncher.launch(ScanOptions()) }) {
Text(text = "Scan barcode")
}
Addendum to the accepted answer
This answer dives into the issues commented on by #Bharat Kumar and #Jose Pose S
in the accepted answer.
I basically just implemented the accepted answer in my code and then added the following code just after the defining compundBarCodeView
DisposableEffect(key1 = "someKey" ){
compoundBarcodeView.resume()
onDispose {
compoundBarcodeView.pause()
}
}
this makes sure the scanner is only active while it is in the foreground and unbourdens our device.
TL;DR
In escence even after you scan a QR code successfully and leave the scanner screen, the barcodeview will "haunt" you by continuing to scan from the backstack. which you usually dont want. And even if you use a boolean flag to prevent the scanner from doing anything after the focus has switched away from the scanner it will still burden your processor and slow down your UI since there is still a process constantly decrypting hi-res images in the background.
I have a problem, I've the same code as you, but i don't know why it's showing me a black screen
Code AddProduct
#ExperimentalPermissionsApi
#Composable
fun AddProduct(
navController: NavController
) {
val context = LocalContext.current
var scanFlag by remember {
mutableStateOf(false)
}
val compoundBarcodeView = remember {
CompoundBarcodeView(context).apply {
val capture = CaptureManager(context as Activity, this)
capture.initializeFromIntent(context.intent, null)
this.setStatusText("")
capture.decode()
this.decodeContinuous { result ->
if(scanFlag){
return#decodeContinuous
}
scanFlag = true
result.text?.let { barCodeOrQr->
//Do something
}
scanFlag = false
}
}
}
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { compoundBarcodeView },
)
}

Automatic update of Button text on change

I'm currently learning scala, and making an encryption program with a basic scala swing UI.
I added 2 swing buttons which text is held by 2 var.
The code looks like this :
var encText = "Encrypt"
var decText = "Decrypt"
def top = new MainFrame {
title = "Data Guardian"
minimumSize = new Dimension(500, 200)
contents = new GridPanel(2, 2) {
hGap = 3; vGap = 3
contents += new Button {
text = encText
reactions += {
case ButtonClicked(_) => Main.startEnc
}
}
contents += new Button {
text = decText
reactions += {
case ButtonClicked(_) => Main.startDec
}
}
}
size = new Dimension(150, 40)
}
Those "text" var will be changed often during the encryption/decryption process by various methods, but when they do change, the text displayed on the buttons doesn't.
I'd like to know a way to make the displayed text of the buttons automatically change when the var that holds that text changes.
Thanks a lot for your insight :)
Make the strings private and write getters/setters that change the button text as a side-effect.
You'll need to give the buttons names, rather than having anonymous instances as you do above.

Add and bind UIView on External Monitor using MvvmCross

I'm attempting to Display an UIView on an external monitor in iOS if one is detected. I am able to detected and display a simple UIView using the following code...
public void CheckForExternalDisplay()
{
if (UIScreen.Screens.Length > 1)
{
Mvx.Trace(MvxTraceLevel.Diagnostic, "Multiple screens found");
var externalScreen = UIScreen.Screens[1];
var rect = new RectangleF(new PointF(0, 0), externalScreen.AvailableModes.Max(m => m).Size);
var window = new UIWindow(rect)
{
Screen = UIScreen.Screens[1],
ClipsToBounds = true,
Hidden = false
};
var presenterView = new PresenterView(rect);
window.AddSubview(presenterView);
}
}
This UIView is Very Simple. It contains a UILabel and a RadialProgress View. Most of the heavy lifting to determine what the values should be are already being done on another viewmodel that is updating a view attached to a screen on the phone. I have tried several techniques to try and get the UIView on the external display to update.
Using MvxMessenger. - I tried passing a message to both a new ViewModel and to the View itself. The new ViewModel received the message only after I created a new instance from the publishing viewmodel. However, I could never intercept messages directly from the view...
Delay binding and regular fluent binding where the bound viewmodel properties are simply updated from another viewmodel.
Attempted to bind this View with a viewmodel already associated with another view.
Wishing in one hand, and crapping in the other... Guess which one filled up first ;)
It's almost as if the UIview (below), isn't being registered/associated with a viewmodel. I'm sure I'm missing something somewhere. As always, I appreciate the help!
public sealed class PresenterView
: MvxView
{
private readonly RadialProgressView _progressView;
private readonly MvxSubscriptionToken _token;
private IMvxMessenger _messenger;
private UILabel _displayLabel;
public PresenterView(RectangleF frame)
: base(frame)
{
Frame = frame;
_messenger = Mvx.Resolve<IMvxMessenger>();
_token = _messenger.Subscribe<DisplayMessage>(OnDisplayMessageReceived);
_displayLabel = new UILabel
{
AdjustsFontSizeToFitWidth = true,
Lines = 1,
LineBreakMode = UILineBreakMode.TailTruncation,
Text = "This is a workout",
Font = UIFont.FromName("rayando", 96f),
BackgroundColor = UIColor.Clear,
PreferredMaxLayoutWidth = Frame.Width - 10,
Frame = new RectangleF(0, 0, Frame.Width - 10, frame.Height / 7),
TextColor = UIColor.White,
TextAlignment = UITextAlignment.Center,
AutoresizingMask = UIViewAutoresizing.All
};
AddSubview(_displayLabel);
_progressView = new RadialProgressView
{
Center = new PointF(Center.X, Center.Y),
MinValue = 0f,
};
AddSubview(_progressView);
this.DelayBind(() =>
{
MvxFluentBindingDescriptionSet<PresenterView, PresenterViewModel> set =
this.CreateBindingSet<PresenterView, PresenterViewModel>();
set.Bind(_progressView).For(pv => pv.Value).To(vm => vm.ClockValue);
set.Bind(_progressView).For(pv => pv.MaxValue).To(vm => vm.MaxProgress);
set.Bind(_workoutLabel).To(vm => vm.DisplayText);
set.Apply();
});
}
private void OnDisplayMessageReceived(DisplayMessage obj)
{
_workoutLabel.Text = obj.Message;
}
}
I do realize that I have included both solutions here. I did try each of them independently.
From the code you've posted, I can't see anywhere you are actually setting the data context for your view.
In 'normal mvvmcross' either:
an MvxViewController creates its own DataContext (ViewModel) using its show request in viewDidLoad
some other app code sets an MvxView's DataContext based on app- specific logic - see n=32 in http://mvvmcross.wordpress.com as an example
In your code, i can't currently see where you set this - so try setting view.DataContext somewhere.

how to check image value with tab host

Ok so i will try to explain what im trying to do as clearly as possible with examples.
i have a tabhost with 5 tabs (engine,body,photo,spec,colors).
the upper portion of the xml layout(car.xml) has an imageView1.
and the and the lower portion of the layout car.xml holds the tabs. each tab calls an intent.
such as "" car.this.coupe_specs = new Intent(this, coupe_tab.class); ""
and coupe_tab.xml layout has 1 textView.
now what i am trying to do is change the text in the textView field in coupe_tab.xml
after checking the id: of the imageView1 on the car.xml.
so basicly i want to from car.java check the value of a ImageView in car.java and if true change the text in the TextView in coupe_tab.java the problem i seem to be having is doing all this inside a onItemSelected function.
non functional example just to help you under stand my wants
example:
import com.myexample._coupe_tab;
public void onItemSelected(AdapterView<?> example int view) {
if ("car.png").equals imageView1 {
textView1 = "example text 1";
}
else if ("car2.png).equals imageView1 {
textView1 = "example text 2";
}
else {
// do something else
}

Make Webview's auto links visible

A Webview will display links in the content HTML as having blue underlines. So if you have something in the HTML like
blah blah
... it is clearly visible as a link.
The Webview also allows you to click on phone numbers and addresses (even if those are just text in the HTML, not links) to launch the Dialer or Maps.
How can one get Webview to display those (Linkify, probably) links with underlines etc? It's easy enough in a TextView since one can get the spans from a TextView and style them, but Webview doesn't expose any way to retrieve that data... at least not that I can see looking through the docs.
Here is some JS code which can be injected to linkify phone numbers, emails and urls:
function linkify() {
linkifyTexts(linkifyPhoneNumbers);
linkifyTexts(linkifyEmails);
linkifyTexts(linkifyWebAddresses1);
linkifyTexts(linkifyWebAddresses2);
}
function linkifyPhoneNumbers(text) {
text = text.replace(/\b\+?[0-9\-]+\*?\b/g, '$&');
return text;
}
function linkifyEmails(text) {
text = text.replace(/(\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim, '$1');
return text;
}
function linkifyWebAddresses1(text) {
text = text.replace(/(\b(https?|ftp):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/gim, '$1');
return text;
}
function linkifyWebAddresses2(text) {
text = text.replace(/(^|[^\/])(www\.[\S]+(\b|$))/gim, '$1$2');
return text;
}
var linkifyTexts = function(replaceFunc)
{
var tNodes = [];
getTextNodes(document.body,false,tNodes,false);
var l = tNodes.length;
while(l--)
{
wrapNode(tNodes[l], replaceFunc);
}
}
function getTextNodes(node, includeWhitespaceNodes,textNodes,match) {
if (node.nodeType == 3) {
if (includeWhitespaceNodes || !/^\s*$/.test(node.nodeValue)) {
if(match){
if(match.test(node.nodeValue))
textNodes.push(node);
}
else {
textNodes.push(node);
}
}
} else {
for (var i = 0, len = node.childNodes.length; i < len; ++i) {
var subnode = node.childNodes[i];
if (subnode.nodeName != "A") {
getTextNodes(subnode,includeWhitespaceNodes,textNodes,match);
}
}
}
}
function wrapNode(n, replaceFunc) {
var temp = document.createElement('div');
if(n.data)
temp.innerHTML = replaceFunc(n.data);
else{
//whatever
}
while (temp.firstChild) {
n.parentNode.insertBefore(temp.firstChild,n);
}
n.parentNode.removeChild(n);
}
Given this:
http://code.google.com/p/android/issues/detail?id=742
it still doesn't seem to be a way to do this from Java directly. One thing that might work is to write some JavaScript code and run it after page is loaded, e.g. as given here:
In Android Webview, am I able to modify a webpage's DOM?
Here's an example of a similar thing:
Disabling links in android WebView
where the idea is to disable links. You may be able to use a similar approach to add some CSS, including underlining. A couple of other SOqs / links that might help:
Android: Injecting Javascript into a Webview outside the onPageFinished Event
Android: Injecting Javascript into a Webview outside the onPageFinished Event
http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview
Injecting Javascript into a Webview outside the onPageFinished Event (Using DatePicker to set a date on an input of a WebView)
Hope this helps.