Tkinter

How

can

I

dynamically

create

a

widget

that

can

then

be

destroyed

or

removed

Ask

Question

I

am

looking

for

a

method

to

create

widgets

most

likely

a

Label

ad

nauseam

with

the

caveat

that

they

can

be

removed

or

unpacked

later

on

I

can

generate

the

widgets

just

fine

but

they

are

not

assigned

a

name

I

do

not

understand

how

I

would

if

it

is

possible

remove

a

certain

anonymous

widget

My

first

instinct

was

to

dynamically

create

variable

names

with

a

stable

convention

but

that

may

unnecessarily

open

a

can

of

worms

The

idea

is

expressed

below

Id

like

to

be

able

to

remove

a

certain

Button

widget

while

not

knowing

at

runtime

how

many

I

will

handle

Thank

you

from

Tkinter

import

import

time

import

ttk

def

onDoubleClick

event

item

t

selection

print

you

clicked

on

titemitemtext

if

t

item

item

text

Start

IO

Button

text

command

but

pack

def

but

root

Tk

root

geometry

Frame

root

Frame

root

t

ttk

Treeview

t

heading

text

Test

steps

t

insert

IO

text

IO

t

insert

IO

end

text

Start

t

bind

onDoubleClick

t

pack

pack

side

LEFT

pack

side

LEFT

EDIT

My

feature

request

was

admittedly

shortsighted

My

ultimate

goal

is

to

have

a

Label

widget

and

a

Button

sidebyside

both

comprising

what

is

to

be

a

step

in

a

test

launcher

Clicking

the

button

will

remove

both

itself

and

its

respective

Label

from

the

GUI

Im

able

to

create

both

widgets

and

delete

either

one

of

them

on

the

Buttons

callback

but

to

both

I

believe

I

need

to

def

a

function

I

believe

my

problem

lies

in

passing

a

correct

reference

to

def

removeStep

A

use

case

is

diagrammed

below

If

this

could

be

solved

my

RTFM

please

feel

free

to

let

me

know

I

just

couldnt

find

it

TEST

Make

a

PBJ

Step

Get

Bread

Remove

step

Step

Smear

PB

Remove

step

Step

Smear

Jelly

Remove

step

python

userinterface

tkinter

improve

this

question

asked

Feb

at

trstowell

Answers

oldest

Youll

want

to

store

the

dynamicallycreated

widgets

in

a

list

Have

something

like

def

onDoubleClick

event

button

Button

append

button

button

pack

You

can

then

access

the

buttons

for

removal

with

say

destroy

Edit

With

more

information

about

your

use

case

I

would

probably

do

class

RemovableTask

Frame

def

self

master

name

options

Frame

self

master

options

lbl

Label

self

text

name

btn

Button

self

text

Remove

step

command

self

destroy

lbl

grid

row

column

btn

grid

row

column

Then

just

create

instances

of

RemovableTask

with

names

like

Step

Get

Bread

and

grid

or

pack

them

in

a

column

Everything

else

would

be

handled

automatically

answered

Feb

at

Abe

Karplus

You

have

a

few

options

here

The

first

option

is

to

store

your

dynamically

created

buttons

in

a

list

Then

you

can

addremove

buttons

whenever

you

want

And

you

keep

a

reference

to

all

of

the

buttons

Clean

and

simple

A

second

option

is

to

use

the

config

method

after

youve

created

the

button

and

have

a

reference

to

it

to

destroy

the

widget

Or

at

least

remove

it

from

the

display

doesnt

actually

destroy

the

widget

It

could

be

repacked

later

To

actually

destroy

a

widget

you

want

to

call

widgetdestroy

instead

import

Tkinter

as

tk

root

tk

Tk

def

b

tk

root

text

Click

to

destroy

b

pack

b

config

command

b

b

tk

root

text

command

b

pack

root

mainloop

answered

Feb

at

mgilson

If

all

that

should

happen

when

a

button

is

clicked

is

to

remove

it

this

is

cleaner

However

the

config

version

gets

messy

if

you

want

the

button

to

do

any

more

than

just

remove

itself

Abe

Karplus

Feb

at

AbeKarplus

It

really

depends

on

what

OP

is

trying

to

achieve

with

the

buttons

Ultimately

the

list

solution

has

the

problem

of

somehow

keeping

track

of

which

button

is

at

which

index

However

this

second

solution

is

problematic

as

you

dont

have

a

good

reference

to

the

button

in

question

Its

easy

to

have

the

button

perform

other

actions

before

destroying

itself

however

bconfigcommandlambdado_something_with_bb

but

ultimately

youre

right

that

unless

that

function

finds

a

way

to

destroy

b

its

going

to

be

tricky

to

do

get

rid

of

it

any

other

way

mgilson

Feb

at

This

seems

like

a

memory

leak

to

me

you

remove

the

widget

from

view

but

you

never

destroy

it

Bryan

Oakley

Feb

at

BryanOakley

hence

my

comment

about

not

actually

destroying

the

widget

Anyway

Ive

elaborated

a

little

more

about

widgetdestroy

and

added

emphasis

to

my

original

comment

Thanks

for

the

comment

mgilson

Feb

at

Not

the

answer

youre

looking

for

Browse

other

questions

tagged

python

userinterface

tkinter

or

ask

your

own

question

asked

years

months

ago

viewed

times

active

years

months

ago

Blog

Years

of

Stack

Giving

Back

question

feed

B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


What is a seed in terms of generating a random number duplicate Ask Question 25 13 This question already has an answer here What does seeding mean 4 answers What is a seed in terms of generating a random number I need to generate hundreds to thousands of random numbers I have read a lot about using a seed What is a seed Is a seed where the random numbers start from For example if I set my seed to be 5 will it generate numbers from 5 to whatever my limit is So it will never give me 3 for example I am using C so if you provide any examples itd be nice if it was in C Thanks c random seeding improve this question asked Feb 16 13 at 2006 KKendall 4841 15 59 101 marked as duplicate by phuclv πάντα ῥεῖ c Jul 27 16 at 545 This question has been asked before and already has an answer If those answers do not fully address your question please ask a new question 2 Answers active oldest 33 What is normally called a random number sequence in reality is a pseudorandom number sequence because the values are computed using a deterministic algorithm and probability plays no real role The seed is a starting point for the sequence and the guarantee is that if you start from the same seed you will get the same sequence of numbers This is very useful for example for debugging when you are looking for an error in a program you need to be able to reproduce the problem and study it a nondeterministic program would be much harder to debug because every run would be different If you need just a random sequence of numbers and dont need to reproduce it then simply use current time as seed for example with srand time NULL edited Jan 25 16 at 1849 answered Feb 16 13 at 2012 6502 855k 13 114 214 Well yes srand to seed rand But the rich new random number generators in TR1 okay not so new and in C11 all have their own mechanics for seeding and srand is not involved Pete Becker Feb 16 13 at 2143 PeteBecker my wild guess is that for what the OP needs srandrand is more than enough 6502 Feb 17 13 at 031 Yes probably Still seeding isnt just about srand g Pete Becker Feb 17 13 at 1300 6502 why you passed null as an argument to time function Can I pass anything other than NULL Destructor Oct 17 15 at 517 1 PravasiMeet time for historical reasons dating back to prestandard C accepts a pointer where to store the time_t value that is also returned by the function Normally modern code never needs to pass anything but NULL when calling time 6502 Oct 17 15 at 619 4 So lets put it this way if you and your friend set the seed equals to the same number by then you and your friend will get the same random numbers So if all of us write this simple program include iostream using namespace std void main srand 0 for int i 0 i 3 i int x rand 11 range between 0 and 10 cout x endl We all will get the same random numbers which are 5 8 8 And if you want to get different number each time you can use srandtime edited Dec 25 18 at 1117 answered Apr 11 16 at 1217 Anwarvic 953 10 20 Not the answer youre looking for Browse other questions tagged c random seeding or ask your own question asked 5 years 10 months ago viewed 34638 times active 10 days ago Blog Adios to Winter Bash 2018 Hot Network Questions

O O O B O O O B I I I O O O O O O O O O O O O O O O O O O O O O B O O O B I I I O O O O O O O O O O O O O O O O O O B O O O B O O B O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


Model backing a DB Context has changed Consider Code First Migrations Ask Question 38 15 The model backing the MyDbContext context has changed since the database was created Consider using Code First Migrations to update the database httpgomicrosoftcomfwlinkLinkId238269 What causes this to happen Ive literally just created a brand new database and have changed nothing but every time I try to access a model from a controller it throws this It has something to do with the fact that I was attempting to share a connection string ie a database with two separate entities aspnetmvc entityframework improve this question asked Feb 19 13 at 112 Brian D 4043 13 49 87 As long as both entities are in the same DbContext then that shouldnt be a problem You configure the DbContext to use a connection string and all entities will use it AaronLS Feb 19 13 at 139 Ive had this same issue but my workaround has been to enablemigrations in the package manager console I have no idea why this works droo46 Jun 11 15 at 1533 asked 5 years 10 months ago viewed 103644 times active 1 month ago Blog Adios to Winter Bash 2018 Hot Network Questions Does a player know if their Intimidation attempt worked Asking about eating policy during interview at the end of command Why are some specific seats marked as prioritized for people in need Is it a bad idea to have a character that has an imaginary friend Is my chlorine sample real New manager is a jerk is it OK to quit without providing a reason Count lines containing word draw two boxes on a slanted plane in a mechanical illustration What does Matthew 95 mean How do you say in all directions in a single word The Grand Heist J H C Whitehead and his pig Awk replace one character only in a certain column Problem in evaluating logarithm derivatives I got accepted into a math PhD program but I dont feel adequate enough to attend Why have all negatives been exposed to one frame Daily standup vs Micromanagement Did new Brazilian president Bolsonaro say hed rather have a dead son than a gay one A critic made a comment that my female character sounds like she was written by a man Examples of words that are monomorphemic in English but polymorphemic in other languages What is the movie referenced in Black Panther with the automatic shoes When to move a common field into a base class Is this projected return from Wealthify too low question feed

O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


Running jQuery in Project Details Page 0 I am trying to limit the length of the Project Notes Custom Field in Project Details Page PDP using Project Server 2010 I am using the following jQuery in a Content Editor Web Part added to the PDP script src httpcodejquerycomjquerylatestjs type textjavascript script script document ready function var textArea textareatitleProject Notes textArea keypress function var text textArea text if text length 10 alert Project Notes can not exceed 100 characters in length textArea val text substring 0 9 script However when the user types into the text area nothing is happeningThe event does not fire I have tried various modifications to the code but the same result Please let me know what I am missing Thank you jquery msprojectserver2010 improve this question asked Mar 3 13 at 1059 Moe 8 3 2 Answers oldest 0 You can use maxlength attribute for html element textarea maxlength 10 rows 5 cols 25 textarea and if we go with your script then use val instead of text form elements does have values where you have to input the text whether that is input type text or textarea var textArea textareatitleProject Notes textArea keypress function var text textArea val if text length 10 alert Project Notes can not exceed 100 characters in length textArea val text substring 0 9 You can try this out in fiddle here answered Mar 3 13 at 1141 Jai 635k 9 54 79 Thank you for your comments however the suggested solution does not work The problem is that the event is not firing inside the Project Server page Even if I remove the the code inside the keypress function and keep a simple alert it still does not execute as well I tried using keyup instead of keypress but same result Events are not firing when the user types into the text area Moe Mar 4 13 at 539 0 I have changed the text area to a text box and the events started firing normally answered Mar 5 13 at 555 Moe 8 3 Your Answer By clicking Post Your Answer you acknowledge that you have read our updated terms of service and cookie policy and that your continued use of the website is subject to these policies Not the answer youre looking for Browse other questions tagged jquery msprojectserver2010 or ask your own question

O B O B I I O O O O O O O O O O O O O O O B I I O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O


How to sql select a product with two status 1 favorite I have a Products table with ProductId and StatusId columns How can i select all the products which have StatusId 1 and 2 two rows sql sqlserver database tsql improve this question edited Mar 12 13 at 1038 Taryn 188k 45 286 351 asked Mar 12 13 at 1012 Elad Benda 126k 58 179 333 i think the question is unclear Maybe you can post some examples of your data Obl Tobl Mar 12 13 at 1019 3 Answers active oldest 6 accepted To select Products with both status 12 SqlServer fiddle demo SELECT ProductId FROM products WHERE status IN 1 2 GROUP BY ProductId HAVING COUNT distinct status 2 answered Mar 12 13 at 1024 Kaf 265k 4 39 59 2 Try this query to get records for product having both the status 1 and 2 SELECT DISTINCT a ProductId FROM Products a Products b WHERE a ProductId b ProductId AND a StatusId 1 AND b StatusId 2 USING ANSI JOIN SELECT DISTINCT a ProductId FROM Products a INNER JOIN Products b ON a ProductId b ProductId WHERE a StatusId 1 AND b StatusId 2 Fiddle edited Mar 12 13 at 1041 answered Mar 12 13 at 1018 Meherzad 7139 22 38 1 This query is wrong and it wont select anything aF Mar 12 13 at 1020 Check this fiddle its working Meherzad Mar 12 13 at 1023 aStatusId 1 AND bStatusId 2 its impossible to select both You need to use IN or replace it with aStatusId 1 OR bStatusId 2 aF Mar 12 13 at 1024 a and b are different table and OP has mentioned that there are 2 rows for record so this condition will return true for that Check this fiddle sqlfiddlecom21f9b01 Meherzad Mar 12 13 at 1025 1 Indeed didnt noticed that But its a hell of a way to do it P aF Mar 12 13 at 1026 show 4 more comments 0 SELECT DISTINCT a ProductId FROM Products a INNER JOIN Products b ON a ProductId b ProductId WHERE a StatusId IN 1 2 answered Mar 12 13 at 1020 aF 31k 34 112 172 fyi This query will select all the rows from table with status 1 or 2 Kaf Mar 12 13 at 1039 Not the answer youre looking for Browse other questions tagged sql sqlserver database tsql or ask your own question

O O B O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O


Using a variable as a sheet name Ask Question 1 I am getting a RunTime 13 error when trying to use a variable for a sheetname as per below Sub inputdata Set asheet1 ThisWorkbook Worksheets input Range D12 Set rangeDate ThisWorkbook Worksheets input Range inputdate Range F12M12 Copy Sheets asheet1 It is erroring on the line Sheetsasheet1Select Any help would be great thanks excel vba excelvba improve this question asked Apr 17 13 at 1338 subtilty 18 1 2 5 A general hint is to always use Option Explicit Tools Options Editor Require Variable Declaration which will force you to dimension all variables and objects you use If you would have asheet1 dimensioned as a Worksheet it would never allow to be Set to a Range K_B Apr 17 13 at 1355 2 Answers oldest 3 The asheet1 is not a string you are asigning a range object to it You should declare asheet1 as string and the change this line to Dim asheet1 as string asheet1 ThisWorkbook Worksheets input Range D12 Value That should make it work Edit removed the Set keyword from the string var answered Apr 17 13 at 1343 Ateszki 11 11 1 1 for the short answer shahkalpesh Apr 17 13 at 1348 Thank you for your answer unfortuately it gives the error of object required on Set asheet1 subtilty Apr 17 13 at 1348 subtilty Im sorry I should have removed the Set keyword since asheet1 is a string and no longer an object Ateszki Apr 17 13 at 1350 Thank you for your help that has helped alot subtilty Apr 17 13 at 1353 subtilty you are welcome Ateszki Apr 17 13 at 1353 show 4 more comments 2 Option Explicit Sub inputdata dim inputSheet as WorkSheet dim aCellOnInputSheet as Range dim inputDateCell as Range dim userSheetName as String Set inputSheet ThisWorkbook Worksheets input Set aCellOnInputSheet inputSheet Range D12 userSheetName aCellOnInputSheet Value Set inputDateCell inputSheet Range inputdate Range F12M12 Copy Sheets userSheetName End Sub EDIT A couple of points 1 Option Explicit is a must 2 Define variables and name it appropriately ie define variable which refers to a cellRange with the name range instead of aSheet it confuses the reader answered Apr 17 13 at 1342 shahkalpesh 292k 2 46 81 This is not what he asked for Ateszki Apr 17 13 at 1344 Ateszki I have corrected it Please check Thanks shahkalpesh Apr 17 13 at 1346 I see you edited subtilty Apr 17 13 at 1349 Thank you for the points you gave subtilty Apr 17 13 at 1354 I hope this answer helps the OP on other points shahkalpesh Apr 17 13 at 1355 Not the answer youre looking for Browse other questions tagged excel vba excelvba or ask your own question asked 5 years 8 months ago viewed 16157 times active 5 years 8 months ago question feed

B I I O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


arp request and reply using c socket programming Ask Question 15 11 I am trying to receive and send arp packets using c programming in Linux Ubuntu My program works fine ie runs without any error but I can not trace the packets using Wireshark source code include syssocketh include sysioctlh include systimeh include asmtypesh include mathh include stringh include stdioh include stdlibh include unistdh include signalh include linuxif_packeth include linuxif_etherh include linuxif_arph define BUF_SIZE 42 define DEVICE eth0 define ETH_P_NULL 0x0 define ETH_MAC_LEN ETH_ALEN define ETH_ARP 0x0806 int s 0 Socketdescriptor void buffer NULL long total_packets 0 long answered_packets 0 void sigint int signum struct __attribute__ packed arp_header unsigned short arp_hd unsigned short arp_pr unsigned char arp_hdl unsigned char arp_prl unsigned short arp_op unsigned char arp_sha 6 unsigned char arp_spa 4 unsigned char arp_dha 6 unsigned char arp_dpa 4 int main void buffer void malloc BUF_SIZE Buffer for Ethernet Frame unsigned char etherhead buffer Pointer to Ethenet Header struct ethhdr eh struct ethhdr etherhead Another pointer to ethernet header unsigned char arphead buffer 14 struct arp_header ah unsigned char src_mac 6 our MAC address struct ifreq ifr struct sockaddr_ll socket_address int ifindex 0 Ethernet Interface index int i int length length of received packet int sent printf Server started entering initialiation phasen open socket s socket AF_PACKET SOCK_RAW htons ETH_P_ALL if s 1 perror socket exit 1 printf Successfully opened socket in s retrieve ethernet interface index strncpy ifr ifr_name DEVICE IFNAMSIZ if ioctl s SIOCGIFINDEX ifr 1 perror SIOCGIFINDEX exit 1 ifindex ifr ifr_ifindex printf Successfully got interface index in ifindex retrieve corresponding MAC if ioctl s SIOCGIFHWADDR ifr 1 perror SIOCGIFINDEX exit 1 for i 0 i 6 i src_mac i ifr ifr_hwaddr sa_data i printf Successfully got our MAC address 02X02X02X02X02X02Xn src_mac 0 src_mac 1 src_mac 2 src_mac 3 src_mac 4 src_mac 5 prepare sockaddr_ll socket_address sll_family PF_PACKET socket_address sll_protocol htons ETH_P_IP socket_address sll_ifindex ifindex socket_address sll_hatype ARPHRD_ETHER socket_address sll_pkttype PACKET_OTHERHOST socket_address sll_halen 0 socket_address sll_addr 6 0x00 socket_address sll_addr 7 0x00 establish signal handler signal SIGINT sigint printf Successfully established signal handler for SIGINTn printf We are in production state waiting for incoming packetsn while 1 Wait for incoming packet length recvfrom s buffer BUF_SIZE 0 NULL NULL if length 1 perror recvfrom exit 1 if htons eh h_proto 0x806 unsigned char buf_arp_dha 6 unsigned char buf_arp_dpa 4 ah struct arp_header arphead if htons ah arp_op 0x0001 continue printf buffer is s n char ah printf HD TYPE x PROTO TYPE x n ah arp_hd ah arp_pr printf HD leng x PROTO leng x n ah arp_hdl ah arp_prl printf OPERATION x n ah arp_op printf SENDER MAC address 02X02X02X02X02X02Xn ah arp_sha 0 ah arp_sha 1 ah arp_sha 2 ah arp_sha 3 ah arp_sha 4 ah arp_sha 5 printf SENDER IP address 02d02d02d02dn ah arp_spa 0 ah arp_spa 1 ah arp_spa 2 ah arp_spa 3 if ah arp_spa 0 10 ah arp_spa 1 00 ah arp_spa 2 00 ah arp_spa 3 01 printf Sender ip is bam bamn system sudo arp s 10001 001e7391040d printf TARGET MAC address 02X02X02X02X02X02Xn ah arp_dha 0 ah arp_dha 1 ah arp_dha 2 ah arp_dha 3 ah arp_dha 4 ah arp_dha 5 printf TARGET IP address 02d02d02d02dn ah arp_dpa 0 ah arp_dpa 1 ah arp_dpa 2 ah arp_dpa 3 printf n printf ETHER DST MAC address 02X02X02X02X02X02Xn eh h_dest 0 eh h_dest 1 eh h_dest 2 eh h_dest 3 eh h_dest 4 eh h_dest 5 printf ETHER SRC MAC address 02X02X02X02X02X02Xn eh h_source 0 eh h_source 1 eh h_source 2 eh h_source 3 eh h_source 4 eh h_source 5 memcpy void etherhead const void etherhead ETH_MAC_LEN ETH_MAC_LEN memcpy void etherhead ETH_MAC_LEN const void src_mac ETH_MAC_LEN eh h_proto ETH_ARP printf n printf ETHER DST MAC address 02X02X02X02X02X02Xn eh h_dest 0 eh h_dest 1 eh h_dest 2 eh h_dest 3 eh h_dest 4 eh h_dest 5 printf ETHER SRC MAC address 02X02X02X02X02X02Xn eh h_source 0 eh h_source 1 eh h_source 2 eh h_source 3 eh h_source 4 eh h_source 5 ah arp_hd ntohs ah arp_hd ah arp_pr ntohs ah arp_pr ah arp_op 0x0002 buf_arp_dpa 0 ah arp_dpa 0 buf_arp_dpa 1 ah arp_dpa 1 buf_arp_dpa 2 ah arp_dpa 2 buf_arp_dpa 3 ah arp_dpa 3 ah arp_dha 0 ah arp_sha 0 ah arp_dha 1 ah arp_sha 1 ah arp_dha 2 ah arp_sha 2 ah arp_dha 3 ah arp_sha 3 ah arp_dha 4 ah arp_sha 4 ah arp_dha 5 ah arp_sha 5 ah arp_dpa 0 ah arp_spa 0 ah arp_dpa 1 ah arp_spa 1 ah arp_dpa 2 ah arp_spa 2 ah arp_dpa 3 ah arp_spa 3 ah arp_spa 0 buf_arp_dpa 0 ah arp_spa 1 buf_arp_dpa 1 ah arp_spa 2 buf_arp_dpa 2 ah arp_spa 3 buf_arp_dpa 3 change the sender mac address ah arp_sha 0 0x00 ah arp_sha 1 0x1e ah arp_sha 2 0x73 ah arp_sha 3 0x78 ah arp_sha 4 0x9a ah arp_sha 5 0x0d socket_address sll_addr 0 eh h_dest 0 socket_address sll_addr 1 eh h_dest 1 socket_address sll_addr 2 eh h_dest 2 socket_address sll_addr 3 eh h_dest 3 socket_address sll_addr 4 eh h_dest 4 socket_address sll_addr 5 eh h_dest 5 printf n printf SENDER MAC address 02X02X02X02X02X02Xn ah arp_sha 0 ah arp_sha 1 ah arp_sha 2 ah arp_sha 3 ah arp_sha 4 ah arp_sha 5 printf SENDER IP address 02d02d02d02dn ah arp_spa 0 ah arp_spa 1 ah arp_spa 2 ah arp_spa 3 if ah arp_spa 0 10 ah arp_spa 1 0 ah arp_spa 2 0 ah arp_spa 3 1 printf 10001n printf TARGET MAC address 02X02X02X02X02X02Xn ah arp_dha 0 ah arp_dha 1 ah arp_dha 2 ah arp_dha 3 ah arp_dha 4 ah arp_dha 5 printf TARGET IP address 02d02d02d02dn ah arp_dpa 0 ah arp_dpa 1 ah arp_dpa 2 ah arp_dpa 3 printf HD TYPE x PROTO TYPE x n ah arp_hd ah arp_pr printf HD leng x PROTO leng x n ah arp_hdl ah arp_prl printf OPERATION x n ah arp_op sent sendto s buffer BUF_SIZE 0 struct sockaddr socket_address sizeof socket_address if sent 1 perror sendto exit 1 answered_packets total_packets void sigint int signum Clean up struct ifreq ifr if s 1 return strncpy ifr ifr_name DEVICE IFNAMSIZ ioctl s SIOCGIFFLAGS ifr ifr ifr_flags IFF_PROMISC ioctl s SIOCSIFFLAGS ifr close s free buffer printf Server terminatingn printf Totally received ld packetsn total_packets printf Answered ld packetsn answered_packets exit 0 c linux sockets wireshark improve this question asked May 23 13 at 919 dsharew 7023 3 26 53 Best form ASK Wireshark does it shows packets for other application on your PC Grijesh Chauhan May 23 13 at 925 yeah i can see the packets other applications dsharew May 23 13 at 929 The problem must be from the code but the compiler is not able to spot it dsharew May 23 13 at 930 No read this question Capturing UDP packets sent from my own app try with filter by port you are using in your code Grijesh Chauhan May 23 13 at 931 to tell the truth i am using straight through cable to connect my pc to the other pc But i can see the arp packets from other applications from the second pc dsharew May 23 13 at 936 3 Answers oldest 5 A couple of things to get your packets on the wireair The proper sll_protocol for the arp reply is ETH_P_ARP from linuxif_etherh There was an error in endianness when setting aharp_op It is a network byteorder field of 2 octets so use htons In general the code is a little confused about network and host byteorder It currently sends out the reply very mangled but it is unclear to me whether that is the malicious intent of the code or an accident In the case that you want to send real correct IP addresses use htonl and htons when building the reply To fix endianness Properly include arpaineth Use htons htonl ntohs and ntohl always Their implementation makes it a NOP if its not needed on your platform When setting up data to be sent out from host always process it with hton When interpreting data from the network always ntoh it before comparing with local variables In summary the changes I did were 1 sll_protocol htonsETH_P_ARP when sending data 2 aharp_op htonsARPOP_REPLY in the reply arp 3 Removed the nonsensical ntohs on aharp_hd and aharp_pr You dont want to convert data to host byteorder when populating the send buffer unless you really really actually do 4 Added ntohs conversions and proper defines in some of the comparisons 5 some other small fixes 6 disabled the bit doing systemsudo Full code at pastebin Here is a diff thuovilaglx src so arp diff arp2 c arp_orig c 13d12 include arpaineth 20c19 define DEVICE eth1 define DEVICE eth0 25c24 int s 1 Socketdescriptor int s 0 Socketdescriptor 92c91 socket_address sll_protocol htons ETH_P_ARP socket_address sll_protocol htons ETH_P_IP 95c94 socket_address sll_pkttype 0 PACKET_OTHERHOST socket_address sll_pkttype PACKET_OTHERHOST 112c111 if ntohs eh h_proto ETH_P_ARP if htons eh h_proto 0x806 119c118 if ntohs ah arp_op ARPOP_REQUEST if htons ah arp_op 0x0001 139d137 if 0 145d142 endif 182c179 eh h_proto htons ETH_P_ARP eh h_proto ETH_ARP 200 201c197 198 aharp_hd ntohsaharp_hd aharp_pr ntohsaharp_pr ah arp_hd ntohs ah arp_hd ah arp_pr ntohs ah arp_pr 203c200 ah arp_op htons ARPOP_REPLY ah arp_op 0x0002 EDIT Some wireshark advice Capture ether proto 0x0806 or arp for short Use the pseudo device that captures any packets Your packets should become visible On linux if you want to stop the network stack from interfering use echo 8 procsysnetipv4confallarp_ignore EDIT 2 I am not completely sure about the ETH_P_ARP It might have been a snap judgement on my part Using ETH_P_IP is correct in the ARP header field but Im not sure which one to use for the packet socket sll_protocol Also notice that socket_addresssll_pkttype PACKET_OTHERHOST has no effect when sending see man 7 packet Also the mandatory SO observation that you should always use at least Wall when using gcc or clang as a compilation flag EDIT 3 I changed the program a little more and updated the answer and diff accordingly Surprisingly it does indeed seem that sll_protocol needs to be ETH_P_ARP My copy of the man 7 packet doesnt even say it is used for anything but the packet doesnt go out on the wire as ARP without it answered May 23 13 at 1127 thuovila 1694 8 16 i used your code but but problem still not solved dsharew May 24 13 at 1851 Is it still the same problem It did seem to work for me I saw packets going out and wireshark reported an ARP collisionduplicate MAC detected Also the hosts emitting the ARP request seemed to accept the self made reply thuovila May 24 13 at 1859 6 I took user6343961s code did some cleaning and splicing and implemented support for automatically getting interface IP address Also the parameters come from CLI instead of hardcoding bind is also used to get only ARP from the interface we want Have fun This code works for me include syssocketh include sysioctlh include asmtypesh include stringh include stdioh include stdlibh include unistdh include linuxif_packeth include

O O O O O O O O O O O O O O O O O O O B I O B I O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


The

import

javaxxmlrpcencoding

can

not

be

resolved

Ask

Question

Im

trying

to

run

this

code

httpwwwnactemacuksoftwareterminewebservicetermine_soap_clientjava

and

I

get

the

following

error

The

import

javaxxmlrpcencoding

can

not

be

resolved

Im

using

Oracle

Java

What

do

I

need

to

add

to

get

this

resolved

Thanks

Ivelina

java

soap

improve

this

question

asked

Jun

at

ivita

Answer

active

oldest

votes

Add

the

relevant

jars

to

your

build

path

axisjar

jaxrpcjar

and

xercesjar

edited

May

at

Rob

answered

Jun

at

NINCOMPOOP

Your

Answer

By

clicking

Post

Your

Answer

you

acknowledge

that

you

have

read

our

updated

terms

of

service

and

cookie

policy

and

that

your

continued

use

of

the

website

is

subject

to

these

policies

Not

the

answer

youre

looking

for

Browse

other

questions

tagged

java

soap

or

ask

your

own

question

asked

years

months

ago

viewed

times

active

months

ago

Blog

Adios

to

Winter

Bash

question

feed

O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O


Android

ListView

elements

with

multiple

clickable

buttons

Ask

Question

Ive

a

ListView

where

every

element

in

the

list

contains

a

TextView

and

two

different

Buttons

Something

like

this

ListView

Button

Button

Button

Button

and

so

on

With

this

code

I

can

create

an

OnItemClickListener

for

the

whole

item

listView

setOnItemClickListener

new

OnItemClickListener

Override

public

void

onItemClick

AdapterView

list

int

position

long

id

Log

i

TAG

onListItemClick

position

However

I

dont

want

the

whole

item

to

be

clickable

but

only

the

two

buttons

of

each

list

element

So

my

question

is

how

do

I

implement

a

onClickListener

for

these

two

buttons

with

the

following

parameters

int

button

which

button

of

the

element

has

been

clicked

int

position

which

is

the

element

in

the

list

on

which

the

button

click

happened

Update

I

found

a

solution

as

described

in

my

answer

below

Now

I

can

clicktap

the

button

via

the

touch

screen

However

I

cant

manually

select

it

with

the

trackball

It

always

selects

the

whole

list

item

and

from

there

goes

directly

to

the

next

list

item

ignoring

the

buttons

even

though

I

set

setFocusabletrue

and

setClickabletrue

for

the

buttons

in

getView

I

also

added

this

code

to

my

custom

list

adapter

Override

public

boolean

areAllItemsEnabled

return

false

Override

public

boolean

isEnabled

int

position

return

false

This

causes

that

no

list

item

is

selectable

at

all

any

more

But

it

didnt

help

in

making

the

nested

buttons

selectable

Anyone

an

idea

android

listview

improve

this

question

edited

Nov

at

asked

Nov

at

znq

Are

these

still

needed

Stuart

Axon

Jul

at

If

you

look

at

the

BaseAdapter

code

youd

see

that

areAllItemsEnabled

and

isEnabled

are

just

hardcoded

to

true

making

them

simple

placeholders

without

any

logic

Artem

Russakovskii

Jun

at

What

if

I

want

to

use

a

SimpleCursorAdapterDo

I

have

to

make

a

custom

adapter

extends

simplecursoradapter

oratis

Dec

at

Answers

oldest

The

solution

to

this

is

actually

easier

than

I

thought

You

can

simply

add

in

your

custom

adapters

getView

method

a

setOnClickListener

for

the

buttons

youre

using

Any

data

associated

with

the

button

has

to

be

added

with

myButtonsetTag

in

the

getView

and

can

be

accessed

in

the

onClickListener

via

viewgetTag

I

posted

a

detailed

solution

on

my

blog

as

a

tutorial

Rob

Bajorek

answered

Nov

at

znq

my

blog

url

not

working

vnshetty

Apr

at

Fixed

Thanks

for

reporting

znq

Apr

at

how

exacly

did

you

get

the

curItemurl

from

query

would

you

be

more

specific

oratis

Dec

at

Thanks

znq

That

was

very

useful

for

me

Saved

a

bundle

of

time

Nandagopal

T

Feb

at

it

would

be

great

to

have

answers

in

SO

itself

and

not

point

to

some

other

page

The

blog

link

is

dead

gsb

Apr

at

show

more

comments

This

is

sort

of

an

appendage

znqs

answer

There

are

many

cases

where

you

want

to

know

the

row

position

for

a

clicked

item

AND

you

want

to

know

which

view

in

the

row

was

tapped

This

is

going

to

be

a

lot

more

important

in

tablet

UIs

You

can

do

this

with

the

following

custom

adapter

private

static

class

CustomCursorAdapter

extends

CursorAdapter

protected

ListView

mListView

protected

static

class

RowViewHolder

public

TextView

mTitle

public

TextView

mText

public

CustomCursorAdapter

Activity

activity

super

mListView

activity

getListView

Override

public

void

bindView

Context

context

Cursor

cursor

do

what

you

need

to

do

Override

public

newView

Context

context

Cursor

cursor

ViewGroup

parent

view

inflate

context

R

layout

RowViewHolder

holder

new

RowViewHolder

holder

mTitle

TextView

findViewById

R

id

Title

holder

mText

TextView

findViewById

R

id

holder

mTitle

setOnClickListener

mOnTitleClickListener

holder

mText

setOnClickListener

mOnTextClickListener

view

setTag

holder

return

private

OnClickListener

mOnTitleClickListener

new

OnClickListener

Override

public

void

onClick

v

final

int

position

mListView

getPositionForView

v

getParent

Log

v

TAG

Title

clicked

row

d

position

private

OnClickListener

mOnTextClickListener

new

OnClickListener

Override

public

void

onClick

v

final

int

position

mListView

getPositionForView

v

getParent

Log

v

TAG

Text

clicked

row

d

position

edited

Sep

at

answered

Mar

at

or

you

can

also

use

something

like

this

ListView

mListView

ListView

vgetParentgetParent

Mar

at

How

can

I

get

the

position

if

I

use

the

adapter

in

a

separate

class

I

simply

used

the

position

inside

OnClickListener

something

like

this

It

suggest

to

make

the

position

to

final

in

getView

method

of

the

adapter

If

I

make

that

change

the

position

I

get

is

same

for

all

the

items

in

the

lisview

Could

you

suggest

me

how

to

solve

this

Manikandan

Jun

at

Thats

because

when

you

use

the

position

parameter

of

the

getView

method

it

gives

you

the

position

of

the

item

of

the

list

which

is

most

recently

created

but

not

the

one

you

clicked

Archiebpgc

Jul

at

Manikandan

If

youre

using

the

getView

method

of

the

adapter

then

you

already

have

the

position

passed

to

you

in

the

method

params

In

that

case

you

could

use

something

like

setTag

to

store

the

position

information

Jul

at

I

am

using

your

code

in

my

app

but

the

click

event

is

called

after

a

delay

or

when

I

try

to

scroll

the

listview

Any

ideas

why

this

is

happening

Abdullah

Umer

Aug

at

show

more

comments

For

future

readers

To

select

manually

the

buttons

with

the

trackball

use

myListView

setItemsCanFocus

true

And

to

disable

the

focus

on

the

whole

list

items

myListView

setFocusable

false

myListView

setFocusableInTouchMode

false

myListView

setClickable

false

It

works

fine

for

me

I

can

click

on

buttons

with

touchscreen

and

also

alows

focus

an

click

using

keypad

answered

Mar

at

Fabricio

PH

Only

this

one

helped

me

Thank

you

a

lot

Onur

Apr

at

Thanks

I

tryed

the

other

answers

and

neither

work

for

me

Fabricio

PH

May

at

What

about

a

ExpandableListView

I

have

multiple

views

in

the

item

I

just

want

the

views

in

the

child

clickable

thanks

twlkyao

Sep

at

add

a

comment

I

dont

have

much

experience

than

above

users

but

I

faced

this

same

issue

and

I

Solved

this

with

below

Solution

Button

androidid

idbtnRemove

idbtnEdit

androidbackground

drawablebtn

androidtext

stringremove

androidonClick

btnRemoveClick

btnRemoveClick

Click

event

public

void

btnRemoveClick

v

final

int

position

listviewItem

getPositionForView

v

getParent

listItem

remove

position

ItemAdapter

notifyDataSetChanged

edited

Jun

at

sherpya

answered

Jun

at

Bhavin

Chauhan

interesting

solution

sherpya

Jun

at

add

a

comment

Probably

youve

found

how

to

do

it

but

you

can

call

ListView

setItemsCanFocus

true

and

now

your

buttons

will

catch

focus

Fabricio

PH

answered

Aug

at

Rafal

Thanks

man

it

workssaved

my

time

Ghanshyam

Nayma

Jun

at

add

a

comment

I

am

not

sure

about

be

the

best

way

but

works

fine

and

all

code

stays

in

your

ArrayAdapter

package

br

com

fontolan

pessoas

arrayadapter

import

java

util

List

import

android

content

Context

import

android

text

Editable

import

android

text

TextWatcher

import

android

view

LayoutInflater

import

android

view

import

android

view

OnClickListener

import

android

view

ViewGroup

import

android

widget

ArrayAdapter

import

android

widget

EditText

import

android

widget

ImageView

import

br

com

fontolan

pessoas

R

import

br

com

fontolan

pessoas

model

Telefone

public

class

TelefoneArrayAdapter

extends

ArrayAdapter

Telefone

private

TelefoneArrayAdapter

telefoneArrayAdapter

private

Context

context

private

EditText

tipoEditText

private

EditText

telefoneEditText

private

ImageView

deleteImageView

public

TelefoneArrayAdapter

Context

context

List

Telefone

values

super

context

R

layout

values

this

telefoneArrayAdapter

this

this

context

context

Override

public

getView

int

position

convertView

ViewGroup

parent

LayoutInflater

inflater

LayoutInflater

context

getSystemService

Context

view

inflater

inflate

R

layout

parent

false

tipoEditText

EditText

findViewById

R

id

telefoneEditText

EditText

findViewById

R

id

deleteImageView

ImageView

findViewById

R

id

final

int

i

position

final

Telefone

telefone

this

getItem

position

tipoEditText

setText

telefone

getTipo

telefoneEditText

setText

telefone

getTelefone

TextWatcher

tipoTextWatcher

new

TextWatcher

Override

public

void

onTextChanged

CharSequence

s

int

start

int

before

int

count

Override

public

void

beforeTextChanged

CharSequence

s

int

start

int

count

int

after

Override

public

void

afterTextChanged

Editable

s

telefoneArrayAdapter

getItem

i

setTipo

s

toString

telefoneArrayAdapter

getItem

i

setIsDirty

true

TextWatcher

telefoneTextWatcher

new

TextWatcher

Override

public

void

onTextChanged

CharSequence

s

int

start

int

before

int

count

Override

public

void

beforeTextChanged

CharSequence

s

int

start

int

count

int

after

Override

public

void

afterTextChanged

Editable

s

telefoneArrayAdapter

getItem

i

setTelefone

s

toString

telefoneArrayAdapter

getItem

i

setIsDirty

true

tipoEditText

addTextChangedListener

tipoTextWatcher

telefoneEditText

addTextChangedListener

telefoneTextWatcher

deleteImageView

setOnClickListener

new

OnClickListener

Override

public

void

onClick

v

telefoneArrayAdapter

remove

telefone

return

edited

Feb

at

Maveňツ

answered

Jul

at

mFontolan

add

a

comment

I

Know

its

late

but

this

may

help

this

is

an

example

how

I

write

custom

adapter

class

for

different

click

actions

public

class

CustomAdapter

extends

BaseAdapter

TextView

title

Button

public

long

getItemId

int

position

return

position

public

int

getCount

return

mAlBasicItemsnav

size

size

of

your

list

array

public

Object

getItem

int

position

return

position

public

getView

int

position

convertView

ViewGroup

parent

if

convertView

convertView

getLayoutInflater

inflate

R

layout

false

use

sublayout

which

you

want

to

inflate

in

your

each

list

item

title

TextView

convertView

findViewById

R

id

textViewnav

see

you

have

to

find

id

by

using

convertViewfindViewById

title

setText

mAlBasicItemsnav

get

position

Button

convertView

findViewById

R

id

setOnClickListener

new

OnClickListener

Override

public

void

onClick

your

click

action

if

you

have

different

click

action

at

different

positions

then

if

position

click

action

of

list

item

on

button

click

if

position

click

action

of

list

item

on

button

click

similarly

for

button

Button

convertView

findViewById

R

id

setOnClickListener

new

OnClickListener

Override

public

void

onClick

your

click

action

return

convertView

edited

Dec

at

WaKo

answered

Sep

at

Redman

add

a

comment

Isnt

the

platform

solution

for

this

implementation

to

use

a

context

menu

that

shows

on

a

long

press

Is

the

question

author

aware

of

context

menus

Stacking

up

buttons

in

a

listview

has

performance

implications

will

clutter

your

UI

and

violate

the

recommended

UI

design

for

the

platform

On

the

flipside

context

menus

by

nature

of

not

having

a

passive

representation

are

not

obvious

to

the

end

user

Consider

documenting

the

behaviour

This

guide

should

give

you

a

good

start

edited

Feb

at

Maveňツ

answered

Jul

at

Gusdor

And

the

reason

for

the

downvote

is

Gusdor

Mar

at

protected

by

Community

Jul

at

Thank

you

for

your

interest

in

this

question

Because

it

has

attracted

lowquality

or

spam

answers

that

had

to

be

removed

posting

an

answer

now

requires

reputation

on

this

site

the

association

bonus

does

not

count

Would

you

like

to

answer

one

of

these

unanswered

questions

instead

Not

the

answer

youre

looking

for

Browse

other

questions

tagged

android

listview

or

ask

your

own

question

asked

years

month

ago

viewed

times

active

years

ago

People

Chatting

SO

Close

Vote

Reviewers

hour

ago

tripleee

PHP

hour

ago

Tiffany

Hot

Network

Questions

Apply

Max

to

each

element

of

a

list

Why

would

a

previously

spacefaring

civilization

become

unable

to

build

another

interstellar

ship

Why

have

all

negatives

been

exposed

to

one

frame

Why

do

rockets

do

not

glide

back

How

could

a

planet

have

a

sky

without

stars

at

night

Buying

someone

a

steam

game

without

a

steam

account

Can

Intel

Optane

memory

compensate

for

less

RAM

Is

the

direction

of

the

electric

field

relative

to

the

magnetic

field

in

an

electromagnetic

wave

a

convention

Multivariate

Normal

expectation

of

X

given

Y

is

doublytruncated

Is

it

normal

for

a

lithium

ion

battery

to

read

with

a

very

low

voltage

on

a

multimeter

What

will

be

the

smallest

ring

containing

two

rings

Word

for

Significant

enough

to

make

a

difference

Asking

about

eating

policy

during

interview

Get

all

platform

event

definitions

via

REST

API

Can

someone

read

my

EMail

if

I

lose

access

to

my

domain

What

ship

is

this

and

which

military

campaign

What

was

the

highest

temperature

ever

recorded

on

the

surface

of

Earth

How

do

you

say

in

all

directions

in

a

single

word

Are

there

vintage

or

historical

bitmapped

fonts

available

for

noncommercial

use

Can

I

invert

other

companies

logos

and

use

them

for

advertising

them

on

my

site

Can

you

plug

the

computer

directly

into

the

wall

to

access

the

internet

New

manager

is

a

jerk

is

it

OK

to

quit

without

providing

a

reason

Is

this

projected

return

from

Wealthify

too

low

Why

dont

the

Democrats

make

a

deal

to

give

Trump

his

border

wall

in

exchange

for

campaign

finance

reform

question

feed

O B I O B I I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O

multiple clickable buttons
listview elements


Android onDestroy or similar method in Application class 57 4 I am extending Application class to work with some global variables that need context I know there is onCreate method in the Application class that gets called before any other onCreate in activities but I would like to know if there is onDestroy or similar method in the Application class that could be overridden so that I would be able to store variables in persistent memory unregister listener and send last message to server before app process gets killed If not is there any other way to do that android androidlifecycle improve this question asked Jun 24 13 at 1439 Maybe Julius 375 1 6 12 No do this in the appropriate methods of Activity or Service Michael Butscher Jun 24 13 at 1445 11 Already did that What a nuisance Actually the very reason Im extending application is because using android service makes me want to cry or jump off the building Maybe Julius Jun 24 13 at 1455 6 Answers oldest 23 accepted There is no such call back on a production device for the Application class The things you want to do should usually be done right after the changes are made or in the onPause of the respective app component answered Jun 24 13 at 1452 Raghav Sood 713k 19 156 171 6 In android there is no concept of closing an app The user just leaves this is the only event that you will be aware of onPause in an activity You should adapt your app design to fit this lifecycle You shouldnt try to find a workaround it can just make things worse what if the user runs out of battery or if your app crashes all the data will be lost I dont know what kind of data youre working with but this usually result in a poor rating on Google Play Even if something bad happens all the data should already been saved AsyncTasks are great for this dont be afraid to start one on every change to constantly save data answered Jun 24 13 at 1504 Dalmas 229k 8 53 70 4 First of all Im an absolute beginner I need to execute some code when my app exits yes I know not such thing in Android and this works ok for me I have MyApplication wich extends Application As a member of MyApplication there is an AtomicInteger field named activeActivitiesNumber and a public getter method All the application activities extend MyActivy which itself extendes Activity MyActivity overrides onCreate onResume and onStop methods and also have a protected field Protected MyAppication mAppState a OnCreate super onCreate mAppState this getApplication b onResume super OnResume myAppState getactiveActivitiesNumber addAndGet 1 c onStop super onStop if myAppStatemyAppState getactiveActivitiesNumber decrementAndGet 1 call exiting code for instance a public method defined in MyApplication It has a problem if you start any activity tan doesnt belong to your application for instance send an email it will fire the exiting method Another problem dont know if it is a real o theoretical one is that there is no guarantee tan in some situations onStop will be called Hope this help answered May 8 14 at 1310 absoluteBeginner 41 1 0 You can override onDestroy in the Activity which will be the last one closed in your app and check if its finishing In this case your code will not be invoked on a device rotation But you should be aware that onDestroy is not invoked when an app is closed through device home button Override public void onDestroy super onDestroy if isFinishing do your stuff here answered Jul 10 at 1120 dzikovskyy 2810 2 19 31 0 You can use registerActivityLifecycleCallbacks in the Application class with the following callbacks I recommend creating an AppLifecycleCallbacks class that extends the ActivityLifecycleCallbacks public interface ActivityLifecycleCallbacks void onActivityCreated Activity activity Bundle savedInstanceState void onActivityStarted Activity activity void onActivityResumed Activity activity void onActivityPaused Activity activity void onActivityStopped Activity activity void onActivitySaveInstanceState Activity activity Bundle outState void onActivityDestroyed Activity activity edited Sep 5 at 1748 answered Sep 5 at 1740 box 2031 20 28 1 Another option you have is to override onDestroy on the first screen of your app so it will be called right before your process gets killed answered Jun 24 13 at 1458 Dhanesh Budhrani 140 3 14 25 imho thats not too great an idea as onDestroy will also be called when the device is rotated i just got bitten pretty badly by that problem katzenhut Sep 4 14 at 834 Id say its still a viable option in the general case for a singleactivity app that doesnt allow rotation or is careful to detect this so long as its not 100 vital that the cleanup code gets run ie vital data wont be lost Ionoclast Brigham Jun 6 16 at 1921 2 for a singleactivity app that doesnt allow rotation or is careful to detect this so long as its not 100 vital that the cleanup code gets run Seems very far from the general case natario Oct 29 16 at 1406 Not the answer youre looking for Browse other questions tagged android androidlifecycle or ask your own question

B O O O O O B I O O O O O B I O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O


Resize

image

with

javascript

canvas

smoothly

Ask

Question

Im

trying

to

resize

some

images

with

canvas

but

Im

clueless

on

how

to

smoothen

them

On

photoshop

browsers

etc

there

are

a

few

algorithms

they

use

eg

bicubic

bilinear

but

I

dont

know

if

these

are

built

into

canvas

or

not

Heres

my

fiddle

httpjsfiddlenetEWupT

var

canvas

document

createElement

canvas

var

ctx

canvas

getContext

canvas

width

canvas

height

ctx

drawImage

img

document

body

appendChild

canvas

The

first

one

is

a

normal

resized

image

tag

and

the

second

one

is

canvas

Notice

how

the

canvas

one

is

not

as

smooth

How

can

I

achieve

smoothness

javascript

canvas

improve

this

question

asked

Oct

at

steve

Answers

oldest

You

can

use

downstepping

to

achieve

better

results

Most

browsers

seem

to

use

linear

interpolation

rather

than

bicubic

when

resizing

images

Update

There

has

been

added

a

quality

property

to

the

specs

imageSmoothingQuality

which

is

currently

available

in

Chrome

only

Unless

one

chooses

no

smoothing

or

nearest

neighbor

the

browser

will

always

interpolate

the

image

after

downscaling

it

as

this

function

as

a

lowpass

filter

to

avoid

aliasing

Bilinear

uses

pixels

to

do

the

interpolation

while

bicubic

uses

so

by

doing

it

in

steps

you

can

get

close

to

bicubic

result

while

using

bilinear

interpolation

as

seen

in

the

resulting

images

var

canvas

document

getElementById

canvas

var

ctx

canvas

getContext

var

img

new

img

onload

function

set

size

proportional

to

image

canvas

height

canvas

width

img

height

img

width

step

resize

to

var

oc

document

createElement

canvas

octx

oc

getContext

oc

width

img

width

oc

height

img

height

octx

drawImage

img

oc

width

oc

height

step

octx

drawImage

oc

oc

width

oc

height

step

resize

to

final

size

ctx

drawImage

oc

oc

width

oc

height

canvas

width

canvas

height

img

src

img

src

width

height

canvas

id

canvas

width

canvas

Run

code

snippet

Depending

on

how

drastic

your

resize

is

you

can

might

skip

step

if

the

difference

is

less

In

the

demo

you

can

see

the

new

result

is

now

much

similar

to

the

image

element

answered

Oct

at

steve

heh

sometimes

these

things

happen

For

images

you

can

usually

override

this

by

setting

a

css

BTW

Oct

at

Ken

the

first

result

worked

great

but

when

I

change

the

images

you

can

see

it

being

too

blurry

jsfiddlenetkcHLG

What

can

be

done

in

this

case

and

others

steve

Oct

at

steve

you

can

reduce

the

number

of

steps

to

just

or

for

some

images

this

works

fine

See

also

this

answer

which

is

similar

to

this

but

here

I

added

a

sharpen

convolution

to

it

so

you

can

make

the

resulting

image

sharper

after

it

has

been

downscaled

Oct

at

steve

here

is

an

modified

fiddle

with

Bill

using

only

one

extra

step

Oct

at

neaumusic

the

code

is

a

continuation

of

OPs

code

If

you

open

the

fiddle

youll

see

ctx

being

defined

I

have

inlined

it

here

to

avoid

misunderstandings

Nov

at

show

more

comments

I

created

a

reusable

Angular

service

to

handle

high

quality

resizing

of

images

canvases

for

anyone

whos

interested

The

service

includes

two

solutions

because

they

both

have

their

own

pros

cons

The

lanczos

convolution

approach

is

higher

quality

at

the

cost

of

being

slower

whereas

the

stepwise

downscaling

approach

produces

reasonably

antialiased

results

and

is

significantly

faster

Example

usage

angular

module

demo

controller

ExampleCtrl

function

imageService

EXAMPLE

USAGE

NOTE

its

bad

practice

to

access

the

DOM

inside

a

controller

but

this

is

just

to

show

the

example

usage

resize

by

lanczossinc

filter

imageService

resize

myimg

then

function

resizedImage

do

something

with

resized

image

resize

by

stepping

down

image

size

in

increments

of

imageService

resizeStep

myimg

then

function

resizedImage

do

something

with

resized

image

answered

Sep

at

the

links

is

not

working

Ryan

Monreal

Jan

at

Sorry

about

that

I

had

changed

my

github

username

Just

updated

the

link

Jan

at

I

saw

the

word

angular

I

got

that

funny

feeling

SuperUberDuper

Jul

at

Since

Trung

Le

Nguyen

Nhats

fiddle

isnt

correct

at

all

it

just

uses

the

original

image

in

the

last

step

I

wrote

my

own

general

fiddle

with

performance

comparison

FIDDLE

Basically

its

img

onload

function

var

canvas

document

createElement

canvas

ctx

canvas

getContext

oc

document

createElement

canvas

octx

oc

getContext

canvas

width

width

destination

canvas

size

canvas

height

canvas

width

img

height

img

width

var

cur

width

Math

floor

img

width

height

Math

floor

img

height

oc

width

cur

width

oc

height

cur

height

octx

drawImage

img

cur

width

cur

height

while

cur

width

width

cur

width

Math

floor

cur

width

height

Math

floor

cur

height

octx

drawImage

oc

cur

width

cur

height

cur

width

cur

height

ctx

drawImage

oc

cur

width

cur

height

canvas

width

canvas

height

edited

May

at

Community

answered

Sep

at

Sebastian

Ott

Most

underrated

answer

ever

seen

Amsakanna

Apr

at

I

created

a

library

that

allows

you

to

downstep

any

percentage

while

keeping

all

the

color

data

httpsgithubcomdanschumannlimbyresizeblobmasterlibcanvas_resizejs

That

file

you

can

include

in

the

browser

The

results

will

look

like

photoshop

or

image

magick

preserving

all

the

color

data

averaging

pixels

rather

than

taking

nearby

ones

and

dropping

others

It

doesnt

use

a

formula

to

guess

the

averages

it

takes

the

exact

average

answered

Jul

at

Funkodebat

I

would

probably

use

webgl

to

resize

now

Funkodebat

Feb

at

Based

on

answer

I

rewrite

code

generally

for

anyone

wants

var

oc

document

createElement

canvas

octx

oc

getContext

oc

width

img

width

oc

height

img

height

octx

drawImage

img

while

oc

width

width

oc

width

oc

height

octx

drawImage

oc

oc

width

oc

height

oc

width

width

oc

height

oc

width

img

height

img

width

octx

drawImage

img

oc

width

oc

height

UPDATE

JSFIDDLE

DEMO

Here

is

my

ONLINE

DEMO

answered

Jun

at

Trung

Le

Nguyen

Nhat

This

wont

work

each

time

you

resize

the

canvas

it

will

clear

its

context

You

need

canvases

Here

its

just

the

same

as

directly

calling

drawImage

with

the

final

dimensions

Kaiido

Jun

at

This

still

work

Here

is

my

fiddle

Trung

Le

Nguyen

Nhat

Jun

at

I

wrote

small

jsutility

to

crop

and

resize

image

on

frontend

Here

is

link

on

GitHub

project

Also

you

can

get

blob

from

final

image

to

send

it

import

imageSqResizer

from

imagesquareresizerjs

let

resizer

new

imageSqResizer

imageinput

dataUrl

document

getElementById

imageoutput

src

dataUrl

Get

blob

let

formData

new

FormData

formData

append

resizer

blob

get

dataUrl

document

getElementById

imageoutput

src

resizer

dataUrl

answered

Nov

at

Diyaz

Yakubov

While

some

of

those

codesnippets

are

short

and

working

they

arent

trivial

to

follow

and

understand

As

i

am

not

a

fan

of

copypaste

from

stackoverflow

i

would

like

developers

to

understand

the

code

they

are

push

into

they

software

hope

youll

find

the

below

useful

DEMO

Resizing

images

with

JS

and

HTML

Canvas

Demo

fiddler

you

may

find

different

methods

to

do

this

resize

that

will

help

you

understand

how

the

code

is

working

and

why

Full

code

of

both

demo

and

TypeScript

method

that

you

may

want

to

use

in

your

code

can

be

found

in

GitHub

project

this

is

the

final

code

export

class

ImageTools

string

constructor

ResizeImage

string

width

number

height

number

let

img

new

img

src

img

onload

Check

if

the

image

require

resize

at

all

if

img

height

height

img

width

width

this

TODO

Call

method

to

do

something

with

the

resize

image

else

Make

sure

the

width

and

height

preserve

the

original

aspect

ratio

and

adjust

if

needed

if

img

height

img

width

width

Math

floor

height

img

width

img

height

else

height

Math

floor

width

img

height

img

width

let

resizingCanvas

HTMLCanvasElement

document

createElement

canvas

let

resizingCanvasContext

resizingCanvas

getContext

Start

with

original

image

size

resizingCanvas

width

img

width

resizingCanvas

height

img

height

Draw

the

original

image

on

the

temp

resizing

canvas

resizingCanvasContext

drawImage

img

resizingCanvas

width

resizingCanvas

height

let

curImageDimensions

width

Math

floor

img

width

height

Math

floor

img

height

let

halfImageDimensions

width

height

Quickly

reduce

the

dize

by

each

time

in

few

itterations

until

the

size

is

less

then

time

the

target

size

the

motivation

for

it

is

to

reduce

the

aliasing

that

would

have

been

created

with

direct

reduction

of

very

big

image

to

small

image

while

curImageDimensions

width

width

Reduce

the

resizing

canvas

by

half

and

refresh

the

image

halfImageDimensions

width

Math

floor

curImageDimensions

width

halfImageDimensions

height

Math

floor

curImageDimensions

height

resizingCanvasContext

drawImage

resizingCanvas

curImageDimensions

width

curImageDimensions

height

halfImageDimensions

width

halfImageDimensions

height

curImageDimensions

width

halfImageDimensions

width

curImageDimensions

height

halfImageDimensions

height

Now

do

final

resize

for

the

resizingCanvas

to

meet

the

dimension

requirments

directly

to

the

output

canvas

that

will

output

the

final

image

let

outputCanvas

HTMLCanvasElement

document

createElement

canvas

let

outputCanvasContext

outputCanvas

getContext

outputCanvas

width

width

outputCanvas

height

height

outputCanvasContext

drawImage

resizingCanvas

curImageDimensions

width

curImageDimensions

height

width

height

output

the

canvas

pixels

as

an

image

params

format

quality

this

outputCanvas

toDataURL

imagejpeg

TODO

Call

method

to

do

something

with

the

resize

image

answered

Dec

at

Eyal

c

Your

Answer

By

clicking

Post

Your

Answer

you

acknowledge

that

you

have

read

our

updated

terms

of

service

and

cookie

policy

and

that

your

continued

use

of

the

website

is

subject

to

these

policies

Not

the

answer

youre

looking

for

Browse

other

questions

tagged

javascript

canvas

or

ask

your

own

question

asked

years

months

ago

viewed

times

active

days

ago

question

feed

B I O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O